/**
*@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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
表情自动生成器java代码实现
共30个文件
jpg:6个
java:5个
class:5个
4星 · 超过85%的资源 需积分: 15 70 下载量 49 浏览量
2014-01-20
16:12:06
上传
评论 3
收藏 231KB RAR 举报
温馨提示
实现了臣妾真的做不到啊、妈妈再打我一次、王宝强泰囧三张图片,如想处理其他图片,在类 cn.lulei.util.img.ImgParams 、前台index.html 和 index.js 两个文件做相应的配置即可实现
资源推荐
资源详情
资源评论
收起资源包目录
ImgGen.rar (30个子文件)
ImgGen
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 456B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
src
cn
lulei
servlet
ImgServlet.java 834B
file
img
cqzbd.jpg 43KB
wbqtj.jpg 28KB
mmzdwyc.jpg 21KB
service
ImgService.java 1KB
util
img
ImgDeal.java 3KB
ImgParams.java 2KB
parse
Params.java 2KB
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 724B
classes
cn
lulei
servlet
ImgServlet.class 1KB
file
img
cqzbd.jpg 43KB
wbqtj.jpg 28KB
mmzdwyc.jpg 21KB
service
ImgService.class 2KB
util
img
ImgDeal.class 4KB
ImgParams.class 3KB
parse
Params.class 2KB
lib
index.html 2KB
js
jquery-1.7.2.min.js 93KB
index.js 1KB
.mymetadata 291B
.classpath 529B
.myeclipse
共 30 条
- 1
资源评论
- 未知的生活2019-04-09感谢楼主分享,能实现基本功能。
- szmajf2018-07-04感谢楼主的分享
xiaojimanman
- 粉丝: 391
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功