大家好,又见面了,我是你们的朋友全栈君。
还有一篇文章添加多种水印(文字水印(带背景颜色) +图片水印)大家可以看一下、
https://blog.csdn.net/whiteGu/article/details/97653571
/**
* @param srcPath 需要添加水印的完整地址
* @param ids 需要添加的水印的id集合 ,结果以 ","分隔
* @return 返回包含水印图片的输入流
* @throws Exception
*/
public InputStream addWatermark(String srcPath, String ids) throws Exception {
File waterFile1 = null;
File waterFile2 = null;
Image watermarkImage1 = null;
Image watermarkImage2 = null;
String fileSuffix = srcPath.substring(srcPath.lastIndexOf(".") + 1);
if (StringUtils.isBlank(ids)) throw new BaseAppException("必传水印id");
List<SysWatermark> sysWatermarks = Arrays.stream(ids.split(",")).map(x -> sysWatermarkService.getByPrimaryKey(x)
).collect(Collectors.toList());
if (CollectionUtils.isEmpty(sysWatermarks)) throw new BaseAppException("没有对应的水印模板");
//获取图片路径
String waterImagePath1 = imgPrefix + sysWatermarks.get(0).getWatermarkUrl();
File srcFile = new File(srcPath);
waterFile1 = new File(waterImagePath1);
if (sysWatermarks.size() > 1) {
String waterImagePath2 = imgPrefix + sysWatermarks.get(1).getWatermarkUrl();
waterFile2 = new File(waterImagePath2);
}
//读取原图片
try {
Image srcImage = ImageIO.read(srcFile);
int height = srcImage.getHeight(null);
int width = srcImage.getWidth(null);
if (width > 1000 || height > 1000) {
throw new BaseAppException("图片大小最大不能大于1000*1000");
}
//准备画板
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//创建画笔
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawImage(srcImage, width, height, null);
//读取水印
Image temp1 = ImageIO.read(waterFile1);
ImageIcon imageIcon1 = new ImageIcon(temp1);
watermarkImage1 = imageIcon1.getImage();
//读取第二张水印
if (null != waterFile2) {
Image temp2 = ImageIO.read(waterFile2);
ImageIcon imageIcon2 = new ImageIcon(temp2);
watermarkImage2 = imageIcon2.getImage();
}
//第一张水印图片的透明度
watermarkCoordinate(graphics, sysWatermarks.get(0), watermarkImage1, width, height);
//处理第二张水印(如果是多水印可以采用循环方式,由于我这里只会有2张水印所以写死了)
if (null != watermarkImage2) {
watermarkCoordinate(graphics, sysWatermarks.get(1), watermarkImage2, width, height);
}
graphics.dispose();
//本地测试
OutputStream outputStream = new FileOutputStream("D:/test.jpg");
ImageIO.write(bufferedImage, "JPG", outputStream);
//本地测试结束(如果是本地测试下面不需要)
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, fileSuffix, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
e.printStackTrace();
throw new BaseAppException("读取图片失败");
}
}
/**
* 添加水印管理
*
* @param graphics 画笔
* @param sysWatermark 水印
* @param image 水印图片
* @param width 需要添加水印图的宽度
* @param height 需要添加水印图的高度
*/
private void watermarkCoordinate(Graphics2D graphics, SysWatermark sysWatermark, Image image, int width, int height) {
float transparency = sysWatermark.getWatermarkTransparency() / 100;
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, transparency));
//放上水印图
String coordinate = sysWatermark.getWatermarkCoordinate();
JSONObject parseObject = JSONObject.parseObject(coordinate);
int watermarkWidth = (int) parseObject.get("width");
int watermarkHeight = (int) parseObject.get("height");
graphics.drawImage(image, width * watermarkWidth / 100, height * watermarkHeight / 100, null);
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/129084.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...