当前位置: 首页 > news >正文

Java核心: 为图片生成水印

今天干了一件特别不务正业的事,做了一个小程序用来给图片添加水印。事情的起因是需要将自己的身份证照片分享给别人,手边并没有一个趁手的工具来生成图片水印。很多APP提供了水印的功能,但会把我的图片上传到他们的服务器,身份证太敏感了,显然我并不想让别人有机会保留照片。

我把图片处理做了一个抽象,入参是BufferedImage,对图片添加水印、盲印、隐式写入后返回新的BufferedImage作为结果。

package org.keyniu.watermark.image;import java.awt.image.BufferedImage;public interface ImageProcess {/*** @param org* @return*/public BufferedImage process(BufferedImage org) throws Exception;}

1. 基本实现

我们先给出一版基本的实现

package org.keyniu.watermark.image;...
/*** 基于JDK的Graphics2D实现*/
public class Graphics2DWatermark implements ImageProcess {...     public BufferedImage process(BufferedImage org) throws UnsupportedEncodingException, NoSuchAlgorithmException {BufferedImage marked = new BufferedImage(org.getWidth(), org.getHeight(), BufferedImage.TYPE_INT_RGB);Graphics2D g2d = marked.createGraphics();g2d.drawImage(org, 0, 0, null); // 创建结果图片,并绘制原图// 设置字体,计算每个水印文字的块大小FontRenderContext context = g2d.getFontRenderContext();Font font = new Font(fontName, Font.BOLD, fontSize);g2d.setFont(font);TextMetadata textMeta = getTextMetadata(font, context, text);// 设置水印透明度,默认选择45°g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // 设置透明度,0.0~1.0g2d.rotate(Math.PI * rotateArch / 180, org.getWidth() / 2, org.getHeight() / 2);// 计算图片中每行能放几个水印,要放多少行ImageMetadata imageMeta = new ImageMetadata(org.getWidth(), org.getHeight());int columnCount = imageMeta.getColumnCount(textMeta.getWidth());int rowCount = imageMeta.getRowCount(textMeta.getHeight() + textMeta.getCrcHeight());AffineTransform transform = g2d.getTransform();for (int rIdx = 0; rIdx < rowCount; rIdx++) {for (int cIdx = 0; cIdx < columnCount; cIdx++) {g2d.setTransform(transform);randomRotate(g2d, imageMeta);randomTransform(g2d);watermark(g2d, imageMeta, textMeta, rIdx, cIdx);}}g2d.setTransform(transform);// 结束绘制,释放资源g2d.dispose();return marked;}private void watermark(Graphics2D g2d, ImageMetadata imageMeta, TextMetadata textMeta, int rIdx, int cIdx) {Point offset = imageMeta.getOffset();Point textLoc = textMeta.textLocation(rIdx, cIdx);Point crcLoc = textMeta.crcLocation(rIdx, cIdx);randomGradient(g2d, offset.x + textLoc.x, offset.y + textLoc.y, textMeta.totalTextWidth(), textMeta.totalTextHeight());g2d.drawString(textMeta.getText(), offset.x + textLoc.x, offset.y + textLoc.y);randomGradient(g2d, offset.x + crcLoc.x, offset.y + crcLoc.y, textMeta.totalCrcWidth(), textMeta.totalCrcHeight());g2d.drawString(textMeta.getCrc(), offset.x + crcLoc.x, offset.y + crcLoc.y);}protected void randomRotate(Graphics2D g2d, ImageMetadata imageMeta) { // 供子类覆盖,自定义旋转的逻辑}protected void randomTransform(Graphics2D g2d) { // 供子类覆盖,自定义AffineTransform的逻辑}protected void randomGradient(Graphics2D g2d, int x, int y, int dx, int dy) { // 供子类覆盖,实现渐变色的逻辑}... 
}

本地main方法测试,测试代码是这样的的。

public static void main(String[] args) throws Exception {Graphics2DWatermark watermark = new Graphics2DWatermark("仅用于车险办理");BufferedImage image = ImageIO.read(new File("D:\\blog\\linux.png"));BufferedImage certified = watermark.process(image);ImageIO.write(certified, "jpg", new File("D:\\blog\\linux_mark.png"));
}

左边是原始图片,右边是加了水印后的图片

2. 旋转变换

太有规律的水印很容易就被擦除水印,上面的实现中我们预留了3个接口,用来扩展实现,分别是:

