线jsp版泰囧表情的生成器代码,不用PS也能恶搞。

来源:互联网 发布:mysql中的存储过程 编辑:程序博客网 时间:2024/05/21 14:04

原文:在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。

源代码下载地址:hhttp://www.zuidaima.com/share/1687663042939904.htm

以前分享过java实现的通过图片模板生成泰囧的例子:

java版泰囧表情的生成器代码,不用PS也能恶搞。

这次分享的是在线jsp版本的

预览地址:http://demo.zuidaima.com/taijiong.jsp

在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。

也可以自定义其他文字,如:

http://demo.zuidaima.com/taijiong.jsp?t1=%E6%98%A5%E8%8A%82happy%E5%AE%8C%E4%BA%86%E5%90%A7%EF%BC%9F&t2=%E8%BF%98%E6%B2%A1happy%E5%A4%9F%E4%B9%88%EF%BC%9F&t3=%E8%BF%99%E5%91%A8%E5%85%AD%E6%97%A5%E5%B0%BC%E7%8E%9B%E5%8A%A0%E7%8F%AD%E8%BF%9E%E4%B8%8A9%E5%A4%A9%E7%8F%AD%E5%91%80%EF%BC%81%EF%BC%81%EF%BC%81

在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。

001<%@ page language="java" contentType="image/gif;charset=utf-8" pageEncoding="utf-8"%>
002<%@ page import="java.io.*" %>
003<%@ page import="org.apache.commons.io.FileUtils" %>
004<%@ page import="java.awt.*" %>
005<%@ page import="java.awt.image.BufferedImage" %>
006<%@ page import="javax.imageio.ImageIO" %>
007 
008<%!
009int getLength(String text) {
010    int textLength = text.length();
011    int length = textLength;
012    for (int i = 0; i < textLength; i++) {
013        if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
014            length++;
015        }
016    }
017    return (length % 2 == 0) ? length / 2 : length / 2 1;
018}
019%>
020 
021<%
022    String targetImg=null;
023    String t1=request.getParameter("t1");
024    String pressText1="你想加入最代码的java版微博开发活动weibo4j么?";
025    if(t1!=null){
026        pressText1=new String(t1.getBytes("iso8859-1"),"utf-8");
027    }
028    String pressText2="还差1牛币?";
029    String t2=request.getParameter("t2");
030    if(t2!=null){
031        pressText2=new String(t2.getBytes("iso8859-1"),"utf-8");
032    }
033    String t3=request.getParameter("t3");
034    String pressText3="啊哈哈哈哈!!!现在涨成1000牛币了!!!";
035    if(t3!=null){
036        pressText3=new String(t3.getBytes("iso8859-1"),"utf-8");
037    }
038    int fontSize=18;
039    Color color=Color.WHITE;
040    try {
041        File file=new File("/data/www/www.zuidaima.com/images/113/1132701359327232.jpg");
042        Image image = ImageIO.read(file);
043        int width = image.getWidth(null);
044        int height = image.getHeight(null);
045        BufferedImage bufferedImage = new BufferedImage(width, height,
046                BufferedImage.TYPE_INT_RGB);
047        Graphics2D g = bufferedImage.createGraphics();
048        g.drawImage(image, 00, width, height, null);
049        g.setFont(new Font("微软雅黑", Font.BOLD, fontSize));
050        g.setColor(color);
051        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
052                1));
053        int x = 135;
054        int y = 163;
055        int width_1 = fontSize * getLength(pressText1);
056        int height_1 = fontSize;
057        int widthDiff = width - width_1;
058        int heightDiff = height - height_1;
059        if (x < 0) {
060            x = widthDiff / 2;
061        else if (x > widthDiff) {
062            x = widthDiff;
063        }
064        if (y < 0) {
065            y = heightDiff / 2;
066        else if (y > heightDiff) {
067            y = heightDiff;
068        }
069 
070        g.drawString(pressText1, x, y + height_1);
071 
072        x = 135;
073        y = 355;
074        width_1 = fontSize * getLength(pressText1);
075        height_1 = fontSize;
076        widthDiff = width - width_1;
077        heightDiff = height - height_1;
078        if (x < 0) {
079            x = widthDiff / 2;
080        else if (x > widthDiff) {
081            x = widthDiff;
082        }
083        if (y < 0) {
084            y = heightDiff / 2;
085        else if (y > heightDiff) {
086            y = heightDiff;
087        }
088        g.drawString(pressText2, x, y + height_1);
089 
090        x = 135;
091        y = 550;
092        width_1 = fontSize * getLength(pressText1);
093        height_1 = fontSize;
094        widthDiff = width - width_1;
095        heightDiff = height - height_1;
096        if (x < 0) {
097            x = widthDiff / 2;
098        else if (x > widthDiff) {
099            x = widthDiff;
100        }
101        if (y < 0) {
102            y = heightDiff / 2;
103        else if (y > heightDiff) {
104            y = heightDiff;
105        }
106        g.drawString(pressText3, x, y + height_1);
107        g.dispose();
108        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
109    catch (Exception e) {
110        e.printStackTrace();
111    }
112%>

另外centos环境下会出现乱码的问题,windows下没有问题,需要修改jdk的fonts支持

在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。

需要将微软雅黑的字体复制到jdk的fonts目录,如:

/usr/local/jdk1.6.0_31/jre/lib/fonts/fallback/msyh.ttf

微软雅黑下载地址:http://demo.zuidaima.com/download/msyh.ttf


0 0
原创粉丝点击