最代码官方
								2014-02-08 14:42:32
							
							
								证精
							在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。
以前分享过java实现的通过图片模板生成泰囧的例子:
这次分享的是在线jsp版本的
预览地址:http://demo.zuidaima.com/taijiong.jsp
也可以自定义其他文字,如:
<%@ page language="java" contentType="image/gif;charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.commons.io.FileUtils" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="javax.imageio.ImageIO" %>
<%!
int getLength(String text) {
	int textLength = text.length();
	int length = textLength;
	for (int i = 0; i < textLength; i++) {
		if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
			length++;
		}
	}
	return (length % 2 == 0) ? length / 2 : length / 2 + 1;
}
%>
<%
	String targetImg=null;
	String t1=request.getParameter("t1");
	String pressText1="你想加入最代码的java版微博开发活动weibo4j么?";
	if(t1!=null){
		pressText1=new String(t1.getBytes("iso8859-1"),"utf-8");
	}
	String pressText2="还差1牛币?";
	String t2=request.getParameter("t2");
	if(t2!=null){
		pressText2=new String(t2.getBytes("iso8859-1"),"utf-8");
	}
	String t3=request.getParameter("t3");
	String pressText3="啊哈哈哈哈!!!现在涨成1000牛币了!!!";
	if(t3!=null){
		pressText3=new String(t3.getBytes("iso8859-1"),"utf-8");
	}
	int fontSize=18;
	Color color=Color.WHITE;
	try {
		File file=new File("/data/www/www.zuidaima.com/images/113/1132701359327232.jpg");
		Image image = ImageIO.read(file);
		int width = image.getWidth(null);
		int height = image.getHeight(null);
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufferedImage.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		g.setFont(new Font("微软雅黑", Font.BOLD, fontSize));
		g.setColor(color);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
				1));
		int x = 135;
		int y = 163;
		int width_1 = fontSize * getLength(pressText1);
		int height_1 = fontSize;
		int widthDiff = width - width_1;
		int heightDiff = height - height_1;
		if (x < 0) {
			x = widthDiff / 2;
		} else if (x > widthDiff) {
			x = widthDiff;
		}
		if (y < 0) {
			y = heightDiff / 2;
		} else if (y > heightDiff) {
			y = heightDiff;
		}
		g.drawString(pressText1, x, y + height_1);
		x = 135;
		y = 355;
		width_1 = fontSize * getLength(pressText1);
		height_1 = fontSize;
		widthDiff = width - width_1;
		heightDiff = height - height_1;
		if (x < 0) {
			x = widthDiff / 2;
		} else if (x > widthDiff) {
			x = widthDiff;
		}
		if (y < 0) {
			y = heightDiff / 2;
		} else if (y > heightDiff) {
			y = heightDiff;
		}
		g.drawString(pressText2, x, y + height_1);
		x = 135;
		y = 550;
		width_1 = fontSize * getLength(pressText1);
		height_1 = fontSize;
		widthDiff = width - width_1;
		heightDiff = height - height_1;
		if (x < 0) {
			x = widthDiff / 2;
		} else if (x > widthDiff) {
			x = widthDiff;
		}
		if (y < 0) {
			y = heightDiff / 2;
		} else if (y > heightDiff) {
			y = heightDiff;
		}
		g.drawString(pressText3, x, y + height_1);
		g.dispose();
		ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
	} catch (Exception e) {
		e.printStackTrace();
	}
%>
另外centos环境下会出现乱码的问题,windows下没有问题,需要修改jdk的fonts支持
需要将微软雅黑的字体复制到jdk的fonts目录,如:
/usr/local/jdk1.6.0_31/jre/lib/fonts/fallback/msyh.ttf
猜你喜欢
请下载代码后再发表评论
    
相关代码
				
最近下载
				
最近浏览
				
                奋斗的小蚂蚁     LV16
                10月31日
            
            
        
                酷少小新     LV2
                6月7日
            
            
        
                ma406805131     LV19
                2024年12月18日
            
            
        
                mhywad    
                2023年6月25日
            
            
                    暂无贡献等级
            
        
                13133117021     LV5
                2023年5月21日
            
            
        
                95959595959     LV13
                2023年4月3日
            
            
        
                微信网友_6411724627349504     LV3
                2023年3月31日
            
            
        
                这里的山路十八弯     LV4
                2022年12月28日
            
            
        
                一生热爱11     LV3
                2022年11月16日
            
            
        
                Weishenghui     LV7
                2022年10月28日
            
            
        




    
                
                