a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android整理好的圖片壓縮工具類

2019-10-21 21:33:48
字體:
來源:轉載
供稿:網友

Android設備的內存有限,對于大圖片,必須進行壓縮后再進行顯示,否則會出現內存溢出:OOM;

處理策略:

1.使用縮略圖(Thumbnails);

Android系統會給檢測到的圖片創建縮略圖;可以操作Media內容提供者中的Image對圖片進行操作;

2.手動壓縮:

  • (1)根據圖片和屏幕尺寸,等比壓縮,完美顯示;
  • (2)降低圖片質量,壓縮圖片大小;

以下是自己整理的小工具類(對于按比例縮放后,在此并未再進行質量縮放,此時圖片大小有可能超出我們期望的限制;假如我們有嚴格的大小限制需求,可先進行按比例縮放后,判斷此時圖片大小是否超出限制;如果超出限制,對其再進行質量縮放即可。建議使用按比例縮放,按質量縮放很有可能導致圖片失真。)

</pre><p><pre name="code" class="java">package com.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.media.ExifInterface; /**  * 圖片壓縮工具類  * @author 丶Life_  */ public class ImageCompressUtil {   /**    * 通過降低圖片的質量來壓縮圖片    * @param bmp    *      要壓縮的圖片位圖對象    * @param maxSize    *      壓縮后圖片大小的最大值,單位KB    * @return 壓縮后的圖片位圖對象    */   public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     int quality = 100;     bitmap.compress(CompressFormat.JPEG, quality, baos);     System.out.println("圖片壓縮前大小:" + baos.toByteArray().length + "byte");     boolean isCompressed = false;     while (baos.toByteArray().length / 1024 > maxSize) {       quality -= 10;       baos.reset();       bitmap.compress(CompressFormat.JPEG, quality, baos);       System.out.println("質量壓縮到原來的" + quality + "%時大小為:"           + baos.toByteArray().length + "byte");       isCompressed = true;     }     System.out.println("圖片壓縮后大小:" + baos.toByteArray().length + "byte");     if (isCompressed) {       Bitmap compressedBitmap = BitmapFactory.decodeByteArray(           baos.toByteArray(), 0, baos.toByteArray().length);       recycleBitmap(bitmap);       return compressedBitmap;     } else {       return bitmap;     }   }   /**    * 傳入圖片url,通過壓縮圖片的尺寸來壓縮圖片大小   * @param pathName 圖片的完整路徑    * @param targetWidth 縮放的目標寬度    * @param targetHeight 縮放的目標高度    * @return 縮放后的圖片    */   public static Bitmap compressBySize(String pathName, int targetWidth,       int targetHeight) {     BitmapFactory.Options opts = new BitmapFactory.Options();     opts.inJustDecodeBounds = true;// 不去真的解析圖片,只是獲取圖片的頭部信息,包含寬高等;     Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);     // 得到圖片的寬度、高度;     int imgWidth = opts.outWidth;     int imgHeight = opts.outHeight;     // 分別計算圖片寬度、高度與目標寬度、高度的比例;取大于等于該比例的最小整數;     int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);     int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);     if (widthRatio > 1 || heightRatio > 1) {       if (widthRatio > heightRatio) {         opts.inSampleSize = widthRatio;       } else {         opts.inSampleSize = heightRatio;       }     }     // 設置好縮放比例后,加載圖片進內容;     opts.inJustDecodeBounds = false;     bitmap = BitmapFactory.decodeFile(pathName, opts);     return bitmap;   }   /**    * 傳入bitmap,通過壓縮圖片的尺寸來壓縮圖片大小     * @param bitmap 要壓縮圖片    * @param targetWidth 縮放的目標寬度    * @param targetHeight 縮放的目標高度    * @return 縮放后的圖片    */   public static Bitmap compressBySize(Bitmap bitmap, int targetWidth,       int targetHeight) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bitmap.compress(CompressFormat.JPEG, 100, baos);     BitmapFactory.Options opts = new BitmapFactory.Options();     opts.inJustDecodeBounds = true;     bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,         baos.toByteArray().length, opts);     // 得到圖片的寬度、高度;     int imgWidth = opts.outWidth;     int imgHeight = opts.outHeight;     // 分別計算圖片寬度、高度與目標寬度、高度的比例;取大于該比例的最小整數;     int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);     int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);     if (widthRatio > 1 || heightRatio > 1) {       if (widthRatio > heightRatio) {         opts.inSampleSize = widthRatio;       } else {         opts.inSampleSize = heightRatio;       }     }     // 設置好縮放比例后,加載圖片進內存;     opts.inJustDecodeBounds = false;     Bitmap compressedBitmap = BitmapFactory.decodeByteArray(         baos.toByteArray(), 0, baos.toByteArray().length, opts);     recycleBitmap(bitmap);     return compressedBitmap;   }   /**    * 通過壓縮圖片的尺寸來壓縮圖片大小,通過讀入流的方式,可以有效防止網絡圖片數據流形成位圖對象時內存過大的問題;    * @param InputStream 要壓縮圖片,以流的形式傳入    * @param targetWidth 縮放的目標寬度    * @param targetHeight 縮放的目標高度    * @return 縮放后的圖片    * @throws IOException 讀輸入流的時候發生異常    */   public static Bitmap compressBySize(InputStream is, int targetWidth,       int targetHeight) throws IOException {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     byte[] buff = new byte[1024];     int len = 0;     while ((len = is.read(buff)) != -1) {       baos.write(buff, 0, len);     }     byte[] data = baos.toByteArray();     BitmapFactory.Options opts = new BitmapFactory.Options();     opts.inJustDecodeBounds = true;     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,         opts);     // 得到圖片的寬度、高度;     int imgWidth = opts.outWidth;     int imgHeight = opts.outHeight;     // 分別計算圖片寬度、高度與目標寬度、高度的比例;取大于該比例的最小整數;     int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);     int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);     if (widthRatio > 1 || heightRatio > 1) {       if (widthRatio > heightRatio) {         opts.inSampleSize = widthRatio;       } else {         opts.inSampleSize = heightRatio;       }     }     // 設置好縮放比例后,加載圖片進內存;     opts.inJustDecodeBounds = false;     bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);     return bitmap;   }   /**    * 旋轉圖片擺正顯示    * @param srcPath    * @param bitmap    * @return    */   public static Bitmap rotateBitmapByExif(String srcPath, Bitmap bitmap) {     ExifInterface exif;     Bitmap newBitmap = null;     try {       exif = new ExifInterface(srcPath);       if (exif != null) { // 讀取圖片中相機方向信息         int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,             ExifInterface.ORIENTATION_NORMAL);         int digree = 0;         switch (ori) {         case ExifInterface.ORIENTATION_ROTATE_90:           digree = 90;           break;         case ExifInterface.ORIENTATION_ROTATE_180:           digree = 180;           break;         case ExifInterface.ORIENTATION_ROTATE_270:           digree = 270;           break;         }         if (digree != 0) {           Matrix m = new Matrix();           m.postRotate(digree);           newBitmap = Bitmap.createBitmap(bitmap, 0, 0,               bitmap.getWidth(), bitmap.getHeight(), m, true);           recycleBitmap(bitmap);           return newBitmap;         }       }     } catch (IOException e) {       e.printStackTrace();     }     return bitmap;   }   /**    * 回收位圖對象    * @param bitmap    */   public static void recycleBitmap(Bitmap bitmap) {     if (bitmap != null && !bitmap.isRecycled()) {       bitmap.recycle();       System.gc();       bitmap = null;     }   } }

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色免费网站观看 | 91 在线观看| 欧美激情第1页 | 国产成人在线播放 | 亚洲精彩视频 | 97人人干 | 日韩一区二区在线免费观看 | 一本色道久久99精品综合 | 亚洲欧美日韩精品 | 国产在线一区二区三区视频 | 精品国产欧美一区二区三区成人 | 日本三级黄色录像 | 一级毛片中国 | 色av综合 | 99久久网站 | 久久久久久久香蕉 | 日韩精品一区二区三区在线观看 | 成年免费视频黄网站在线观看 | 午夜精品久久久久久久白皮肤 | 日韩在线欧美 | 欧美精品一区二区三区蜜臀 | 亚洲在线视频 | 一本色道久久综合亚洲精品按摩 | 精品在线一区二区 | 国产精品毛片久久久久久久 | 久久久精品高清 | 男女羞羞视频免费观看 | 九九精品视频在线 | 国产一区二区在线视频观看 | 日本三级黄色录像 | 91精品国产乱码久久久久久 | 在线播放三级 | 国产成人一区二区三区 | 成人国产在线观看 | 国产精品呻吟久久av图片 | 国产极品免费 | 91精品秘密在线观看 | 极品av| 草草在线观看 | 中文字幕91 | 国产美女在线精品免费 |