Java

Java怎么给图片加水印

小亿
154
2024-01-04 02:11:29
栏目: 编程语言

在Java中给图片加水印可以通过以下步骤实现:

  1. 导入相关的类库:
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
  1. 加载原始图片和水印图片:
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
  1. 创建一个新的图片对象,大小与原始图片相同:
BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
  1. 创建一个图形对象并设置抗锯齿和渲染模式:
Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  1. 将原始图片绘制到新的图片对象上:
graphics.drawImage(originalImage, 0, 0, null);
  1. 设置水印图片的透明度和位置,并绘制到新的图片对象上:
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); // 设置透明度为0.5
graphics.setComposite(alphaComposite);

int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10; // 水印图片的x坐标
int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10; // 水印图片的y坐标
graphics.drawImage(watermarkImage, x, y, null);
  1. 添加水印文本:
graphics.setFont(new Font("Arial", Font.BOLD, 12));
graphics.drawString("watermark", 10, 20);
  1. 保存新的图片对象为文件:
ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));

完整示例代码如下:

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageWatermarkExample {
    public static void main(String[] args) {
        try {
            BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
            BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));

            BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            graphics.drawImage(originalImage, 0, 0, null);

            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
            graphics.setComposite(alphaComposite);

            int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10;
            int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10;
            graphics.drawImage(watermarkImage, x, y, null);

            graphics.setFont(new Font("Arial", Font.BOLD, 12));
            graphics.drawString("watermark", 10, 20);

            ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上代码将原始图片和水印图片合并为一张新的图片,保存为result.jpg。其中,original.jpg是原始图片,watermark.png是水印图片,"watermark"是水印文本。您可以根据实际情况修改文件路径和水印位置、透明度等参数。

0
看了该问题的人还看了