java對圖片進行壓縮和resize縮放的方法
序
這里展示一下如何對圖片進行壓縮和resize。分享給大家,具體如下:
壓縮
public static boolean compress(String src,String to, float quality) { boolean rs = true; // Build param JPEGEncodeParam param = null; // Build encoder File destination = new File(to); FileOutputStream os = null; try { BufferedImage image = ImageIO.read(new File(src)); param = JPEGCodec.getDefaultJPEGEncodeParam(image); param.setQuality(quality, false); os = FileUtils.openOutputStream(destination); JPEGImageEncoder encoder; if (param != null) { encoder = JPEGCodec.createJPEGEncoder(os, param); } else { return false; } encoder.encode(image); } catch(Exception e){ e.printStackTrace(); rs = false; }finally { IOUtils.closeQuietly(os); } return rs; }
resize
public static boolean resize(String src,String to,int newWidth,int newHeight) { try { File srcFile = new File(src); File toFile = new File(to); BufferedImage img = ImageIO.read(srcFile); int w = img.getWidth(); int h = img.getHeight(); BufferedImage dimg = new BufferedImage(newWidth, newHeight, img.getType()); Graphics2D g = dimg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(img, 0, 0, newWidth, newHeight, 0, 0, w, h, null); g.dispose(); ImageIO.write(dimg, "jpg", toFile); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Elasticsearch倒排索引詳解及實際應(yīng)用中的優(yōu)化
Elasticsearch(ES)使用倒排索引來加速文本的搜索速度,倒排索引之所以高效,主要是因為它改變了數(shù)據(jù)的組織方式,使得查詢操作可以快速完成,這篇文章主要給大家介紹了關(guān)于Elasticsearch倒排索引詳解及實際應(yīng)用中優(yōu)化的相關(guān)資料,需要的朋友可以參考下2024-08-08Java使用自動化部署工具Gradle中的任務(wù)設(shè)定教程
Grandle使用同樣運行于JVM上的Groovy語言編寫,本文會對此進行初步夠用的講解,接下來我們就一起來看一下Java使用自動化部署工具Gradle中的任務(wù)設(shè)定教程:2016-06-06Spring中@RequestMapping、@PostMapping、@GetMapping的實現(xiàn)
RequestMapping、@PostMapping和@GetMapping是三個非常常用的注解,本文就來介紹一下這三種注解的具體使用,具有一定的參考價值,感興趣的可以了解一下2024-07-07線程局部變量的實現(xiàn)?ThreadLocal使用及場景介紹
這篇文章主要為大家介紹了線程局部變量的實現(xiàn)?ThreadLocal使用及場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Jenkins一鍵打包部署SpringBoot應(yīng)用的方法步驟
本文主要介紹了使用Jenkins一鍵打包部署SpringBoot應(yīng)用的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能
本文介紹了如何使用SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能,同時提供了數(shù)據(jù)庫的設(shè)計和詳細的后端代碼,前端界面使用的Vue2,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-05-05SpringBoot處理全局統(tǒng)一異常的實現(xiàn)
這篇文章主要介紹了SpringBoot處理全局統(tǒng)一異常的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09