java实现给图片添加图片水印
java
图片
图片水印
具体代码:
// 水印透明度 private static float alpha = 0.5f; /** * 给图片添加水印图片、可设置水印图片旋转角度 * * @param iconPath 水印图片路径 * @param srcImgPath 源图片路径 * @param location 水印图片位置 * @param degree 水印图片旋转角度 */ public static void markImageByIcon(HttpServletRequest request, HttpServletResponse response, String iconPath, String srcImgPath, String location, Integer degree) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到画笔对象 Graphics2D g = buffImg.createGraphics(); // 设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); // 设置水印旋转角度(默认对角线角度) if (null != degree) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } else { //根据三角形相关定理,计算对角线角度 double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight()); double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal); double acos = Math.acos(v); double myDegree = Math.toDegrees(acos); g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度 ImageIcon imgIcon = new ImageIcon(iconPath); Image img = imgIcon.getImage(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 水印图片的位置 int x = 0, y = 0; if (StringUtils.equals(location, "left-top")) { x = 30; y = 30; } else if (StringUtils.equals(location, "right-top")) { x = buffImg.getWidth() - img.getWidth(null) - 30; y = 30; } else if (StringUtils.equals(location, "left-bottom")) { x += 30; y = buffImg.getHeight() - img.getHeight(null) - 30; } else if (StringUtils.equals(location, "right-bottom")) { x = buffImg.getWidth() - img.getWidth(null) - 30; y = buffImg.getHeight() - img.getHeight(null) - 30; } else { x = (buffImg.getWidth() - img.getWidth(null)) / 2; y = (buffImg.getHeight() - img.getHeight(null)) / 2; } g.drawImage(img, x, y, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); // os = new FileOutputStream(targerPath); os = response.getOutputStream(); ImageIO.write(buffImg, "JPG", os); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } }
登录后复制
(视频教程推荐:java视频)
smbfile时也可以写成这样:
// 展示时 添加水印 public void showRemarkImage(String filePath, String iconPath, HttpServletRequest request, HttpServletResponse response) { InputStream is = null; OutputStream os = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { SmbFile smbFile = new SmbFile(filePath); is = smbFile.getInputStream(); os = response.getOutputStream(); Image srcImg = ImageIO.read(is); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 得到画笔对象 Graphics2D g = buffImg.createGraphics(); // 设置对线段的锯齿状边缘处理 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); // 旋转角度处理(根据三角形相关定理,计算对角线角度) // double lengthOfDiagonal = Math.sqrt(buffImg.getWidth() * buffImg.getWidth() + buffImg.getHeight() * buffImg.getHeight()); // double v = (Math.pow(buffImg.getWidth(), 2) + Math.pow(lengthOfDiagonal, 2) - Math.pow(buffImg.getHeight(), 2)) / (2 * buffImg.getWidth() * lengthOfDiagonal); // double acos = Math.acos(v); // double myDegree = Math.toDegrees(acos); // g.rotate(-Math.toRadians(myDegree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度 ImageIcon imgIcon = new ImageIcon(iconPath); Image img = imgIcon.getImage(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 水印图片居中 int x = 0, y = 0; x = (buffImg.getWidth() - img.getWidth(null)) / 2; y = (buffImg.getHeight() - img.getHeight(null)) / 2; g.drawImage(img, x, y, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); g.dispose(); ImageIO.write(buffImg, "png", out); byte[] b = out.toByteArray(); response.addHeader("Content-Length", String.valueOf(b.length)); os.write(b, 0, b.length); os.flush(); } catch (Exception e) { LogUtil.error("附件下载失败,时间:" + DateUtil.formatToDateTimeText(new Date()) + "原因:" + e.getMessage()); // throw new ApplicationException("文件读取失败:" + e.getMessage(), e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); IOUtils.closeQuietly(out); } }
登录后复制
推荐教程:java入门程序
以上是java实现给图片添加图片水印的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4
