最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)圖片比率縮放

 更新時間:2022年04月22日 10:07:26   作者:Yweir  
這篇文章主要為大家詳細介紹了Java通過Thumbnails實現(xiàn)圖片比率縮放,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)圖片比率縮放的具體代碼,供大家參考,具體內容如下

通過Thumbnails實現(xiàn)圖片縮放

需要導入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)圖片縮放

調用方法

InitImage("自定義壓縮圖", ?zoomBySize(750,1334,bufferedImage,"png"));//調用方法

具體方法實現(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);
? ? ? ? ? ? //當圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? 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);
? ? ? ? ? ? //當圖片寬大于圖片壓縮寬時 再次縮放
? ? ? ? ? ? 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:接收有關 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);
? ? ? ? ? ? //當圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? 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);
? ? ? ? ? ? //當圖片寬大于圖片壓縮寬時 再次縮放
? ? ? ? ? ? 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());
? ? ? //設置縮放目標圖片模板
? ? ? ? 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();
? ? }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

建始县| 安庆市| 蕉岭县| 嘉兴市| 瑞昌市| 皋兰县| 阿图什市| 吉林市| 高阳县| 界首市| 特克斯县| 囊谦县| 玛纳斯县| 西盟| 海兴县| 张家界市| 泾阳县| 安岳县| 辽阳市| 顺平县| 广德县| 云浮市| 忻城县| 兴安盟| 黎城县| 玉屏| 黄大仙区| 成安县| 林周县| 遂宁市| 麻阳| 米泉市| 渝北区| 忻城县| 广平县| 南宁市| 神池县| 义乌市| 渝中区| 南靖县| 彩票|