原文:在线jsp版泰囧表情的生成器代码,不用PS也能恶搞。
源代码下载地址:hhttp://www.zuidaima.com/share/1687663042939904.htm
以前分享过java实现的通过图片模板生成泰囧的例子:
java版泰囧表情的生成器代码,不用PS也能恶搞。
这次分享的是在线jsp版本的
预览地址:http://demo.zuidaima.com/taijiong.jsp
也可以自定义其他文字,如:
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
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" %> |
009 | int 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 ) { |
017 | return (length % 2 == 0 ) ? length / 2 : length / 2 + 1 ; |
022 | String targetImg= null ; |
023 | String t1=request.getParameter( "t1" ); |
024 | String pressText1= "你想加入最代码的java版微博开发活动weibo4j么?" ; |
026 | pressText1= new String(t1.getBytes( "iso8859-1" ), "utf-8" ); |
028 | String pressText2= "还差1牛币?" ; |
029 | String t2=request.getParameter( "t2" ); |
031 | pressText2= new String(t2.getBytes( "iso8859-1" ), "utf-8" ); |
033 | String t3=request.getParameter( "t3" ); |
034 | String pressText3= "啊哈哈哈哈!!!现在涨成1000牛币了!!!" ; |
036 | pressText3= new String(t3.getBytes( "iso8859-1" ), "utf-8" ); |
039 | Color color=Color.WHITE; |
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, 0 , 0 , width, height, null ); |
049 | g.setFont( new Font( "微软雅黑" , Font.BOLD, fontSize)); |
051 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, |
055 | int width_1 = fontSize * getLength(pressText1); |
056 | int height_1 = fontSize; |
057 | int widthDiff = width - width_1; |
058 | int heightDiff = height - height_1; |
061 | } else if (x > widthDiff) { |
066 | } else if (y > heightDiff) { |
070 | g.drawString(pressText1, x, y + height_1); |
074 | width_1 = fontSize * getLength(pressText1); |
076 | widthDiff = width - width_1; |
077 | heightDiff = height - height_1; |
080 | } else if (x > widthDiff) { |
085 | } else if (y > heightDiff) { |
088 | g.drawString(pressText2, x, y + height_1); |
092 | width_1 = fontSize * getLength(pressText1); |
094 | widthDiff = width - width_1; |
095 | heightDiff = height - height_1; |
098 | } else if (x > widthDiff) { |
103 | } else if (y > heightDiff) { |
106 | g.drawString(pressText3, x, y + height_1); |
108 | ImageIO.write(bufferedImage, "jpg" , response.getOutputStream()); |
109 | } catch (Exception e) { |
另外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