Android圖片壓縮技巧,

來源:互聯網
上載者:User

Android圖片壓縮技巧,

請尊重他人的勞動成果,轉載請註明出處:Android圖片壓縮技巧

http://blog.csdn.net/fengyuzhengfan/article/details/41759835

當需要將Android用戶端的圖片上傳到伺服器時,往往需要將圖片進行壓縮,關於圖片的壓縮方法,小編分享幾種常用的方式:

第一種方式:裁切以達到壓縮的目的

我曾在《Android開發之裁剪照片》一文中詳細介紹過如何裁切照片,感興趣的朋友可以去看一下。


第二種方式:將圖片進行降質處理(即降低圖片的品質)以達到壓縮的目的

這種方式也是比較常用的方式,下面就為大家介紹如何對圖片進行降質:

將圖片降質我們可以使用Bitmap的這個方法:boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

其中,參數format表示壓縮後的格式,quality壓縮後的圖片品質(0表示最低,100表示不壓縮),stream表示要將壓縮後的圖片儲存到的輸出資料流。

下面是詳細代碼:

/** * 多線程壓縮圖片的品質 * @author JPH * @param bitmap 記憶體中的圖片 * @param imgPath 圖片的儲存路徑 * @date 2014-12-5下午11:30:43 */public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){new Thread(new Runnable() {//開啟多線程進行壓縮處理@Overridepublic void run() {// TODO Auto-generated method stubByteArrayOutputStream baos = new ByteArrayOutputStream();options = 100;bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//品質壓縮方法,把壓縮後的資料存放到baos中 (100表示不壓縮,0表示壓縮到最小)while (baos.toByteArray().length / 1024 > 100) {//迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮         baos.reset();//重設baos即讓下一次的寫入覆蓋之前的內容 options -= 10;//圖片品質每次減少10if(options<0)options=0;//如果圖片品質小於10,則將圖片的品質壓縮到最小值bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//將壓縮後的圖片儲存到baos中if(options==0)break;//如果圖片的品質已降到最低則,不再進行壓縮}try {FileOutputStream fos = new FileOutputStream(new File(imgPath));//將壓縮後的圖片儲存的本地上指定路徑中fos.write(baos.toByteArray());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}}).start();}

方法解析:

由於此方法中包含I/O操作和遞迴調用比較耗時所以我採用了多線程去處理。


第三種方式:按比例縮小圖片的像素以達到壓縮的目的

此種方法主要是使用android.graphics.BitmapFactory.Options.Options()方法將圖片以指定的採用率載入到記憶體然後輸出到本地以達到壓縮像素的目的。

詳細代碼:

/** * 按比例縮小圖片的像素以達到壓縮的目的 * @author JPH * @param imgPath * @date 2014-12-5下午11:30:59 */public static void compressImageByPixel(String imgPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();newOpts.inJustDecodeBounds = true;//唯讀邊,不讀內容Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);newOpts.inJustDecodeBounds = false;int width = newOpts.outWidth;int height = newOpts.outHeight;float maxSize = 1000f;//預設1000pxint be = 1;if (width > height && width > maxSize) {//縮放比,用高或者寬其中較大的一個資料進行計算be = (int) (newOpts.outWidth / maxSize);} else if (width < height && height > maxSize) {be = (int) (newOpts.outHeight / maxSize);}be++;newOpts.inSampleSize = be;//設定採樣率newOpts.inPreferredConfig = Config.ARGB_8888;//該模式是預設的,可不設newOpts.inPurgeable = true;// 同時設定才會有效newOpts.inInputShareable = true;//。當系統記憶體不夠時候圖片自動被回收bitmap = BitmapFactory.decodeFile(imgPath, newOpts);ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);try {FileOutputStream fos = new FileOutputStream(new File(imgPath));fos.write(baos.toByteArray());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}

第四種方式:將圖片先按比例壓縮然後再降質

此種方式主要結合第二種和第三種方法以下是詳細代碼:

/** * 多線程壓縮圖片的品質 * @author JPH * @param bitmap 記憶體中的圖片 * @param imgPath 圖片的儲存路徑 * @date 2014-12-5下午11:30:43 */public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){new Thread(new Runnable() {//開啟多線程進行壓縮處理@Overridepublic void run() {// TODO Auto-generated method stubByteArrayOutputStream baos = new ByteArrayOutputStream();options = 100;bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//品質壓縮方法,把壓縮後的資料存放到baos中 (100表示不壓縮,0表示壓縮到最小)while (baos.toByteArray().length / 1024 > 100) {//迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮         baos.reset();//重設baos即讓下一次的寫入覆蓋之前的內容 options -= 10;//圖片品質每次減少10if(options<0)options=0;//如果圖片品質小於10,則將圖片的品質壓縮到最小值bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//將壓縮後的圖片儲存到baos中if(options==0)break;//如果圖片的品質已降到最低則,不再進行壓縮}try {FileOutputStream fos = new FileOutputStream(new File(imgPath));//將壓縮後的圖片儲存的本地上指定路徑中fos.write(baos.toByteArray());fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}}}).start();}/** * 按比例縮小圖片的像素以達到壓縮的目的 * @author JPH * @param imgPath * @date 2014-12-5下午11:30:59 */public static void compressImageByPixel(String imgPath) {BitmapFactory.Options newOpts = new BitmapFactory.Options();newOpts.inJustDecodeBounds = true;//唯讀邊,不讀內容Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);newOpts.inJustDecodeBounds = false;int width = newOpts.outWidth;int height = newOpts.outHeight;float maxSize = 1000f;//預設1000pxint be = 1;if (width > height && width > maxSize) {//縮放比,用高或者寬其中較大的一個資料進行計算be = (int) (newOpts.outWidth / maxSize);} else if (width < height && height > maxSize) {be = (int) (newOpts.outHeight / maxSize);}be++;newOpts.inSampleSize = be;//設定採樣率newOpts.inPreferredConfig = Config.ARGB_8888;//該模式是預設的,可不設newOpts.inPurgeable = true;// 同時設定才會有效newOpts.inInputShareable = true;//。當系統記憶體不夠時候圖片自動被回收bitmap = BitmapFactory.decodeFile(imgPath, newOpts);compressImageByQuality(bitmap,imgPath);//壓縮好比例大小後再進行品質壓縮  }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.