2015-04-04 174 views
0

我試圖壓縮圖像並將壓縮和原始圖像的大小輸出到Logcat(使用Bitmap.getAllocationByteCount())。但是,儘管壓縮圖像的質量有明顯變化,但壓縮後的原始圖像大小始終相同。我正在使用Picasso SDK從資源中讀取圖像,並最終將由Target類中的回調返回的位圖加載到ImageView中。Android - 壓縮和解壓縮圖像的大小相同

這裏是我的代碼:

public class MainActivity extends ActionBarActivity { 

ImageView imageView1; 
private Target target; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    imageView1 = (ImageView)findViewById(R.id.image_view_1); 

    target = new Target(){ 

     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 

      Log.v("BITMAP BEFORE",Integer.valueOf(bitmap.getAllocationByteCount()).toString()); 

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

      bitmap.compress(Bitmap.CompressFormat.JPEG,0,outputStream); 

      Bitmap decodedMap = BitmapFactory.decodeStream(new ByteArrayInputStream(outputStream.toByteArray())); 

      imageView1.setImageBitmap(decodedMap); 

      Log.v("BITMAP AFTER",Integer.valueOf(decodedMap.getAllocationByteCount()).toString()); 

     } 

     @Override 
     public void onBitmapFailed(Drawable errorDrawable) { 

      Log.v("ON BITMAP FAILED", "ON BITMAP FAILED"); 
     } 

     @Override 
     public void onPrepareLoad(Drawable placeHolderDrawable) { 

      Log.v("ON PREPARE LOAD","ON PREPARE LOAD"); 
     } 
    }; 

    loadImage(); 
} 


private void loadImage(){ 

    Picasso.with(getApplicationContext()).load(R.drawable.smiley).into(target); 
} 
} 

我試圖壓縮的JPEG和PNG以0壓縮因子,不過尺寸都是一樣的。我究竟做錯了什麼 ?一個解釋或甚至解決方案將不勝感激。我試圖在加載到ImageView之前減小位圖的大小。提前致謝。

編輯: 我試着用getByteCount()getRowBytes() * getHeight()代替getAllocationByteCount(),結果總是一樣的。

回答

2

但是,儘管壓縮圖像的質量有明顯變化,但壓縮後的原始圖像大小始終相同。

Bitmap的RAM大小無關也與圖像是否被壓縮的圖像質量。它僅取決於分辨率(寬度,高度)和位深度。 getAllocationByteCount()返回Bitmap的RAM中的大小。

壓縮和圖像質量影響位圖圖像的磁盤上的大小