您的位置:首页 > 编程语言 > Java开发

表情自动生成器java代码实现

2014-01-20 16:14 316 查看
转载请注明出处:/article/2606849.html

前几天在其他网站上看到一个表情自动生成器,自己就试着做了一下,先看下效果。(目前可以处理 臣妾真的做不到啊、妈妈再打我一次、王宝强泰囧三张图片,如想处理其他图片,在类 cn.lulei.util.img.ImgParams 、前台index.html 和 index.js 两个文件做相应的配置即可实现)







对于上述问题的难点主要在图像的处理过程,因此做了一个图片处理类 ImgDeal 来实现图像的绘制。

/**  
 *@Description:   图片处理类  
 */ 
package cn.lulei.util.img;  

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
  
public class ImgDeal {
	private final static int FONTSIZE = 24;
	
	/**
	 * @param filePath
	 * @param outStream
	 * @param characters
	 * @param heights
	 * @param c
	 * @throws IOException
	 * @Date: 2014-1-20  
	 * @Author: lulei  
	 * @Description: 绘制图片
	 */
	public static void drawImg(String filePath, OutputStream outStream, ArrayList<String> characters, ArrayList<Integer> heights, Color c) throws IOException {
		//参数不对,直接返回
		if(characters == null || heights == null || characters.size() != heights.size()){
			return;
		}
		File file = new File(filePath);
		//文件不存在,直接返回
		if (!file.exists()) {
			return;
		}
		Image img = ImageIO.read(file);
		int[] wh = getImgWH(img);
		BufferedImage bufferedImage = new BufferedImage(wh[0], wh[1], BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufferedImage.createGraphics();
		g.drawImage(img.getScaledInstance(wh[0], wh[1], Image.SCALE_SMOOTH), 0, 0, null);
		g.setColor(c);
		int i = 0;
		//图片上输入文字
		for (String character : characters){
			int[] fl = getFsLs(wh[0], getLength(character, 0));
			g.setFont(new Font("楷体", Font.BOLD, fl[1]));
			g.drawString(character, fl[0], heights.get(i++));
		}
		//将图片输入到输出流中
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
		param.setQuality(1.0F, false);
		encoder.setJPEGEncodeParam(param);
		encoder.encode(bufferedImage);
	}
	
	/**
	 * @param question
	 * @param defaultInt
	 * @return
	 * @Date: 2014-1-20  
	 * @Author: lulei  
	 * @Description: 计算字符长度
	 */
	private static int getLength(String question, int defaultInt){
		int re = 0;
		if (question != null){
			int count = question.length();
			for(int i = 0; i < count; i++) {
				if (question.charAt(i) < 255) {
					re += 5;
				} else {
					re += 9;
				}
			}
		}
		re /= 9;
		return re == 0 ? defaultInt : re;
	}
	
	/**
	 * @param img
	 * @return
	 * @Date: 2014-1-20  
	 * @Author: lulei  
	 * @Description: 获取图片的长宽
	 */
	private static int[] getImgWH(Image img){
		int[] wh = new int[2];
		wh[0] = img.getWidth(null);
		wh[1] = img.getHeight(null);
		return wh;
	}
	
	/**
	 * @param width
	 * @param length
	 * @return
	 * @Date: 2014-1-20  
	 * @Author: lulei  
	 * @Description: 计算生成文字的宽度的起始位置和字体大小
	 */
	private static int[] getFsLs(int width, int length) {
		int[] fl = new int[2];
		if (length * FONTSIZE < width) {
			fl[0] = (width - (length * FONTSIZE)) / 2;
			fl[1] = FONTSIZE;
		} else {
			fl[0] = 5;
			fl[1] = width / length - 1;
		}
		return fl;
	}
}


做完这个图像处理之后,其他的实现都很简单,自己也不在做详细的介绍,其他的就参照源代码即可。

注:自己前端技术很差,前台界面做的太丑~

源代码下载地址:http://download.csdn.net/detail/xiaojimanman/6867463
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: