关于给图片添加水印
笔记
第一步 初始化
BufferedImage是Image的实现类,是一个带缓冲区图像类,主要作用是将一幅图片加载到内存中,提供获得绘图对象、图像缩放、选择图像平滑度等功能,通常用来做图片大小变换、图片变灰、设置透明不透明等。
/**
* 初始化
*
* @param resourceImgPath
* @param degree
* @return
* @throws IOException
*/
private static WaterMarkDto initGraphic(String resourceImgPath, Integer degree) throws IOException {
Image srcImg = ImageIO.read(new File(resourceImgPath));
// 创建图片对象
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 基于图片对象打开绘图
Graphics2D g = buffImg.createGraphics();
// 消除画图锯齿
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
// 旋转
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg
.getHeight() / 2);
}
return new WaterMarkDto().setGraphics(g).setResourceImage(srcImg).setBufferedImage(buffImg);
}
@Data
@Accessors(chain = true)
public class WaterMarkDto {
private Graphics2D graphics;
private Image resourceImage;
private BufferedImage bufferedImage;
}
第二步 实现
/**
* 给图片添加水印、可设置水印图片旋转角度
*
* @param iconPath 水印图片路径
* @param resourceImgPath 源图片路径
* @param targetPath 目标图片路径
* @param degree 水印图片旋转角度
*/
public static void markImageByIcon(String iconPath, String resourceImgPath, String targetPath, Integer degree) {
OutputStream os = null;
try {
if (StringUtils.isBlank(targetPath)) {
targetPath = resourceImgPath.substring(0, resourceImgPath.lastIndexOf("."))
+ FinalValue.WATER_MARK_IMG_POSTFIX + resourceImgPath.substring(resourceImgPath.lastIndexOf("."));
}
WaterMarkDto waterMarkDto = initGraphic(resourceImgPath, degree);
Graphics2D graphics = waterMarkDto.getGraphics();
ImageIcon imgIcon = new ImageIcon(iconPath);
Image img = imgIcon.getImage();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, FinalValue.WATER_MARK_TRANSPARENT));
int width = waterMarkDto.getResourceImage().getWidth(null);
int height = waterMarkDto.getResourceImage().getHeight(null);
int iconWidth = img.getWidth(null);
int iconHeight = img.getHeight(null);
int x = (width - iconWidth)/2;
int y = (height - iconHeight)/2;
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
// 表示水印图片的位置
graphics.drawImage(img, x, y, null);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
graphics.dispose();
os = new FileOutputStream(targetPath);
ImageIO.write(waterMarkDto.getBufferedImage(), "JPG", os);
} catch (Exception e) {
log.error("生成图片水印失败:{}", e);
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
log.error("关闭图片水印流失败:{}", e);
}
}
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText
* @param resourceImgPath
* @param targerPath
* @param degree
*/
public static void markImageByText(String logoText, String resourceImgPath, String targerPath, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
if (StringUtils.isBlank(targerPath)) {
targerPath = resourceImgPath.substring(0, resourceImgPath.lastIndexOf(".")) + FinalValue.WATER_MARK_IMG_POSTFIX + "f" + resourceImgPath.substring(resourceImgPath.lastIndexOf("."));
}
WaterMarkDto waterMarkDto = initGraphic(resourceImgPath, degree);
Graphics2D graphics = waterMarkDto.getGraphics();
graphics.setColor(Color.BLACK);
graphics.setFont(FinalValue.WATER_MARK_FONT);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, FinalValue.WATER_MARK_TRANSPARENT));
graphics.drawString(logoText, FinalValue.WATER_MARK_POSITION_WIDTH, FinalValue.WATER_MARK_POSITION_HEIGHT);
graphics.dispose();
os = new FileOutputStream(targerPath);
ImageIO.write(waterMarkDto.getBufferedImage(), "JPG", os);
} catch (Exception e) {
log.error("生成文字水印失败:{}", e);
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
log.error("关闭写入流失败:{}", e);
}
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
log.error("关闭输出流失败:{}", e);
}
}
}