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

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

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

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

通過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)圖片縮放

調(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);
? ? ? ? ? ? //當圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? 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:接收有關(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);
? ? ? ? ? ? //當圖片大于圖片壓縮高時 再次縮放
? ? ? ? ? ? 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());
? ? ? //設(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宣化县| 吴江市| 隆安县| 文安县| 临安市| 登封市| 寻乌县| 平武县| 祁门县| 洛宁县| 九江市| 镇平县| 拉孜县| 霍城县| 东方市| 岳西县| 仪陇县| 罗田县| 高平市| 玛曲县| 张家界市| 高台县| 全州县| 西青区| 轮台县| 唐海县| 莱西市| 白沙| 湘潭县| 澄城县| 佳木斯市| 兴城市| 合江县| 二手房| 广宗县| 霸州市| 花垣县| 达拉特旗| 永兴县| 修文县| 崇左市|