Thumbnailator 是一个为Java界面更流畅的缩略图生成库。从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。

http://code.google.com/p/thumbnailator/

版本:thumbnailator-0.4.8.jar

package com.zspr.utils.img;import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;/*** 图片工具* @author jichao**/
public class Pic {/*** 指定大小进行缩放* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param width 宽* @param height 高* @throws IOException*/public static void resize(String srcUrl,String targetUrl,int width,int height) throws IOException {/** size(width,height) 若图片横比200小,高比300小,不变* 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300*/Thumbnails.of(srcUrl).size(width, height).toFile(targetUrl);}/*** 按照比例进行缩放* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param num 质量比例如 0.8* @throws IOException*/public static void scale(String srcUrl,String targetUrl,double num) throws IOException {Thumbnails.of(srcUrl).scale(num).toFile(targetUrl );}/*** 水印* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param width 宽* @param height 高* @param num 质量比例如 0.8* @param pos 显示位置:  Positions.BOTTOM_RIGHT  * @throws IOException*/public static void watermark(String srcUrl,String targetUrl,int width,int height,float num,Positions pos) throws IOException {Thumbnails.of(srcUrl).size(width,height).watermark(pos,ImageIO.read(new File(targetUrl)), num).outputQuality(num).toFile(targetUrl);}/*** 裁剪* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param width 宽* @param height 高* @param pos 显示位置:  Positions.BOTTOM_RIGHT  * @param x 区域宽度 * @param y 区域高度* @throws IOException*/public static void cut(String srcUrl,String targetUrl,int width,int height,Positions pos,int x,int y)throws IOException {Thumbnails.of(srcUrl).sourceRegion(pos,x,y).size(width, height).keepAspectRatio(false).toFile(targetUrl);}/*** 裁剪--指定坐标/大小* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param width 宽* @param height 高* @param pointA_1 坐标A1 * @param pointA_2坐标A2 * @param pointB_1坐标B1* @param pointB_2坐标B2* @throws IOException*/public static void cut(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).toFile(targetUrl);}/*** 转化图像格式* @param srcUrl 源图片地址* @param targetUrl 目标图片地址* @param width 宽* @param height 高* @param format 格式 如png/gif/jpg* @throws IOException*/public static void format(String srcUrl,String targetUrl,int width,int height,String format) throws IOException {Thumbnails.of(srcUrl).size(width, height).outputFormat(format).toFile(targetUrl);}
}
图片上传调用代码(jfinal)
/*** 上传多图片*/public void uploadFiles(){try{List<String> dbPathList = new ArrayList<String>();String folderName = UploadUtils.createFolder(SysCommonConstant.SERVER_IMGS_PATH);// 创建日期文件夹名称  :/imgs/2015-10String serverFolderPath = SysCommonConstant.SERVER_IMGS_PATH + File.separator + folderName + File.separator ; //文件保存目录String dbFolerPath = SysCommonConstant.SAVE_IMG_PATH + File.separator + folderName + File.separator ; //数据库文件保存目录List<UploadFile> fileList = getFiles(serverFolderPath, 999999999);System.out.println("size:"+fileList.size());for(UploadFile uf : fileList){File tmpFile = uf.getFile();String saveImgName = uf.getFileName();//图片名称Pattern reg = Pattern.compile("[\\.](jpg|png|jpeg|gif|JPG|PNG|JPEG|GIF)$");Matcher matcher = reg.matcher(saveImgName);if (!matcher.find()) {renderJson("图片格式只支持jpg|png|jpeg|gif.");return ;}  String newImgName = UploadUtils.rename(saveImgName);//文件重命名String newImgName2 = UploadUtils.rename(saveImgName);//文件重命名-用于压缩String serverPath = serverFolderPath + File.separator + newImgName;//服务器文件保存地址String dbPath = "";//数据库文件保存地址//重命名图片拷贝int imageWidthResource = Integer.parseInt(SysCommonConstant.IMAGE_WIDTH);int imageHeightResource = Integer.parseInt(SysCommonConstant.IMAGE_HEIGHT);File targetFile = new File(serverPath);Image img = ImageIO.read(tmpFile);int widthReal = img.getWidth(null);FileUtil.copyFile(tmpFile, targetFile);if(widthReal>imageWidthResource){/*PicUtils mypic = new PicUtils();mypic.compressPic(serverFolderPath, serverFolderPath, newImgName, newImgName2, imageWidthResource, imageHeightResource, true);*/ Pic.resize(serverPath,  serverFolderPath + File.separator + newImgName2, imageWidthResource, imageHeightResource);dbPath = dbFolerPath + newImgName2;//数据库保存地址}else{dbPath = dbFolerPath + newImgName;//数据库保存地址}dbPathList.add(dbPath);//将未重命名的图片删除tmpFile.delete();if(widthReal>imageWidthResource){targetFile.delete();}}Map<String,Object> result = new HashMap<String, Object>();Collections.reverse(dbPathList);//反转顺序result.put("picurl",dbPathList);renderJson(BeanUtil.getSucessRes(result));printReslutJson(result);}catch(Exception e){e.printStackTrace();log.info("上传图片异常.");renderJson(BeanUtil.getFaileRes("上传图片异常."));}}

图片压缩工具Thumbnailator的使用相关推荐

  1. java图片压缩工具类

    java图片压缩工具类 PicCompressUtil.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...

  2. java图片压缩工具类(指定压缩大小)

    1:先导入依赖 <!--thumbnailator图片处理--><dependency><groupId>net.coobird</groupId>&l ...

  3. Java图片压缩工具类(递归压缩到指定大小范围)

    Java图片压缩工具 工具类使用场景 公司做人脸识别项目时候需要上传学生.家长.教师.访客的正面照图片,但是人脸识别机器有限制只接收200KB-1M的图片,所以必须做图片压缩到指定范围大少. APP上 ...

  4. java 图片压缩 图片添加水印 thumbnailator javafx

    应用截图 启动界面 添加图片 处理结果 项目地址 图片压缩: java 图片压缩 图片添加水印 thumbnailator javafx pom.xml <?xml version=" ...

  5. C#制作图片压缩工具

    最近做的项目当中,需要将视频采集卡采集过来的图片进行压缩处理,原有一张JPG默认320*240大小为300KB,经过压缩之后为6KB,压缩50倍! 先放上截图吧: 可以添加单个文件,支持多选,也可以添 ...

  6. TinyPng:在线PNG图片压缩工具

    本资源由 伯乐在线 - 卢伟 整理 TinyPng:在线PNG图片压缩工具是一款可以帮助网页设计师们优化图片的工具,只需要简单的两步就可以完成对PNG图片的高压缩而且还不会影响PNG图片的质量,这样就 ...

  7. 在线PNG图片压缩工具推荐——TinyPng

    日常开发工作中时常需要对PNG图片压缩,今天推荐大家一个在线的图片压缩工具,ThinyPng 在线地址:TinyPNG – Compress WebP, PNG and JPEG images int ...

  8. 怎么直接压缩图片?好用的图片压缩工具推荐

    在我们现在的工作生活当中,图片已经变得无处不在了,但由于现在的拍摄设备像素越来越高,因此图片也变得越来越大,因此在使用一些高清图片的时候经常会遇到图片太大无法上传或者图片太大传输太慢的情况.因此,图片 ...

  9. android图片压缩工具类

    好久没写博客了,一方面是因为最近找了家实习单位,很累基本上下班后就没有打不起精神去学习,另一方面我自己觉得写博客确实有点耗时间,趁着周六周日想花点时间研究下fresco,picass,Glide等框架 ...

最新文章

  1. 笑翻了!想象的论文答辩和真实的论文答辩
  2. 在线验证json字符串
  3. 【数论Day1】 最大公约数(gcd)题目
  4. 2019年江苏省计算机一级考试题目和答案,江苏省计算机等级考试一级2019年(春)...
  5. Media Player 嵌套网页中播放上传视频记录
  6. TensorFlow入门篇(三):MNIST数据集简易分类
  7. git pull命令报错
  8. php伪随机数 ctf,[GWCTF 2019]枯燥的抽奖
  9. paip.程序不报错自动退出的解决
  10. Himall商城枚举帮助类EnumHelper(3)
  11. mysql 嵌套查询多表_MySql嵌套查询+关联查询+多表查询+对应案例 超详细,一看就会!!!...
  12. 单细胞测序在免疫治疗研究中的应用
  13. gwt执行ajax,使用GWT开发AJAX应用程序
  14. Ant design Vue 如何在a-table表格标题/内容上添加一个按钮
  15. java实现福利彩票抽奖_【福利】快来参与抽奖获得《Java程序设计》
  16. 信息级联(Information cascade)
  17. 如何买到便宜的云服务器
  18. WIN7-OEM资料包
  19. Echarts实现区级地图
  20. 冲量在线正式加入中关村可信计算产业联盟,共同推动可信计算产业深度发展

热门文章

  1. IOS开发之开发工具Xcode下载
  2. 祝各位SAPER元旦快乐!
  3. 牛客网-Verilog篇
  4. Disabling usage of PDSC Debug Description
  5. 写好作文的6大步骤,把写作文变成一件“轻松事儿”!
  6. 阿里云创建管理bucket(一)
  7. 网技·实验·3.29
  8. linux iso挂载报错,linux下文件系统、iso挂载
  9. COVID-19 Cases Prediction (Regression)
  10. 用python通过selenium自动化测试抓取天猫店铺数据