/**
* 读取正方形 log图片(200x200)
* @param img
* @param width log图片的放大倍数
* @return
* @throws IOException
*/
public BufferedImage resizeImage(String img, int width) throws IOException {
width *= 200;
BufferedImage image = ImageIO.read(new File(img));
Image resultingImage = image.getScaledInstance(width , width , Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(width , width , BufferedImage.TYPE_INT_ARGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
/**
* @param imagePath 原始图片路径
* @param watermarkPath 水印图片路径
* @param outputPath 输出图片路径
* @param list log图片的参数
* @throws IOException
*/
public void addWatermark(String imagePath, String watermarkPath, String outputPath, List<CodePositionVo> list) throws IOException{
BufferedImage image = ImageIO.read(new File(imagePath));
BufferedImage watermark = null;
// 图片打码
for (CodePositionVo item : list){
watermark = resizeImage(watermarkPath,item.getWidth());
Graphics2D graphics2D = image.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(watermark, item.getX(), item.getY(), null);
graphics2D.dispose();
}
ImageIO.write(image, "jpg", new File(outputPath));
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CodePositionVo {
// 图片放大的倍速
int width;
// x、y 坐标原点(0,0) 是图片左上角
int x;
int y;
}