水印要求:左上角为文字水印,左下角为Logo图片水印,右下角为图片水印(如百度定位静态图)。
一、引入Jar依赖
compile 'net.coobird:thumbnailator:0.4.8'
net.coobird
thumbnailator
0.4.8
二、代码实现
public File handWaterMark(MultipartFile file, String uploadPath, Integer type, String address, String dealerName,
String latitudeCommaLongitude, String date) throws IOException {
// 原文件名
String originalFilename = file.getOriginalFilename();
// 原文件扩展名
String originalFilenameExtension = org.springframework.util.StringUtils.getFilenameExtension(originalFilename);
// 百度定位静态图文件名
String baiduStaticImgFileName = UUID.randomUUID() + "." + "png";
// 新文件名
String newFileName = UUID.randomUUID() + "." + originalFilenameExtension;
// ===================== 水印处理 =====================
File srcImgFile = new File(uploadPath, Objects.requireNonNull(originalFilename));
Thumbnails.Builder builder = Thumbnails.of(srcImgFile);
// 1 左上角 库存点报备信息文字水印的原数据准备
BufferedImage image = handleTextWaterMark(address, dealerName, latitudeCommaLongitude, date);
// 2 左下角 logo图片水印
handleLogoImageWaterMark(uploadPath, builder);
// 3 右下角 百度定位静态图水印
File baiduStaticImage = handleBaiduStaticImageWaterMark(uploadPath, latitudeCommaLongitude, baiduStaticImgFileName, builder);
// 最终成品图片
FileOutputStream outputStream = new FileOutputStream(srcImgFile);
outputStream.write(file.getBytes());
outputStream.close();
// 添加库存点报备信息文字水印
builder.watermark(Positions.TOP_LEFT, image, 0.8f)
.toFile(uploadPath + file.getOriginalFilename());
// 重命名
File newFile = new File(uploadPath, newFileName);
boolean b = srcImgFile.renameTo(newFile);
// 删除文件 => 百度定位静态图
boolean delete = baiduStaticImage.delete();
return new File(newFileName);
}
/**
* @return java.awt.image.BufferedImage
* @throws
* @description 操纵文字型水印
* @params [address, dealerName, latitudeCommaLongitude, date]
*/
private BufferedImage handleTextWaterMark(String address, String dealerName, String latitudeCommaLongitude, String date) {
final Font font = getSimsunFont();
BufferedImage image = new BufferedImage(800, 320, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
image = g.getDeviceConfiguration().createCompatibleImage(800, 320, Transparency.TRANSLUCENT);
int y = 0;
int divider30 = 30;
g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.red);
g.setFont(font);
if (StringUtils.isNotBlank(address)) {
g.drawString("库存点地址:" + address, 5, y += divider30);
}
if (StringUtils.isNotBlank(dealerName)) {
g.drawString("经销商名称:" + dealerName, 5, y += divider30);
}
if (StringUtils.isNotBlank(latitudeCommaLongitude)) {
g.drawString("经纬度:" + latitudeCommaLongitude, 5, y += divider30);
}
if (StringUtils.isNotBlank(date)) {
g.drawString("拍照时间:" + date, 5, y += divider30);
}
g.dispose();
return image;
}
/**
* @return java.io.File
* @throws
* @description 操纵百度定位静态图水印
* @params [uploadPath, latitudeCommaLongitude, newFileName, builder]
*/
private File handleBaiduStaticImageWaterMark(String uploadPath, String latitudeCommaLongitude,
String newFileName, Thumbnails.Builder builder)
throws IOException {
String mapUrl = baiduMapUrl + "?ak=" + baiduMapAppKey + "¢er=" +
latitudeCommaLongitude + "&width=300&height=200&zoom=18";
File baiduStaticImage = new File(uploadPath + newFileName);
FileUtils.copyURLToFile(new URL(mapUrl), baiduStaticImage);
builder.watermark(Positions.BOTTOM_RIGHT,
ImageIO.read(baiduStaticImage), 1);
return baiduStaticImage;
}
/**
* @return void
* @throws
* @description 操纵logo型图片水印
* @params [uploadPath, builder]
*/
private void handleLogoImageWaterMark(String uploadPath, Thumbnails.Builder builder) throws IOException {
// 添加水印 参数分别为(水印位置,水印图片的路径,透明度)
File logo = new File(URLDecoder.decode(uploadPath, "utf-8"), "logo.png");
builder.scale(1).watermark(Positions.BOTTOM_LEFT, ImageIO.read(logo), 0.5f);
}