package com.az.textile.baseframe.commom.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 类ImageUtil.java的实现描述:处理图片
*
* @author 杨冬 2015年10月13日 下午4:16:10
*/
public final class ImageUtil {
private static final Logger log = LoggerFactory.getLogger(ImageUtil.class);
/**
* 把水印印刷到图片上
*
* @param pressImg -- 水印文件
* @param targetImg -- 目标文件
* @param loaction 水印位置:left-top:左上角,right-top:右上角,left-bottom:左下角,right-bottom:右下角
* @param degree 水印旋转角度
* @time 2015年10月13日
* @author leon
*/
public final static void pressImage(String pressImg, String targetImg, String loaction, Integer degree) {
try {
// 目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
// 水印坐标
int x = 0, y = 0;
if (StringUtil.equals(loaction, "left-top")) {
g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);
} else if (StringUtil.equals(loaction, "right-top")) {
x = wideth - wideth_biao;
} else if (StringUtil.equals(loaction, "left-bottom")) {
y = height - height_biao;
} else if (StringUtil.equals(loaction, "right-bottom")) {
x = wideth - wideth_biao;
y = height - height_biao;
} else {
x = (wideth - wideth_biao) / 2;
y = (height - height_biao) / 2;
}
if (null != degree) {
// 设置水印旋转
g.rotate(Math.toRadians(degree), x, y);
}
g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
log.error("把水印印刷到图片上出错了" + e);
}
}
/**
* 打印文字水印图片
*
* @par pressText --文字
* @pam targetImg -- 目标图片
* @par fontName -- 字体名
* @param foStyle -- 字体
* @param color -- 字体颜色
* @param fontSize -- 字体大小
* @param x -- 偏移量
* @param y
*/
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, int color,
int fontSize, int x, int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize / 2 - y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
log.error("打印文字水印图片出错了" + e);
}
}
// public static void main(String[] args) {
// File fileDir = new File("C:/Users/as/Desktop/img-test");
// File[] tempList = fileDir.listFiles();
// System.out.println("该目录下对象个数:" + tempList.length);
// for (File file : tempList) {
// if (file.isFile()) {
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", file.getPath(), "right-top");
// }
// }
//
// }
// public static void main(String[] args) {
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", "C:/Users/as/Desktop/img-test/1.jpg", "left-top", -45);
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", "C:/Users/as/Desktop/img-test/2.jpg", "right-top", -45);
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", "C:/Users/as/Desktop/img-test/3.jpg", "center", -145);
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", "C:/Users/as/Desktop/img-test/4.jpg", "left-bottom", -45);
// pressImage("C:/Users/as/Desktop/img-test/shuiyin.png", "C:/Users/as/Desktop/img-test/5.jpg", "right-bottom",
// -45);
// }
}