如何给图片指定位置添加水印

Zest_wang 2016-12-28 05:33:46

下面是所用的方法,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();
}
}


...全文
367 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