我们在java开发时,使用Thumbnails第三方jar能帮助我们对图片进行很好的处理。

开发步骤

1、导入相关jar包

<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version>
</dependency>
compile("net.coobird:thumbnailator:0.4.8")

2、使用方法

/*** 指定大小进行缩放* * @throws IOException*/private void test1() throws IOException {/** size(width,height) 若图片横比200小,高比300小,不变* 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300*/Thumbnails.of("images/test.jpg").size(200, 300).toFile("C:/image_200x300.jpg");Thumbnails.of("images/test.jpg").size(2560, 2048).toFile("C:/image_2560x2048.jpg");}/*** 按照比例进行缩放* * @throws IOException*/private void test2() throws IOException {/*** scale(比例)*/Thumbnails.of("images/test.jpg").scale(0.25f).toFile("C:/image_25%.jpg");Thumbnails.of("images/test.jpg").scale(1.10f).toFile("C:/image_110%.jpg");}/*** 不按照比例,指定大小进行缩放* * @throws IOException*/private void test3() throws IOException {/*** keepAspectRatio(false) 默认是按照比例缩放的*/Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg");}/*** 旋转* * @throws IOException*/private void test4() throws IOException {/*** rotate(角度),正数:顺时针 负数:逆时针*/Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile("C:/image+90.jpg");Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile("C:/iamge-90.jpg");}/*** 水印* * @throws IOException*/private void test5() throws IOException {/*** watermark(位置,水印图,透明度)*/Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f).outputQuality(0.8f).toFile("C:/image_watermark_bottom_right.jpg");Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f).outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");}/*** 裁剪* * @throws IOException*/private void test6() throws IOException {/*** 图片中心400*400的区域*/Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_center.jpg");/*** 图片右下400*400的区域*/Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_bootom_right.jpg");/*** 指定坐标*/Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");}/*** 转化图像格式* * @throws IOException*/private void test7() throws IOException {/*** outputFormat(图像格式)*/Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png").toFile("C:/image_1280x1024.png");Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif").toFile("C:/image_1280x1024.gif");}/*** 输出到OutputStream* * @throws IOException*/private void test8() throws IOException {/*** toOutputStream(流对象)*/OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);}/*** 输出到BufferedImage* * @throws IOException*/private void test9() throws IOException {/*** asBufferedImage() 返回BufferedImage*/BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));}

3、开发实例

