首先該文章是總結, 不是原創, 是通過看網上其他大神的文章和自己的一些實踐總結出來的.
一.圖片的存在形式
1.文件形式(即以二進制形式存在於硬盤上)
2.流的形式(即以二進制形式存在於內存中)
3.Bitmap形式
2.流的形式(即以二進制形式存在於內存中)
3.Bitmap形式
這三種形式的區別: 文件形式和流的形式對圖片體積大小並沒有影響,也就是說,如果你手機SD卡上的如果是100K,那麼通過流的形式讀到內存中,也一定是佔100K的內存,注意是流的形式,不是Bitmap的形式,當圖片以Bitmap的形式存在時,其佔用的內存會瞬間變大, 我試過500K文件形式的圖片加載到內存,以Bitmap形式存在時,佔用內存將近10M,當然這個增大的倍數並不是固定的
檢測圖片三種形式大小的方法:
文件形式: file.length()
流的形式: 講圖片文件讀到內存輸入流中,看它的byte數
Bitmap: bitmap.getByteCount()
二.常見的壓縮方式
1. 將圖片保存到本地時進行壓縮, 即將圖片從Bitmap形式變爲File形式時進行壓縮,
特點是: File形式的圖片確實被壓縮了, 但是當你重新讀取壓縮後的file爲 Bitmap是,它佔用的內存並沒有改變
- public static void compressBmpToFile(Bitmap bmp,File file){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int options = 80;//個人喜歡從80開始,
- bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
- }
- try {
- FileOutputStream fos = new FileOutputStream(file);
- fos.write(baos.toByteArray());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
方法說明: 該方法是壓縮圖片的質量, 注意它不會減少圖片的像素,比方說, 你的圖片是300K的, 1280*700像素的, 經過該方法壓縮後, File形式的圖片是在100以下, 以方便上傳服務器, 但是你BitmapFactory.decodeFile到內存中,變成Bitmap時,它的像素仍然是1280*700, 計算圖片像素的方法是 bitmap.getWidth()和bitmap.getHeight(), 圖片是由像素組成的,
每個像素又包含什麼呢? 熟悉PS的人知道, 圖片是有色相,明度和飽和度構成的.
該方法的官方文檔也解釋說, 它會讓圖片重新構造, 但是有可能圖像的位深(即色深)和每個像素的透明度會變化,JPEG onlysupports opaque(不透明), 也就是說以jpeg格式壓縮後, 原來圖片中透明的元素將消失.所以這種格式很可能造成失真
因爲: bitmap.getByteCount() 是計算它的像素所佔用的內存, 請看官方解釋: Returns the number of bytes used to store this bitmap's
pixels.
特點: 通過設置採樣率, 減少圖片的像素, 達到對內存中的Bitmap進行壓縮
先看一個方法: 該方法是對內存中的Bitmap進行質量上的壓縮, 由上面的理論可以得出該方法是無效的, 而且也是沒有必要的,因爲你已經將它讀到內存中了,再壓縮多此一舉, 儘管在獲取系統相冊圖片時,某些手機會直接返回一個Bitmap,但是這種情況下, 返回的Bitmap都是經過壓縮的,
它不可能直接返回一個原聲的Bitmap形式的圖片, 後果可想而知
- private Bitmap compressBmpFromBmp(Bitmap image) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int options = 100;
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
- private Bitmap compressImageFromFile(String srcPath) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = true;//只讀邊,不讀內容
- Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
- float hh = 800f;//
- float ww = 480f;//
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;//設置採樣率
- newOpts.inPreferredConfig = Config.ARGB_8888;//該模式是默認的,可不設
- newOpts.inPurgeable = true;// 同時設置纔會有效
- newOpts.inInputShareable = true;//。當系統內存不夠時候圖片自動被回收
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- // return compressBmpFromBmp(bitmap);//原來的方法調用了這個方法企圖進行二次壓縮
- //其實是無效的,大家儘管嘗試
- return bitmap;
- }
方法說明: 該方法就是對Bitmap形式的圖片進行壓縮, 也就是通過設置採樣率, 減少Bitmap的像素, 從而減少了它所佔用的內存