图片压缩工具Thumbnailator的使用
来源:互联网 发布:部落冲突亡灵升级数据 编辑:程序博客网 时间:2024/04/25 19:57
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("上传图片异常."));}}
0 0
- 图片压缩工具Thumbnailator的使用
- 图片压缩工具Thumbnailator的使用
- 使用Thumbnailator缩略图片
- 使用thumbnailator操作图片
- java使用Thumbnailator操作图片
- java使用Thumbnailator操作图片
- java使用Thumbnailator操作图片
- java使用Thumbnailator操作图片
- java使用Thumbnailator操作图片
- java使用Thumbnailator操作图片
- 使用Thumbnailator工具压缩图片
- Thumbnailator的使用
- 一些图片的压缩工具
- 图片的压缩工具类
- java 使用Thumbnailator图片处理包装类
- 最适合程序员使用的图片压缩工具 pngquant
- ESC_JavaScript压缩工具的使用
- PNG图片的在线压缩工具
- Ubuntu 14.04 安装xvid编码器
- 关于arcgis 9.3 Java服务器发布服务问题
- <LeetCode OJ> 357. Count Numbers with Unique Digits
- 单链表的增、删、查、改、逆置、排序
- restoration of CBCentralManager is only allowed for applications that have specified the "bluetooth-
- 图片压缩工具Thumbnailator的使用
- AngularJs+bootstrap搭载前台框架3
- 博客搬家
- Execution failed for task ':compileDebugAidl'. java.lang.IllegalStateException: aidl is missing
- IOS Block传值
- import caffe经典报错处理on Ubuntu & Mac OS 10.11
- 值得学习的C语言开源项目
- iOS 获得键盘的高度 NSNotificationCenter
- Check the existence of public synonyms Remove the public synonyms