Hutool操作图片(缩放,加水印)

您所在的位置:网站首页 全黑图片无水印 Hutool操作图片(缩放,加水印)

Hutool操作图片(缩放,加水印)

2024-07-11 19:38| 来源: 网络整理| 查看: 265

一、地址

https://www.hutool.cn/

针对awt中图片处理进行封装,这些封装包括:缩放、裁剪、转为黑白、加水印等操作。

二、缩放图片

近期线上环境CDN过高,排查下来发现图片有点大,在对于图片分辨率不是特别高的场景,显示效果影响不是很大的情况下,可以对返回的图片进行适当缩放,缩放图片不仅减少图片文件大小,也可以减少CDN带宽,

现在提供一张图片的访问路径,需要把这张图片进行缩放,

比如这里有一张图片

https://images.cnblogs.com/cnblogs_com/LiuFqiang/1429011/o_220528033315_star-squashed.jpg

图片大小为1920 x 1080格式的,

if (FileNameUtil.containsInvalid(picUrl)) { BufferedImage bufferedImage = ImgUtil.read(URLUtil.url(picUrl));Image scaleImage = ImgUtil.scale(bufferedImage, 0.5f);File file = FileUtil.file("/Users/liufuqiang/a.jpg");ImgUtil.write(scaleImage, file); }

 

 

 ImgUtil.scale()为缩放图片方法,里面重载了几个参数,这里使用的为Image原图像来源流,scale为缩放比例

 

 三、图片添加水印

1、添加文字水印

原图

 

 添加文字水印图片

 

String picUrl = "https://images.cnblogs.com/cnblogs_com/LiuFqiang/1429011/o_220528033315_star-squashed.jpg"; BufferedImage bufferedImage = ImgUtil.read(URLUtil.url(picUrl)); ImgUtil.pressText( bufferedImage, FileUtil.file("/Users/liufuqiang/b.jpg"), "博客园-LiuFqiang", Color.WHITE, new Font("微软雅黑", Font.BOLD, 40), 0 , 0, 0.8f );

  

 

 可以设置文字水印的字体颜色等,默认添加水印的坐标为图片正中央

2、图片文字水印

 显示在右下方,正常水印会跟随图片大小而调整

public static BufferedImage pressText (BufferedImage srcImage, Image pressImg, String pressText, Color color, Font font, float alpha) { final BufferedImage destImage = srcImage; final Graphics2D gf = destImage.createGraphics(); // 抗锯齿 gf.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gf.setColor(color); gf.setFont(font); // 透明度 gf.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 在指定坐标绘制水印文字 FontMetrics metrics = gf.getFontMetrics(font); int textLength = metrics.stringWidth(pressText); int textHeight = metrics.getAscent() - metrics.getLeading() - metrics.getDescent(); gf.drawString(pressText, destImage.getWidth() - textLength, destImage.getHeight() - (textHeight / 2)); gf.dispose(); int width = destImage.getWidth((ImageObserver) null); int height = destImage.getHeight((ImageObserver) null); BufferedImage destImage2 = new BufferedImage(width, height, 1); Graphics2D g = destImage2.createGraphics(); g.drawImage(destImage, 0, 0, width, height, (ImageObserver) null); int pressImgWidth = pressImg.getWidth((ImageObserver) null); int pressImgHeight = pressImg.getHeight((ImageObserver) null); int x = (width - pressImgWidth) - textLength - 5; int y = (height - pressImgHeight); g.setComposite(AlphaComposite.getInstance(10, alpha)); // 给图片打上图片水印 g.drawImage(pressImg, x, y, pressImgWidth, pressImgHeight, (ImageObserver) null); g.dispose(); return destImage2; }

  方法调用  

BufferedImage bufferedImage1 = pressText(ImgUtil.read(URLUtil.url(picUrl)), ImgUtil.read(URLUtil.url("https://www.baidu.com/img/flexible/logo/pc/[email protected]")), "博客园-LiuFqiang", Color.WHITE, new Font("微软雅黑", Font.BOLD, 40), 1f); ImgUtil.write(bufferedImage1, FileUtil.file("/Users/liufuqiang/b.jpg"));

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3