制作水印图:
图片1:
图片2:
将图片2叠加到图片1上,输出
package org.example;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static enum WatermarkLocation {
TOP_LEFT,
TOP_RIGHT,
BOTTOM_LEFT,
BOTTOM_RIGHT,
CENTER
}
public static void main(String[] args) {
String inputImagePath = "C:\\Users\\Administrator\\Pictures\\images\\pic1.jpg"; // 原始图片路径
String outputDirectory = "C:\\Users\\Administrator\\Pictures\\images\\water"; // 输出图片路径
String watermarkImagePath = "C:\\Users\\Administrator\\Pictures\\images\\pic2.jpg"; // 水印图片路径
// 添加水印到不同位置
addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.TOP_LEFT);//左上
addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.TOP_RIGHT);//左下
addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.BOTTOM_LEFT);//右上
addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.BOTTOM_RIGHT);//右下
addWatermark(inputImagePath, outputDirectory, watermarkImagePath, WatermarkLocation.CENTER);//居中
}
public static void addWatermark(String inputImagePath, String outputDirectory, String watermarkImagePath, WatermarkLocation location) {
try {
File inputFile = new File(inputImagePath); //原图文件流
File watermarkFile = new File(watermarkImagePath);//水印图文件流
BufferedImage inputImage = ImageIO.read(inputFile);
BufferedImage watermarkImage = ImageIO.read(watermarkFile);
// 创建一个与原始图片大小相同的 BufferedImage
BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_INT_RGB);
// 获取图形上下文以在输出图像上绘制
Graphics2D graphics = outputImage.createGraphics();
// 将原始图片绘制到输出图像
graphics.drawImage(inputImage, 0, 0, null);
int watermarkX = 0;
int watermarkY = 0;
double widthPercentage=0.5;
double heightPercentage=0.5;
// 计算水印图片的宽度和高度,以原图的百分比为基础
int watermarkWidth = (int) (inputImage.getWidth() * widthPercentage);
int watermarkHeight = (int) (inputImage.getHeight() * heightPercentage);
// 根据位置设置水印的坐标
switch (location) {
case TOP_LEFT:
watermarkX = 0;
watermarkY = 0;
break;
case TOP_RIGHT:
watermarkX = inputImage.getWidth() - watermarkWidth - 0;
watermarkY = 0;
break;
case BOTTOM_LEFT:
watermarkX = 0;
watermarkY = inputImage.getHeight() - watermarkHeight - 0;
break;
case BOTTOM_RIGHT:
watermarkX = inputImage.getWidth() - watermarkWidth - 0;
watermarkY = inputImage.getHeight() - watermarkHeight - 0;
break;
case CENTER:
watermarkX = (inputImage.getWidth() - watermarkWidth) / 2;
watermarkY = (inputImage.getHeight() - watermarkHeight) / 2;
break;
}
// 添加水印图片到输出图像
//drawImage(Image img, int x, int y,int width, int height,ImageObserver observer)
graphics.drawImage(watermarkImage, watermarkX, watermarkY,watermarkWidth, watermarkHeight,null);
// 生成输出文件的名称
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String timestamp = dateFormat.format(new Date());
String outputFileName = "watermarked_" + timestamp + ".jpg";
// 保存输出图像到指定文件夹
String outputFilePath = outputDirectory + File.separator + outputFileName;
File outputFile = new File(outputFilePath);
ImageIO.write(outputImage, "jpg", outputFile);
// 释放资源
graphics.dispose();
System.out.println("水印已成功添加到图片:" + outputFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行代码: