java 图片加水印实例代码
发布时间:2020-12-14 14:21:27 所属栏目:Java 来源:网络整理
导读:复制代码 代码如下: try { String targetImg = "D:/Blue hills.jpg"; // String pressImg = "D:/20130311220300.jpg"; String pressImg = "D:/html_original.jpg"; File f1 = new File(targetImg); Image src = ImageIO.read(f1); int width = src.getWidth(
复制代码 代码如下: try { String targetImg = "D:/Blue hills.jpg"; // String pressImg = "D:/20130311220300.jpg"; String pressImg = "D:/html_original.jpg"; File f1 = new File(targetImg); Image src = ImageIO.read(f1); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src,width,null); File f2 = new File(pressImg); Image src_pao = ImageIO.read(f2); int width_pao = src_pao.getWidth(null); int height_pao = src_pao.getHeight(null); g.drawImage(src_pao,(width-+width_pao)/2,(height-height_pao)/2,width_pao,height_pao,null); // g.drawImage(src_pao,100,null); g.dispose(); FileOutputStream out = new FileOutputStream(targetImg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); System.out.println("水印添加成功!!"); } catch (Exception e) { // TODO: handle exception System.out.println("水印添加失败!!"); e.printStackTrace(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |