如何给图片指定位置添加水印
下面是所用的方法,pressImg是要添加的水印图片,targetImg是页面上显示的图片,X和Y是鼠标点击的图片上坐标位置。
但是执行完后插入的水印位置不在鼠标点击的位置上,求大牛帮忙解答一下!!!!
public static void pressImage(String pressImg, String targetImg,
int x, int y) {
try {
//目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth =src.getWidth(null);//1100;
int height = src.getHeight(null);//1000;
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics 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);
System.out.println("x==="+x+" y==="+y );
g.drawImage(src_biao, x-(wideth_biao/2),y-(height_biao/2),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) {
e.printStackTrace();
}
}