图片添加文字水印(java)

需要注意几个踩坑的地方(代码51行,部署在Linux 必须有这个字体,当时踩坑了,代码72注意输出图片的格式)

package com.example.Watermark;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Cimpany yuJ.wang
 * @ClassName: PictureUtils
 * @Description: 图片添加水印
 * @Date: 2023/3/13 13:48
 * @Author: 老王头
 */
public class PictureUtils {

    //水印字体大小
    public static final int FONT_SIZE = 18;
    //水印间隔
    private static final int X_MOVE = 40;
    private static final int Y_MOVE = 40;



    /**
     * @Description:  图片添加文字水印
     * * @Param filePath: 图片路径
     * @Param watermark: 水印文字
     * @Param newFilePath: 保存路径
     * @Param opacity: 透明度设置
     * @Param size: 字体大小
     * @Param angle: 旋转度
     * @return: void
     * @Author: laoWangTou
     * @Date: 2023/3/13
     */
    public static void watermark(String filePath,String watermark,String newFilePath,float opacity,int size,int angle) throws IOException{
        // 读取原图片
        File file = new File(filePath);
        BufferedImage image =  ImageIO.read(file);
        int width = image.getWidth();
        int height = image.getHeight();
        // 创建Buffer缓存对象  在缓存中操作
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取画笔
        Graphics2D g = newImage.createGraphics();
        // 将原图片绘制到缓存图片上
        g.drawImage(image, 0, 0, width, height, null);
        // 设置水印的字体样式 (要注意, 部署在Linux 必须有这个字体,当时踩坑了)
        g.setFont(new Font("宋体", Font.BOLD, size));
        // 水印颜色
        g.setColor(Color.red);
        g.rotate(Math.toRadians(angle), width / 2, height / 2);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, opacity));
        int x = -width / 2;
        int y = -height / 2;
        int markWidth = FONT_SIZE * getTextLength(watermark);
        int markHeight = FONT_SIZE;
        while (x < width * 1.5) {
            y = -height / 2;
            while (y < height * 1.5) {
                g.drawString(watermark, x, y);
                y += markHeight + Y_MOVE;
            }
            x += markWidth + X_MOVE;
        }
        // 释放资源
        g.dispose();
        // 保存带水印的图片
        try {
            ImageIO.write(newImage, "jpg", new File(newFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * @Description: 获取text的文本长度
     * * @Param text:
     * @return: int
     * @Author: laoWangTou
     * @Date: 2023/3/13
     */
    private static int getTextLength(String text) {
        int length = text.length();
        for (int i = 0; i < text.length(); i++) {
            String s = String.valueOf(text.charAt(i));
            if (s.getBytes().length > 1) {
                length++;
            }
        }
        length = length % 2 == 0 ? length / 2 : length / 2 + 1;
        return length;
    }


    public static void main(String[] args) throws IOException {
        watermark("D:\\test\\1\\3.jpg","测试水印","D:\\test\\1\\33.jpg",0.2f,20,30);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java可以使用Java2D API来实现图片文字水印。下面是一个简单的示例代码: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageWatermark { public static void main(String[] args) throws IOException { // 读取原始图片 BufferedImage image = ImageIO.read(new File("original.jpg")); // 创建一个空白的图片,大小和原始图片一样 BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); // 获取图片的Graphics2D对象 Graphics2D g = result.createGraphics(); // 将原始图片绘制到空白图片上 g.drawImage(image, 0, 0, null); // 设置水印文字的相关属性 String text = "Hello, world!"; Font font = new Font("Arial", Font.BOLD, 36); Color color = Color.WHITE; // 绘制水印文字 g.setFont(font); g.setColor(color); int x = (image.getWidth() - g.getFontMetrics().stringWidth(text)) / 2; int y = image.getHeight() / 2; g.drawString(text, x, y); // 保存水印图片 ImageIO.write(result, "jpg", new File("watermark.jpg")); } } ``` 在这个示例代码中,我们首先读取原始图片,然后创建一个空白的图片,大小和原始图片一样。接着,我们获取空白图片的Graphics2D对象,将原始图片绘制到空白图片上。然后,我们设置水印文字的相关属性,包括文字内容、字体和颜色,并在空白图片上绘制水印文字。最后,我们将水印图片保存到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值