package com.lindows.util;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
public class ImageProcess {
/**
* @param processImg
* 水印文件,最好用gif或者png可以支持透明
* @param oldImg
* 原始图片文件
* @param newImg
* 生成图片文件
* @param x
* 水印的横坐标
* @param y
* 水印的纵坐标
*/
public static final void processImg(String processImg, String oldImg,
String newImg, int x, int y) {
try {
// 目标文件
File file = new File(oldImg);
Image oldImage = ImageIO.read(file);
int width = oldImage.getWidth(null);
int height = oldImage.getHeight(null);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(oldImage, 0, 0, width, height, null);
// 水印文件
File file2 = new File(processImg);
Image addImg = ImageIO.read(file2);
int width1 = addImg.getWidth(null);
int height1 = addImg.getHeight(null);
g.drawImage(addImg, width - width1 - x, height - height1 - y,
width1, height1, null);
// g.drawImage(addImg, (width - width1) / 2 - x, (height - height1)/
// 2 - y, null);
// 水印结束
g.dispose();
FileOutputStream out = new FileOutputStream(newImg);
JPEGImageEncoder encorder = JPEGCodec.createJPEGEncoder(out);
encorder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 测试方法
ImageProcess.processImg("G:/png.png", "G:/1.png", "G:/3.jpg", 0, 0);
}
}