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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Thumbnailator实现图片压缩

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

今天給大家介紹一個非常好用的一個java開源的Thumbnailator圖片壓縮jar,隨著智能手機的像素越來越高,用戶把手機拍攝的3-10兆的圖片上傳到服務器上,在從服務器上面讀取上傳的圖片會加載非常的緩慢,所以只能考慮壓縮圖片的質量從而保證網站打開的速度。

第一步導入jar:

<!-- 圖片壓縮 --> <dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version> </dependency>

常用的格式:

Thumbnails.of("原圖文件的路徑") .scale(1f) // 值在0到1之間,1f就是原圖大小,0.5就是原圖的一半大小.outputQuality(0.5f) // 值也是在0到1,越接近于1質量越好,越接近于0質量越差.toFile("壓縮后文件的路徑");

其他的格式:

1.指定大小比例進行縮放--考慮圖片的完整度size(寬度, 高度) 2.按照比例進行縮放scale(比例) 3.不按照比例,指定大小進行縮放--不考慮圖片的完整度size(寬度, 高度).keepAspectRatio(false) 4.圖片旋轉size(寬度, 高度).rotae(90) -- 旋轉90度(必須加size不然報錯) 5.水印BOTTOM_RIGHT 右下角CENTER 中心size(寬度, 高度).watermark(Positions.CENTER,).size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("d:/uploadImg/head.png")), 0.5f).outputQuality(0.8f).toFile(dest); 6.裁剪 -- 以圖片中心400*400區域.sourceRegion(Positions.CENTER, 400,400).size(200,200).keepAspectRatio(false) 7.轉化圖像格式.size(200,200).outputFormat(".png")

工具類:

/*** 圖片上傳壓縮** @param file 文件* @param subdirectory 文件夾名稱* @param width 圖片寬度* @param height 圖片高度* @return*/public static Map<String, String> saveImgCompress(MultipartFile file, String subdirectory,Integer width,Integer height) {//上傳文件路徑String path = GetServerPathUtil.getPath("uploadImg");// 獲取文件名稱String filename = file.getOriginalFilename();String fielhouzhui = filename.substring(filename.lastIndexOf("."), filename.length());//重新修改文件名防止重名filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+ (new Random().nextInt(9000) % (9000 - 1000 + 1) + 1000) + StringUtils.getUUID() + fielhouzhui;File filepath = new File(path, filename);//判斷路徑是否存在,沒有就創建一個if (!filepath.getParentFile().exists()) {filepath.getParentFile().mkdirs();}File img = new File(path + File.separator + filename);Map<String, String> map = new HashMap<>();try {Thumbnails.of(file.getInputStream()).size(width,height).toFile(img);map.put("res", "success");map.put("url", filename);return map;} catch (IOException e) {LOG.info(filename + "圖片上傳失敗:" + e);map.put("res", "error");return map;}}

設置上傳的圖片存放在項目運行目錄下面的某一個文件夾:

/*** 獲取文件目錄** @param subdirectory 文件夾名稱* @return*/public static String getPath(String subdirectory) {//獲取跟目錄---與jar包同級目錄的upload目錄File upload = null;try {// 獲取項目中所在的路徑// C:\Users\Administrator\Desktop\SpringBoot\xx\xx-common\target\classesFile path = new File(ResourceUtils.getURL("classpath:").getPath());// 判斷目錄是否存在if (!path.exists()) path = new File("");// 項目運行目錄 + subdirectoryupload = new File(path.getAbsolutePath(), subdirectory);//如果不存在則創建目錄if (!upload.exists()) upload.mkdirs();String realPath = upload + "/";return realPath;// C:\Users\Administrator\Desktop\SpringBoot\XX\XX-common\target\classes\123/} catch (FileNotFoundException e) {LOG.info("GetServerPathUtil===>獲取服務器路徑發生錯誤!");System.err.println("GetServerPathUtil===>獲取服務器路徑發生錯誤!");throw new RuntimeException("獲取服務器路徑發生錯誤!");}}

總結

以上是生活随笔為你收集整理的Thumbnailator实现图片压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

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