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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

JAVA对图片进行等比/非等比压缩处理

發(fā)布時間:2023/12/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA对图片进行等比/非等比压缩处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我在項目中遇到了對接人臉識別的需求,上游需要對上傳的圖片進行壓縮,并且嚴格限制圖片的大小和長寬。

我們APP端也會對圖片進行處理,自己也找了一些對圖片進行處理的方法,這里記錄一下,方便學習使用。

注:下面的代碼只是我自己測試使用,具體業(yè)務還需要進行處理。

代碼中使用到的這個類【import com.sun.image.codec.jpeg.JPEGCodec;】在JDK1.7以后就刪除了,可以使用這個【ImageIO.write】進行代替

轉自:https://blog.csdn.net/qq844579582/article/details/52790344

package com.payment.biz.util;import com.payment.biz.factory.log.LogEx;import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream;/*** @Author: *** @Company: **** @Date: 2018-12-06 18:29* @Description: 圖片壓縮工具類*/ public class CommonPictureUtils {private CommonPictureUtils() {}/*** @Author: *** @Company: **** @Description: 是否等比壓縮圖片的方法,是的話proportion=true,只需設置一個新圖片的寬度,否的話proportion=false,新圖片的寬高都需要設置* @Date: 2018/12/7 10:32* @Param: [filePath, outputWidth, outputHeight, proportion]* @Return: java.lang.String*/public static String compressPic(String filePath, int outputWidth, int outputHeight, boolean proportion) throws Exception {//獲得文件源File file = new File(filePath);if (!file.exists()) {return "";}//imageIO.read(arg);圖片解析,可傳入文件,文件輸入流等Image img = ImageIO.read(file);if (img.getWidth(null) == -1) {return "";} else {int newWidth;int newHeight;//判斷是否等比縮放if (proportion) {double rate = (double) img.getWidth(null) / (double) outputWidth;newWidth = outputWidth;newHeight = (int) ((double) img.getHeight(null) / rate);} else {newWidth = outputWidth;newHeight = outputHeight;}/** Image.SCALE_SMOOTH的縮略算法生成縮略圖片的平滑度的優(yōu)先級比速度高,生成的圖片質量好但是速度慢* 根據具體要求取舍時間或是質量*/FileOutputStream out = null;try {BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);/* @param img the specified image to be drawn.* @param x x坐標.* @param y y坐標.* @param width the width of the rectangle.* @param height the height of the rectangle.* @param observer 像素是否被改變*/tag.getGraphics().drawImage(img, 0, 0, newWidth, newHeight, null);out = new FileOutputStream(filePath);//JPEGImageEncoder可適用于其他圖片的類型的轉換:// 注意下面這個注釋掉的類在JDK1.7以后就刪除不再使用了,可以使用ImageIO.write進行代替 // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); // encoder.encode(tag);ImageIO.write(tag, "jpg", out);out.flush();out.close();tag.flush();tag = null;} catch (Exception e) {e.printStackTrace();LogEx.BI("9005", "CommonPictureUtils", filePath, "壓縮圖片異常:[{}].[{}]", new Object[]{filePath, e.getMessage()});return "";} finally {if (out != null) {out.close();}}return filePath;}}public static void main(String[] args) throws Exception {String s = CommonPictureUtils.compressPic("F:\\temp\\123 - 副本.jpg", 400, 400, true);System.out.println(s);} }

?

總結

以上是生活随笔為你收集整理的JAVA对图片进行等比/非等比压缩处理的全部內容,希望文章能夠幫你解決所遇到的問題。

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