public List<Map<String, String>> selectVoucherImages(String voucherImageId) {logger.info("查询图片详细信息请求参数" + voucherImageId);List<Map<String, String>> list = new ArrayList<>();AvisVoucherImage avisVoucherImage = avisVoucherImageDao.selectByPrimaryKey(voucherImageId);String filePackPath = imagesRootPath + "/" + avisVoucherImage.getDeptId() + "/" + avisVoucherImage.getYear() + "/" +avisVoucherImage.getMonth() + "/" + avisVoucherImage.getDay();List<AvisImage> imageList = avisImageDao.qryImages(voucherImageId);String userName = avisVoucherImage.getUserName();for (AvisImage image : imageList) {try {String filePath = filePackPath + "/" + image.getFilename();//定义新的压缩图片String thumbnailsFile = filePackPath + "/" + image.getFilename().replace(".", "_Thumbnails.");File tmpFile = new File(thumbnailsFile);if (!tmpFile.exists()) {/*  其中的scale是可以指定图片的大小,值在0到1之间,1f就是原图大小,0.5就是原图的一半大小,这里的大小是指图片的长宽。而outputQuality是图片的质量,值也是在0到1,越接近于1质量越好,越接近于0质量越差。size指定转换后的图片长宽确定值*///原图压缩赋值给新定义的图片Thumbnails.of(filePath).scale(0.6f).toFile(thumbnailsFile);/*Thumbnails.of(filePath).scale(0.5f).outputQuality(0.1).toFile(thumbnailsFile);*/}Map<String, String> imageMap = new HashedMap();imageMap.put("filename", filePath);imageMap.put("fileinfo", new Base64Encoder().encode(Files.readAllBytes(Paths.get(thumbnailsFile))));imageMap.put("username", userName);list.add(imageMap);} catch (IOException e) {logger.error("获取图片内容出现异常", e);return null;}}return list;}

4、博客参考

https://blog.csdn.net/qq_25508039/article/details/82257436

https://blog.csdn.net/qq_20974635/article/details/77503478

java Thumbnails处理图片相关推荐

  1. Thumbnails 处理图片

    博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导): Thumbnails 处理图片 Java图片处理开源框架 java使用google开源工具实现图片压缩 Thumbnai ...

  2. java thumbnails 中心点_java Thumbnails 图片处理的使用

    在后端开发的过程中,都逃不开与文件传输特别是图片的传输打交道,但是因为现在各种拍照设备发展越来越快,拍出的照片更是越来越清晰,但是照片文件的大小也是越来越大了,手机拍照小则2M大则30M这在网络传输过 ...

  3. java thumbnails 中心点_JavaDemo——java使用Thumbnails处理图片

    thumbnailator是谷歌处理图片的开源工具类,能够对图片进行缩放,旋转,压缩,剪裁,添加水印,转换格式等:html maven引入:git net.coobird thumbnailator ...

  4. java异常处理图片_Java处理图片时出现异常

    Java处理图片时出现异常 javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49 at com.sun.imageio. ...

  5. java thumbnails 内存_Java修改图片尺寸,总是报内存溢出怎么解决?

    项目需求:有很多尺寸很大的图片(图片大小可能几十MB,甚至上百MB),需要等比例缩小它们的尺寸,相当于生成缩略图. 例如:原图8268x6201,需要压缩成400x300. 我现在的方法:将图片整个读 ...

  6. thumbnails java_Java后端使用Thumbnails处理图片格式-Fun言

    在日常开发中,经常会使用到图片上传,但是过大的图片在查看的时候会影响打开速度,浪费流量,所以需要在后端处理完图片再上传,这里我们用到了Thumbnails图片处理工具类. Thumbnails支持: ...

  7. java thumbnails 内存_Java图片处理 Thumbnails框架

    一.设置图片的缩放比例或者图片的质量比 第一步:导入maven的jar包 net.coobird thumbnailator 0.4.8 第二步:获取一个Builder对象 public static ...

  8. Java高斯模糊处理图片

    1.获取jhlabs的图片滤镜 方式一: 直接下载jar包 下载地址:http://www.jhlabs.com/ip/filters/download.html,点击"Download F ...

  9. 如何使用Java快速地给图片转码和生成缩略图(Thumbnailator和webp-imageio-core的使用)

    文章简介 本文中介绍,如何使用Java优雅处理图片:包括:主流图片格式转码.图片压缩(缩略图生成)等.主要用到的外部工具包: Google Thumbnailator webp-imageio-cor ...

最新文章

  1. java 线程池学习小记
  2. python程序加密license_怎么解决pycharm license Acti的方法
  3. Unity中制作游戏的快照游戏支持玩家拍快照
  4. 金蝶清空日志数据库脚本
  5. Java的知识点14——内部类的概念、内部类的分类
  6. Couldn't find leader offsets for Set([smt,0], [smt,1], [smt,2])
  7. mysql提示错误[Error Code] 1290 - The MySQL server is running with the --secure-file-priv option解决办法...
  8. MariaDB 10的复制 集群 高可用搭建 大表拆分【持续更新中】
  9. dialog element 删掉标题_ElementUI 销毁Dialog数据(简单粗暴)
  10. Gnuplot 简单使用
  11. 在Mac上唤出「快速备忘录」的开启与关闭设置教程
  12. python装饰品 后端_python装饰器
  13. 什么是SQL Server数据库镜像?
  14. 常微分方程——解的延拓性定理
  15. Linux 基础之基础网络ss命令
  16. unix编程艺术读书笔记
  17. 院士如何应对互联网的碎片化和复杂性?道翰天琼认知智能机器人平台API接口大脑为您揭秘。
  18. 在Java中打印金字塔图案
  19. 日常pytho3练习脚本之--彩票自动选号机
  20. 如何快速学会三子棋游戏

热门文章

  1. Java基于 ssm+jsp的大学生就业求职招聘简历投递平台
  2. 信息学奥赛一本通 1327:【例7.6】黑白棋子的移动 | 洛谷 P1259 黑白棋子的移动
  3. Log4net 乱码问题解决
  4. Java如何使用dom4j获取,添加,删除,查找,设置Element节点呢?
  5. IM即时通讯框架设计(1)
  6. java计算机毕业设计智慧机场管理系统源程序+mysql+系统+lw文档+远程调试
  7. 透过皮亚诺公理看自然数
  8. CS231n 计算机视觉(学习笔记)第七章(0809)
  9. 尚硅谷——谷粒商城项目开发记录——2021.11.19
  10. OMI卫星数据介绍[整理]