转载请注明出处:在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。
代码下载地址:http://www.zuidaima.com/share/1687663042939904.htm
以前分享过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" %> <%@ page import="zuidaima.com" %> <%! 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
微软雅黑下载地址:http://demo.zuidaima.com/download/msyh.ttf