哈哈,用Java打造一个有趣的表情生成器(附源码)

来源:https://blog.csdn.net/xiaojimanman/article/details/18556703

前几天,在其他网站上看到一个表情自动生成器,自己就试着做了一下,没想到还真给搞定了~

目前,可以处理“臣妾真的做不到啊”、“妈妈再打我一次”、“王宝强泰囧三张图片”,如想处理其他图片,在类 cn.lulei.util.img.ImgParams 、前台index.html 和 index.js 两个文件做相应的配置即可实现。

具体效果如下:

整体项目结构如下图所示

其他的实现都很简单,自己也不在做详细的介绍,参照源代码即可,整个项目的难点主要在图像的处理过程,因此做了一个图片处理类 ImgDeal 来实现图像的绘制,相关源码如下:

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;
  }
}

资源获取方法,老规矩啦!

识别下方二维码,添加后回复【023】

即可获取下载链接

获取更多资料

请加小助理微信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值