最代码官方的gravatar头像
最代码官方 2014-02-08 14:42:32

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

以前分享过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也能恶搞。

<%@ 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支持

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

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

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

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


打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友