Java多种方式向图片添加自定义水印、图片转换及webp图片压缩

给个创建水印的示例:

/**
     * 获取水印
     *
     * @param watermarkText 水印文字
     * @return 水印bufferimage
     */
    public static BufferedImage getWatermark(String watermarkText) {
        BufferedImage measureBufferdImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics2D measureG2d = measureBufferdImage.createGraphics();
        measureG2d.setFont(new Font("SimSun", Font.BOLD, 32));
        Rectangle2D rectangle2D = measureG2d.getFontMetrics().getStringBounds(watermarkText, null);
        measureG2d.dispose();

        int width = (int) (rectangle2D.getWidth() + 50);
        int height = (int) (rectangle2D.getHeight() + 50);

        // 创建一个 BufferedImage 对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // 获取 Graphics2D 对象
        Graphics2D g2d = image.createGraphics();
        // 设置背景色
        g2d.setColor(new Color(0, 0, 0, 0));
        g2d.fillRect(0, 0, width, height);
        Color customColor = new Color(255, 0, 0, 50); // 半透明红色
        // 设置文字颜色和字体
        g2d.setColor(customColor);
        g2d.setFont(new Font("SimSun", Font.BOLD, 32));
        g2d.drawString(watermarkText, 25, height / 2 + 12);
        // 释放资源
        g2d.dispose();
        return image;
    }

这样可以根据一个字符串的文字生成一个水印图片

利用imageio压缩转换图片并且直接添加水印

public static File compressToWebp(File sourceImage, String watermarkText) throws IOException {
        BufferedImage image = ImageIO.read(new FileInputStream(sourceImage));
        if (StringUtils.isNotBlank(watermarkText)) {
            Graphics2D g2d = image.createGraphics();
            g2d.setFont(new Font("SimSun", Font.BOLD, 32));
            Rectangle2D rectangle2D = g2d.getFontMetrics().getStringBounds(watermarkText, null);

            int width = (int) (rectangle2D.getWidth() + 50);
            int height = (int) (rectangle2D.getHeight() + 50);

            // 设置背景色
            g2d.setColor(new Color(0, 0, 0, 0));
            Color customColor = new Color(255, 0, 0, 50); // 半透明红色
            // 设置文字颜色和字体
            g2d.setColor(customColor);
            g2d.setFont(new Font("SimSun", Font.BOLD, 32));
            g2d.drawString(watermarkText, (int) (image.getWidth() - width), image.getHeight() - height-25);
            // 释放资源
            g2d.dispose();
        }
        // Obtain a WebP ImageWriter instance
        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
        // Configure encoding parameters
        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
        writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);
        File file = new File("d:/test.webp");
        FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(file);
        // Configure the output on the ImageWriter
        writer.setOutput(fileImageOutputStream);
        // Encode
        writer.write(null, new IIOImage(image, null, null), writeParam);
        fileImageOutputStream.flush();
        fileImageOutputStream.close();
        return file;
    }

将webp图片转为png图片

 public static File webpToPng(File sourceImage) throws IOException {
        // 读取webp格式图片
        BufferedImage image = ImageIO.read(sourceImage);
        // 写入png格式图片
        File pngFile = new File("d:/test.png");
        ImageIO.write(image, "png", pngFile);
        System.out.println("转换完成!");
        return pngFile;
    }

利用Thumbnails添加水印

Thumbnails.of(new File("D:/20231108011418994.jpg"))
                    .outputFormat("png")
                    .sourceRegion(Positions.BOTTOM_RIGHT, 200, 100)
                    .size(200, 100).keepAspectRatio(false)
                    .crop(Positions.BOTTOM_CENTER)
                    .watermark(Positions.BOTTOM_RIGHT, 
                        ImageIO.read(new File("D:/20231019193150520-bc.png")), 0.7f)
                    .toOutputStream(new FileOutputStream(new File("D:/20231108011418994-b.jpg")));

20231019193150520-bc.png是水印图片

相关maven包参考


            com.github.nintha
            webp-imageio-core
            0.1.0
            system
            ${project.basedir}/libs/webp-imageio-core-0.1.0.jar
        

            net.coobird
            thumbnailator
            0.4.20
        

你可能感兴趣的:(java,python,开发语言)