  1. randomRotate,输出一行水印之前,有机会做旋转
  2. randomTransform,输出一行水印前,有机会执行AffineTransfrom
  3. randomGradient,输出水印文字和CRC之前,有机会设置渐变色

我们提供了一个增强实现

public class EnhancedGraphics2DWatermark extends Graphics2DWatermark {public EnhancedGraphics2DWatermark(String text) {super(text);}protected void randomRotate(Graphics2D g2d, ImageMetadata imageMeta) {g2d.rotate(Math.PI * (Math.random() * 45 - 45) / 180, imageMeta.getSourceX(), imageMeta.getSourceY());}@Overrideprotected void randomTransform(Graphics2D g2d) {if (Math.random() < 0.5) {g2d.shear(Math.random() * 0.2, 0);} else {g2d.shear(0, Math.random() * 0.2);}}protected void randomGradient(Graphics2D g2d, int fx, int fy, int tx, int ty) {Color from = generateColor();Color to = reverse(from);GradientPaint gp = new GradientPaint(fx, fy, from, tx, ty, to);g2d.setPaint(gp);}private Color generateColor() {int r = (int) (256 * Math.random() + fontColor.getRed()) & 0xFF;int g = (int) (256 * Math.random() + fontColor.getGreen()) & 0xFF;int b = (int) (256 * Math.random() + fontColor.getBlue()) & 0xFF;return new Color(r, g, b);}private Color reverse(Color c) {return new Color((256 - c.getRed()) & 0xFF, (256 - c.getGreen()) & 0xFF, (256 - c.getBlue()) & 0XFF, c.getAlpha());}}

修改测试的main方法,改用这个实现

public static void main(String[] args) throws Exception {Graphics2DWatermark watermark = new EnhancedGraphics2DWatermark("仅用于车险办理");BufferedImage image = ImageIO.read(new File("D:\\blog\\linux.png"));BufferedImage certified = watermark.process(image);ImageIO.write(certified, "jpg", new File("D:\\blog\\linux_mark.png"));
}

这是新的水印效果

3. 提供GUI访问

直接通过代码来调用对非程序来说太有友好了,所以我在上一篇的基础上做了一点点改成,做了一个GUI入口,通过菜单设置水印的文案

然后再使用JFileChooser打开一个图片文件,最终展示水印后的图片。

完整的项目代码见附件,如果使用GraalVM打包称为可执行文件,就可以分享给你的小伙伴们使用啦。

http://www.lryc.cn/news/365227.html

相关文章:

  • Spark MLlib 机器学习详解
  • MySQL报ERROR 2002 (HY000)解决
  • 【校招】【社招】字节跳动UG营销算法工程师招聘
  • Go实战 | 使用Go-Fiber采用分层架构搭建一个简单的Web服务
  • Web自动化测试框架+PO模式分层实战(超细整理)
  • 光猫、路由器的路由模式、桥接模式、拨号上网
  • iOS--工厂设计模式
  • [Python]用Qt6和Pillow实现截图小工具
  • Podman和Docker的区别
  • Go微服务: 分布式Cap定理和Base理论
  • Mysql学习(四)——SQL通用语法之DQL
  • 【ARFoundation自学05】人脸追踪(AR Face manager)实现
  • Vulnhub-DC-2
  • VNC server ubuntu20 配置
  • c++--priority_queue和仿函数
  • Harmony os Next——关系型数据库relationalStore.RdbStore的使用
  • 快手直播限流怎么办?
  • 【MySQL】数据库入门基础
  • cannot allocate memory in static TLS block
  • Leetcode 654:最大二叉树
  • uniapp小程序src引用服务器图片时全局变量与图片路径拼接
  • 比较PWM调光和无极调光
  • 【高校科研前沿】新疆生地所陈亚宁研究员团队在GeoSus发文:在1.5°C和2°C全球升温情景下,中亚地区暴露于极端降水的人口增加
  • 使用 OKhttp3 实现 智普AI ChatGLM HTTP 调用(SSE、异步、同步)
  • 智慧校园教学模式的崛起:优化学习体验
  • ffmpeg视频编码原理和实战-(5)对编码过程进行封装并解决丢帧问题
  • halo进阶-主题插件使用
  • 资深开发推荐的IDEA 插件
  • 数学题目系列(一)|丑数|各位和|埃氏筛|欧拉筛
  • k8s学习--Secret详细解释与应用