Thumbnails使用方法(java图片处理工具类) 转载 2021-03-09 14:10:05.0 阅读(1611)次 本文链接:https://blog.csdn.net/qq_25508039/article/details/82257436 #### 介绍 我们在java开发时,使用Thumbnails工具类能帮助我们对图片进行很好的处理,Thumbnails对图片的操作进行了很好的封装,往往很复杂的步骤能用一行代码就完成。 Thumbnails支持: - 1. 指定大小进行缩放 - 2. 按照比例进行缩放 - 3. 不按照比例,指定大小进行缩放 - 4. 旋转 - 5. 水印 - 6. 裁剪 - 7. 转化图像格式 - 8. 输出到OutputStream - 9. 输出到BufferedImage #### 使用步骤 导入jar包 ```xml net.coobird thumbnailator 0.4.8 ``` 使用方法 ```java /** * 指定大小进行缩放 * * @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")); } ``` java 上一篇:java中BufferedImage转成 base64字符串 下一篇:maven修改jar包版本不生效解决办法
相关文章 切分List集合为多个List集合(7505) fastjson转字符串时保留null空字段(2558) 线程安全测试 ArrayList Collections.synchronizedList CopyOnWriteArrayList(1510) jmeter随机日期参数(1125) idea类找不到问题Caused by: java.lang.NoClassDefFoundError(4055) Base64编码出现换行符(2601) java使用snakeyaml库读取和操作yml文件(2493) QLExpress在脚本中执行Java代码(2523) QLExpress if判断以及多个if else如何写(2592) java表达式运算性能比较:Jep与QLExpress(2470) 推荐文章 使用spring4实现websocket连接(1) Parameter index out of range (1 > number of parameters, which is 0(7) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) jquery对象与dom对象互转(1) linux使用epel源yum安装iftop、nload、nginx等(2) linux下nginx安装其他模块(1) 热门文章 java stream去重的几种方式(40819) the dependencies of some of the beans in the application context form a cycle(16324) 解决mybatis打印查询结果集造成太多日志的问题(9839) java enum枚举转list和Map(9722) java stream List转Map与List转List与Map转List以及List转Map(8468) ServletRequest转HttpServletRequest设置header之后取不到header的问题(8434) java中BufferedImage转成 base64字符串(8098) 切分List集合为多个List集合(7385) maven修改jar包版本不生效解决办法(6990) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (6949) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab