天天看點

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

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

前幾天,在其他網站上看到一個表情自動生成器,自己就試着做了一下,沒想到還真給搞定了~

目前,可以處理“臣妾真的做不到啊”、“媽媽再打我一次”、“王寶強泰囧三張圖檔”,如想處理其他圖檔,在類 cn.lulei.util.img.ImgParams 、前台index.html 和 index.js 兩個檔案做相應的配置即可實作。

具體效果如下:

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

整體項目結構如下圖所示

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

其他的實作都很簡單,自己也不在做詳細的介紹,參照源代碼即可,整個項目的難點主要在圖像的處理過程,是以做了一個圖檔處理類 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】

即可擷取下載下傳連結

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

擷取更多資料

請加小助理微信