Java三方---->Thumbnailator框架的使用
Thumbnailator是一个用来生成图像缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。有了它我们就不用在费心思使用Image I/O API,Java 2D API等等来生成缩略图了,它支持图片缩放,区域裁剪,水印,旋转,保持比例等等。今天,我们就开始Thumbnailator的学习。
Thumbnailator项目的引入
一、 Thumbnailator的下载地址:https://github.com/coobird/thumbnailator。下载完成之后进入src/main/java/。将net文件夹复制到自己新建的项目的src下。
二、 选中复制的部分,右键选择expot ---> JAR file。在JAR Export中选择jar的输出位置。
三、 将jar包导入到项目中,删除上述的thumbnailator源文件。
Thumbnailator的简单使用
项目中引用的huhx.jpg如下:
一、 生成一张固定大小的huhx1.jpg
@Test public void thumbnailator1() { try { Thumbnails.of("image/huhx.jpg").size(80, 80).toFile("photo/huhx1.jpg"); } catch (IOException e) { e.printStackTrace(); } }
生成的huhx1.jpg如下:
二、 文件夹所有图片都生成缩略图
@Test public void thumbnailator2() { try { Thumbnails.of(new File("image").listFiles()).size(640, 480).outputFormat("jpg").toFiles(Rename.PREFIX_DOT_THUMBNAIL); } catch (IOException e) { e.printStackTrace(); } }
生成的thumbnail.huhx.jpg如下:
三、 旋转角度的缩略图
@Test public void thumbnailator3() { try { Thumbnails.of(new File("image/huhx.jpg")).scale(0.25).rotate(90).outputFormat("jpg").toFile("photo/huhx2.jpg"); } catch (IOException e) { e.printStackTrace(); } }
生成的huhx2.jpg如下:
四、 生成一个带有旋转和水印的缩略图
@Test public void thumbnailator4() { try { Thumbnails.of(new File("image/huhx.jpg")).size(160, 160).rotate(90) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("image/water.jpg")), 0.5f) .outputQuality(0.8f).toFile(new File("photo/huhx3.jpg")); } catch (IOException e) { e.printStackTrace(); } }
生成的huhx3.jpg如下:
五、 把生成的图片输出到输出流
@Test public void thumbnailator5() { try { OutputStream os = new FileOutputStream("photo/huhx4.jpg"); Thumbnails.of("image/huhx.jpg").size(200, 200).outputFormat("jpg").toOutputStream(os); } catch (IOException e) { e.printStackTrace(); } }
生成的huhx4.jpg:
六、 按一定的比例生成缩略图,生成缩略图的大小是原来的25%
@Test public void thumbnailator6() { try { BufferedImage originalImage = ImageIO.read(new File("image/huhx.jpg")); BufferedImage thumbnail = Thumbnails.of(originalImage).scale(0.25f).asBufferedImage(); ImageIO.write(thumbnail, "JPG", new File("photo/huhx5.jpg")); } catch (IOException e) { e.printStackTrace(); } }
七、 对图片进行裁剪
@Test public void thumbnailator7() { try { /** * 中心400*400的区域 */ Thumbnails.of("image/huhx.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxCenter.jpg"); /** * 右下400*400的区域 */ Thumbnails.of("image/huhx.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxRight.jpg"); /** * 指定坐标(0, 0)和(400, 400)区域,再缩放为200*200 */ Thumbnails.of("image/huhx.jpg").sourceRegion(0, 0, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxRegion.jpg"); } catch (IOException e) { e.printStackTrace(); } }
生成的huhxCenter.jpg, huhxRight.jpg, huhxRegion.jpg分别如下:
友情链接
作者:
huhx
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库