Java實現(xiàn)圖片比率縮放
本文實例為大家分享了Java實現(xiàn)圖片比率縮放的具體代碼,供大家參考,具體內(nèi)容如下
通過Thumbnails實現(xiàn)圖片縮放
需要導(dǎo)入pom依賴,可以到中央倉庫獲取最小的工具包
<dependency> ? ? ? ?<groupId>net.coobird</groupId> ? ? ? ?<artifactId>thumbnailator</artifactId> ? ? ? ? <version>0.4.8</version> </dependency>
//讀取圖片
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
ByteArrayOutputStream out2 = new ByteArrayOutputStream(); ?
?? ?Thumbnails.of(bufferedImage).size(750,1344).outputFormat("png").toOutputStream(out2);//縮放圖片
?? ?InitImage("縮放圖", out2.toByteArray());//顯示圖片java API實現(xiàn)圖片縮放
調(diào)用方法
InitImage("自定義壓縮圖", ?zoomBySize(750,1334,bufferedImage,"png"));//調(diào)用方法具體方法實現(xiàn)1
? /**
? ? ?*@description: 按比例對圖片進行縮放. 檢測圖片是橫圖還是豎圖
? ? ?*@param : width 縮放后的寬
? ? ?*@param : height 縮放后的高
? ? ?*@param : img BufferedImage
? ? ?*@param : ext 圖片類型 如: jpg
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:08
? ? ?*/
? ? public static byte[] zoomBySize(int width, int height, BufferedImage img, String ext) throws IOException {
? ? ? ? //橫向圖
? ? ? ? if (img.getWidth() >= img.getHeight()) {
? ? ? ? ? ? double ratio = CalculateZoomRatio(width, img.getWidth());
? ? ? ? ? ? //獲取壓縮對象
? ? ? ? ? ? BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? //當(dāng)圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? if (newbufferedImage.getHeight() > height) {
? ? ? ? ? ? ? ? ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
? ? ? ? ? ? ? ? newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? }
? ? ? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ? ? ImageIO.write(newbufferedImage, ext, out);
? ? ? ? ? ? return out.toByteArray();
? ? ? ? }
? ? ? ? //縱向圖
? ? ? ? if (img.getWidth() < img.getHeight()) {
? ? ? ? ? ? double ratio = CalculateZoomRatio(height, img.getHeight());
? ? ? ? ? ? //獲取壓縮對象
? ? ? ? ? ? BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? //當(dāng)圖片寬大于圖片壓縮寬時 再次縮放
? ? ? ? ? ? if (newbufferedImage.getHeight() > height) {
? ? ? ? ? ? ? ? ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
? ? ? ? ? ? ? ? newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? }
? ? ? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ? ? ImageIO.write(newbufferedImage, ext, out);
? ? ? ? ? ? return out.toByteArray();
? ? ? ? }
? ? ? ? //以上都不符合時 強制縮放
? ? ? ? Image _img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
? ? ? ? BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
? ? ? ? Graphics2D graphics = image.createGraphics();
? ? ? ? graphics.drawImage(_img, 0, 0, null);
? ? ? ? graphics.dispose();
? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ImageIO.write(image, ext, out);
? ? ? ? return out.toByteArray();
? ? }
? ? /**
? ? ?*@description: 按比例對圖片進行縮放.
? ? ?*@param : scale 縮放比率
? ? ?*@param : img BufferedImage
? ? ?*@param : ext 圖片類型
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:07
? ? ?*/
? ? public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
? ? ? ? //獲取縮放后的長和寬
? ? ? ? int _width = (int) (scale * img.getWidth());
? ? ? ? int _height = (int) (scale * img.getHeight());
? ? ? ? //獲取縮放后的Image對象
? ? ? ? Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);
? ? ? ? //新建一個和Image對象相同大小的畫布
? ? ? ? BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
? ? ? ? //獲取畫筆
? ? ? ? Graphics2D graphics = image.createGraphics();
? ? ? ? //將Image對象畫在畫布上,最后一個參數(shù),ImageObserver:接收有關(guān) Image 信息通知的異步更新接口,沒用到直接傳空
? ? ? ? graphics.drawImage(_img, 0, 0, null);
? ? ? ? //釋放資源
? ? ? ? graphics.dispose();
? ? ? ? return image;
? ? }
? ? /**
? ? ?*@description: 縮放比率計算
? ? ?*@param : divisor
? ? ?*@param : dividend
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:07
? ? ?*/
? ? private static double CalculateZoomRatio(int divisor, int dividend) {
? ? ? ? return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
? ? }實現(xiàn)方法2
?/**
? ? ?*@description: 按比例對圖片進行縮放. 檢測圖片是橫圖還是豎圖
? ? ?*@param : width 縮放后的寬
? ? ?*@param : height 縮放后的高
? ? ?*@param : img BufferedImage
? ? ?*@param : ext 圖片類型 如: jpg
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:08
? ? ?*/
?? ?public static byte[] zoomByScale(int width, int height, BufferedImage img, String ext) throws IOException {
? ? ? ? //橫向圖
? ? ? ? if (img.getWidth() >= img.getHeight()) {
? ? ? ? ? ? double ratio = CalculateZoomRatio(width, img.getWidth());
? ? ? ? ? ? //獲取壓縮對象
? ? ? ? ? ? BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? //當(dāng)圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? if (newbufferedImage.getHeight() > height) {
? ? ? ? ? ? ? ? ratio = CalculateZoomRatio(height, newbufferedImage.getHeight());
? ? ? ? ? ? ? ? newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? }
? ? ? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ? ? ImageIO.write(newbufferedImage, ext, out);
? ? ? ? ? ? return out.toByteArray();
? ? ? ? }
? ? ? ? //縱向圖
? ? ? ? if (img.getWidth() < img.getHeight()) {
? ? ? ? ? ? double ratio = CalculateZoomRatio(height, img.getHeight());
? ? ? ? ? ? //獲取壓縮對象
? ? ? ? ? ? BufferedImage newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? //當(dāng)圖片寬大于圖片壓縮寬時 再次縮放
? ? ? ? ? ? if (newbufferedImage.getHeight() > height) {
? ? ? ? ? ? ? ? ratio = CalculateZoomRatio(width, newbufferedImage.getWidth());
? ? ? ? ? ? ? ? newbufferedImage = zoomByScale(ratio, img, ext);
? ? ? ? ? ? }
? ? ? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ? ? ImageIO.write(newbufferedImage, ext, out);
? ? ? ? ? ? return out.toByteArray();
? ? ? ? }
? ? ? ? //以上都不符合時 強制縮放
? ? ? ? Image _img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
? ? ? ? // 獲取縮放比例
? ? ? ? double wr=0,hr=0;
?? ??? ?wr = width * 1.0 / img.getWidth();?
?? ??? ?hr = height * 1.0 / img.getHeight();
? ? ? ? AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
? ? ? ? _img = ato.filter(img, null);
? ? ? ? ByteArrayOutputStream out = new ByteArrayOutputStream();
? ? ? ? ImageIO.write((BufferedImage) _img,ext,out);//寫入縮減后的圖片
? ? ? ? return out.toByteArray();
? ? }
? ? /**
? ? ?*@description: 按比例對圖片進行縮放.
? ? ?*@param : scale 縮放比率
? ? ?*@param : img BufferedImage
? ? ?*@param : ext 圖片類型
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:07
? ? ?*/
? ? public static BufferedImage zoomByScale(double scale, BufferedImage img, String ext) throws IOException {
? ? ? ? //獲取縮放后的長和寬
? ? ? ? int _width = (int) (scale * img.getWidth());
? ? ? ? int _height = (int) (scale * img.getHeight());
? ? ? //設(shè)置縮放目標圖片模板
? ? ? ? Image _img = img.getScaledInstance(_width, _height, Image.SCALE_SMOOTH);
? ? ? ? //縮放圖片
? ? ? ? AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
? ? ? ? _img = ato.filter(img, null);
? ? ? ? return (BufferedImage) _img;
? ? }
? ? /**
? ? ?*@description: 縮放比率計算
? ? ?*@param : divisor
? ? ?*@param : dividend
? ? ?*@author: YWR
? ? ?*@return:
? ? ?*@throws:
? ? ?*@date: 2020/9/17 0:07
? ? ?*/
? ? private static double CalculateZoomRatio(int divisor, int dividend) {
? ? ? ? return BigDecimal.valueOf(divisor).divide(BigDecimal.valueOf(dividend), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud應(yīng)用idea實現(xiàn)可相互調(diào)用的多模塊程序詳解
IDEA 全稱 IntelliJ IDEA,是java編程語言的集成開發(fā)環(huán)境。IntelliJ在業(yè)界被公認為最好的Java開發(fā)工具,尤其在智能代碼助手、代碼自動提示、重構(gòu)、JavaEE支持、各類版本工具(git、svn等)、JUnit、CVS整合、代碼分析、 創(chuàng)新的GUI設(shè)計等方面的功能可以說是超常的2022-07-07
Springboot發(fā)送郵件功能的實現(xiàn)詳解
Java數(shù)組與二維數(shù)組及替換空格實戰(zhàn)真題講解

