package com.gwi.util;
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.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import kiosk.util.KDate;
import org.apache.log4j.Logger;
public class SealImgSyntheticUtil {
private static Logger logger = Logger.getLogger(SealImgSyntheticUtil.class);
public static final int IMG_WIDTH = 180;//缩放图片宽
public static final int IMG_HEIGHT = 180;//缩放图片高
public static final String IMG_TYPE = "jpg";
/**
* 图片加文字
* @param srcImgPath 原图路径
* @param tarImgPath 新图路径
* @param waterMarkContent 水印内容
* @param markContentColor 水印颜色
* @param font 字体
* @param x 水印位置x
* @param y 水印位置y
*/
public void addWaterMark(String srcImgPath, String tarImgPath,String waterMarkContent, Color markContentColor, Font font,int x,int y,String imageType) {
try {
//文件转化为图片
File srcImgFile = new File(srcImgPath);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
//设置图片属性
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(markContentColor);
g.setFont(font);
//水印位置设置
g.drawString(waterMarkContent, x, y);
g.dispose();
//图片输出
writeImage(tarImgPath,bufImg, imageType);
} catch (Exception e) {
logger.error("合成电子印章错误:", e);
}
}
/**
* 图片缩小
* @param originalImage 原图
* @param type
* @return
*/
public void resizeImage(BufferedImage originalImage,String imageType,int type,String tarImgPath) {
try {
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
writeImage(tarImgPath,resizedImage, imageType);
} catch (Exception e) {
logger.error("合成电子印章错误:", e);
}
}
/**
* 输出图片
* @param tarImgPath 图片存放位置
* @param bufImg
* @param imageType
*/
public static void writeImage(String tarImgPath,BufferedImage bufImg,String imageType){
FileOutputStream outImgStream;
try {
outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, imageType, outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
logger.error("合成电子印章错误:", e);
}
}
public static void main(String[] args) {
try {
Font font = new Font("微软雅黑", Font.PLAIN, 50);// 水印字体
String srcImgPath = "D:\\y.jpg"; // 源图片地址
String tarImgPath = "D:\\t.jpg"; // 待存储的地址
String tarImgPath2 = "D:\\ target.jpg"; //缩放图片地址
String waterMarkContent =KDate.getDateTime(null,"yyyyMMdd");// 水印内容
Color color = new Color(255, 0, 0, 255);
new SealImgSyntheticUtil().addWaterMark(srcImgPath, tarImgPath,waterMarkContent, color, font,110,250,IMG_TYPE);
File file = new File(tarImgPath);
BufferedImage originalImage = ImageIO.read(file);
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
new SealImgSyntheticUtil().resizeImage(originalImage, IMG_TYPE,type,tarImgPath2);
} catch (IOException e) {
System.out.println(e);
}
}
}
效果: