本文主要实现基于java使用Graphics2D进行图片叠加覆盖,话不多说直接上代码
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class NewImageUtils {
/**
* @param file 源文件(图片)
* @param waterFile 水印文件(图片)
* @param x 距离右下角的X偏移量
* @param y 距离右下角的Y偏移量
* @param alpha 透明度, 选择值从0.0~1.0: 完全透明~完全不透明
* @return BufferedImage
* @throws IOException
* @Title: 构造图片
* @Description: 生成水印并返回java.awt.image.BufferedImage
*/
private static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException { //本地图片
// private static BufferedImage watermark(URL file, URL waterFile, int x, int y, float alpha) throws IOException { //线上网络图片
// 获取底图
BufferedImage buffImg = ImageIO.read(file);
// 获取层图
BufferedImage waterImg = ImageIO.read(waterFile);
// 创建Graphics2D对象,用在底图对象上绘图
Graphics2D g2d = buffImg.createGraphics();
// 获取层图的宽度
int waterImgWidth = buffImg.getWidth();
// 获取层图的高度
int waterImgHeight = buffImg.getHeight();
// 在图形和图像中实现混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 绘制
g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
// 释放图形上下文使用的系统资源
g2d.dispose();
return buffImg;
}
/**
* 输出水印图片
*
* @param buffImg 图像加水印之后的BufferedImage对象
* @param savePath 图像加水印之后的保存路径
*/
private static void generateWaterFile(BufferedImage buffImg, String savePath) {
int temp = savePath.lastIndexOf(".") + 1;
try {
ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* 测试本地
*
* @param args
*/
public static void main(String[] args) {
// 这里测试单次的,但是实际引用大多都是批量处理
// //底层图片地址
// String sourceFilePath = "xxx";
// //上层图片地址
// String waterFilePath = "xxx";
// //合成后的图片地址
// String saveFilePath = "xxx";
//
// imageSynthesis(sourceFilePath, waterFilePath, saveFilePath);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//底层图片地址
String sourceFilePath = "xxx";
//合成后的图片地址
String saveFilePath = "xxx";
//上层图片地址
List<String> waterFilePathList = new ArrayList<>();
waterFilePathList.add("xxx");
waterFilePathList.add("xxx");
waterFilePathList.add("xxx");
waterFilePathList.add("xxx");
waterFilePathList.add("xxx");
//这里要先合成一次,将底图变成后续合成后的地址
imageSynthesis(sourceFilePath, waterFilePathList.get(0), saveFilePath);
for (int i = 1; i < waterFilePathList.size(); i++) {
imageSynthesis(saveFilePath, waterFilePathList.get(i), saveFilePath);
}
}
/**
* 图片合成
*
* @param sourceFilePath 底层图片地址
* @param waterFilePath 上层图片地址
* @param saveFilePath 合成后的图片地址
*/
public static void imageSynthesis(String sourceFilePath, String waterFilePath, String saveFilePath) {
try {
// 构建叠加层(可以自己设置透明度哦)
BufferedImage buffImg = watermark(new File(sourceFilePath), new File(waterFilePath), 0, 0, 1.0f);//本地图片
// BufferedImage buffImg = watermark(new URL(sourceFilePath), new URL(waterFilePath), 0, 0, 0.5f);//线上网络图片
// 输出水印图片
generateWaterFile(buffImg, saveFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}