java rgba图片添加水印
时间: 2023-09-02 13:14:47 浏览: 201
可以使用Java的Graphics2D类来实现图片添加水印的功能。具体实现步骤如下:
1. 加载图片文件,创建Graphics2D对象。
```java
BufferedImage image = ImageIO.read(new File("image.png"));
Graphics2D g2d = image.createGraphics();
```
2. 设置水印字体、大小、颜色等属性。
```java
Font font = new Font("Arial", Font.BOLD, 36);
Color color = new Color(255, 255, 255, 128);
g2d.setFont(font);
g2d.setColor(color);
```
3. 绘制水印文字或图片。
```java
String watermarkText = "Watermark";
g2d.drawString(watermarkText, 10, 50);
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
g2d.drawImage(watermarkImage, 0, 0, null);
```
4. 保存修改后的图片。
```java
ImageIO.write(image, "png", new File("output.png"));
```
完整代码示例:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageWatermark {
public static void main(String[] args) throws Exception {
// 加载图片文件,创建Graphics2D对象
BufferedImage image = ImageIO.read(new File("image.png"));
Graphics2D g2d = image.createGraphics();
// 设置水印字体、大小、颜色等属性
Font font = new Font("Arial", Font.BOLD, 36);
Color color = new Color(255, 255, 255, 128);
g2d.setFont(font);
g2d.setColor(color);
// 绘制水印文字或图片
String watermarkText = "Watermark";
g2d.drawString(watermarkText, 10, 50);
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
g2d.drawImage(watermarkImage, 0, 0, null);
// 保存修改后的图片
ImageIO.write(image, "png", new File("output.png"));
}
}
```
阅读全文