图片增加水印
工具类
package com.ruoyi.bnbackstage.util;
import com.ruoyi.bnbackstage.domain.ShuiYinClass;
import org.springframework.stereotype.Service;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import javax.imageio.ImageIO;
@Service
public class MarkImageUtils {
public boolean waterMark(String filePath, String outFile,
String text, Color color, Font font, int x, int y) {
String result = "打水印失败!";
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
BufferedImage im = ImageIO.read(fis);
int width = im.getWidth();
int height = im.getHeight();
if (x > width || y > height) {
System.out.println(result);
return false;
}
Graphics g = im.getGraphics();
g.setColor(color);
g.setFont(font);
g.drawString(text, x, y);
g.dispose();
ImageIO.write(im, "png", new File(outFile));
} catch (IOException e) {
System.out.println(result);
return false;
} finally {
closeFileInputStream(fis);
}
result = "打水印成功!";
System.out.println(result);
return true;
}
public boolean waterMarkMany(String filePath, String outFile,
List<ShuiYinClass> suiyinList, Color color, Font font, int x, int y) {
String result = "打水印失败!";
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
BufferedImage im = ImageIO.read(fis);
int width = im.getWidth();
int height = im.getHeight();
if (x > width || y > height) {
System.out.println(result);
return false;
}
Graphics g = im.getGraphics();
g.setColor(color);
g.setFont(font);
for (int i = 0; i <suiyinList.size() ; i++) {
y += 35;
g.drawString(suiyinList.get(i).getShuiYinText(),x, y);
}
g.dispose();
if (filePath.contains(".jpg")){
ImageIO.write(im, "jpg", new File(outFile));
}else {
ImageIO.write(im, "png", new File(outFile));
}
} catch (IOException e) {
System.out.println(result);
return false;
} finally {
closeFileInputStream(fis);
}
result = "打水印成功!";
System.out.println(result);
return true;
}
public void closeFileInputStream (FileInputStream fis){
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Map getPhotoSize(String filePath) throws IOException {
Map map = new HashMap();
File picture = new File(filePath);
BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));
map.put("width", sourceImg.getWidth());
map.put("height", sourceImg.getHeight());
return map;
}
}
调用方式
String timePath = timeUtil.getTimeYMDone();
String fileName = file.getOriginalFilename();
String savePathOld = "D:/downloadZip" + "/" + "one" + timePath + fileName;
String savePathNew = "D:/downloadZip" + "/" + "two" + timePath + fileName;
File dest = new File(savePathOld);
file.transferTo(dest);
List<ShuiYinClass> list = new ArrayList<>();
Map fileSizeMap = markImageUtils.getPhotoSize(savePathOld);
System.out.println("图片宽度:" +fileSizeMap.get("width"));
System.out.println("图片高度:"+fileSizeMap.get("height"));
Integer width = Integer.valueOf(fileSizeMap.get("width").toString());
Integer height = Integer.valueOf(fileSizeMap.get("height").toString());
int startWidth = width - 330;
int statrHeight = height - 250;
Color color=new Color( 255,0,0);
Font font = new Font("微软雅黑", Font.BOLD, 20);
boolean addTestResult = markImageUtils.waterMarkMany(savePathOld, savePathNew, list, color, font, startWidth, statrHeight);
if (addTestResult == true){
File uploadOssFile = new File(savePathNew);
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(uploadOssFile);
MultipartFile multipartFile = new MockMultipartFile(uploadOssFile.getName(), uploadOssFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
}