java在图片上加盖公章和添加水印
时间: 2023-10-07 20:05:04 浏览: 281
在Java中添加水印和公章可以使用Java的图像处理库 - ImageIO和Graphics2D。下面是一个简单的示例代码,演示如何在一张图片上添加水印和公章:
```java
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class AddWatermark {
public static void main(String[] args) throws Exception {
String imagePath = "test.jpg";
String watermarkPath = "watermark.png";
String sealPath = "seal.png";
BufferedImage image = ImageIO.read(new File(imagePath));
BufferedImage watermark = ImageIO.read(new File(watermarkPath));
BufferedImage seal = ImageIO.read(new File(sealPath));
Graphics2D g = image.createGraphics();
int watermarkWidth = image.getWidth() / 3;
int watermarkHeight = watermarkWidth * watermark.getHeight() / watermark.getWidth();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
g.drawImage(watermark, image.getWidth() - watermarkWidth, image.getHeight() - watermarkHeight, watermarkWidth, watermarkHeight, null);
int sealWidth = image.getWidth() / 4;
int sealHeight = sealWidth * seal.getHeight() / seal.getWidth();
g.drawImage(seal, image.getWidth() / 2 - sealWidth / 2, image.getHeight() / 2 - sealHeight / 2, sealWidth, sealHeight, null);
g.dispose();
ImageIO.write(image, "jpg", new File("result.jpg"));
}
}
```
这个示例代码加载了一张名为test.jpg的图片,以及两张水印和公章图片。它使用Graphics2D把水印和公章添加到原始图片上,并将结果保存到result.jpg文件中。
您也可以根据需要调整代码。希望这可以帮到您!
阅读全文