【Java】如何给你的图片添加自定义水印(附完整代码)?

这是一篇关于怎么尽可能的用尽你电脑里的所有字体给你的图片加水印。。。。

先上效果~

当然这只是其中一部分字体,,,我也是今天才发现我电脑里居然装了那么多字体 ==

好了废话不多说直接上完整代码~

Java 复制代码
import io.swagger.models.auth.In;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
*  @Description
 *  @Author  aqin1012 AQin.
*  @Date  12/29/23 5:38 PM
*  @Version  1.0
*/
public class AddTextWatermark {
    static String imagePath = "/Users/aqin1012/Pictures/20231220-111041.jpeg"; // 图片路径
    static String text = "AQin.AI"; // 水印文字
    static Integer FONT_SIZE = 70;


    public static void main(String[] args) {
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (String fontName : fontNames) {
            System.out.println(fontName);
            String outputImagePath = "/Users/aqin1012/Pictures/20231220-111041-" + fontName + ".jpeg"; // 输出图片路径
            addWatermark(fontName, outputImagePath);
        }
    }

    private static void addWatermark(String fontName, String outputImagePath) {
        Font font = new Font(fontName, Font.BOLD, FONT_SIZE); // 指定字体样式

        try {
            File imageFile = new File(imagePath);
            BufferedImage image = ImageIO.read(imageFile);

            Graphics2D g2d = (Graphics2D) image.getGraphics();
            g2d.setFont(font);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // 计算文字位置
            FontMetrics fontMetrics = g2d.getFontMetrics(font);
            Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
            int centerX = (image.getWidth() - (int) rect.getWidth()) / 10;
            int centerY = (image.getHeight() - (int) rect.getHeight()) / 10 * 9 + fontMetrics.getAscent();

            // 添加文字水印
            g2d.setColor(Color.white); // 水印文字的颜色
            g2d.drawString(text, centerX, centerY);
            g2d.dispose();

            // 保存图片
            ImageIO.write(image, "jpg", new File(outputImagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这段代码用到了Graphics2D,Graphics2D是Java中用于绘图的工具类,是Graphics类的扩展,可以绘制形状、图像、文本等各种二维内容,上面的代码的逻辑是先搜索你电脑里都安装了哪些字体,然后依次用这些字体在特定的位置给特定地址的图片加上特定的文字水印~ 当然如果想批量就在外面搞个循环读取文件夹里的所有图片,如果想要修改水印位置就可以修改代码里的centerX和centerY,比如下面这个~~

就是在原有的基础上添加了透明度和循环

Java 复制代码
g2d.setColor(new Color(255, 255, 255, 128)); // 水印文字的颜色(红色,半透明)
while (centerX < image.getWidth()) {
    while (centerY < image.getHeight()) {
        g2d.drawString(text, centerX, centerY);
        centerY += fontMetrics.getAscent() * 2;
    }
    centerX += 300;
    centerY = 0;
}

搞定撒花*★,°* :.☆( ̄▽ ̄)/$:.°★

相关推荐
java1234_小锋33 分钟前
Java高频面试题:SpringBoot为什么要禁止循环依赖?
java·开发语言·面试
2501_944525541 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 账户详情页面
android·java·开发语言·前端·javascript·flutter
计算机学姐1 小时前
基于SpringBoot的电影点评交流平台【协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·spring·信息可视化·echarts·推荐算法
Filotimo_1 小时前
Tomcat的概念
java·tomcat
索荣荣2 小时前
Java Session 全面指南:原理、应用与实践(含 Spring Boot 实战)
java·spring boot·后端
Amumu121382 小时前
Vue Router(二)
java·前端
念越2 小时前
数据结构:栈堆
java·开发语言·数据结构
千寻技术帮3 小时前
10333_基于SpringBoot的家电进存销系统
java·spring boot·后端·源码·项目·家电进存销
dear_bi_MyOnly3 小时前
【多线程——线程状态与安全】
java·开发语言·数据结构·后端·中间件·java-ee·intellij-idea