日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图片压缩工具Thumbnailator的使用

發(fā)布時間:2023/12/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图片压缩工具Thumbnailator的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? 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 區(qū)域寬度 * @param y 區(qū)域高度* @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);// 創(chuàng)建日期文件夾名稱 :/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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。