2013-05-09 59 views
4

我在發佈前真的在整個網上搜索過。我的問題是,我無法調整位圖的大小而不會丟失圖像的質量(質量非常差和像素化)。Android:在不丟失質量的情況下調整位圖大小

我從相機取得位圖,然後我必須縮小它的大小,這樣我就可以更快地將它上傳到服務器。

這是做採樣

public Bitmap resizeBitmap(Bitmap bitmap){ 
     Canvas canvas = new Canvas(); 
     Bitmap resizedBitmap = null; 
     if (bitmap !=null) { 
       int h = bitmap.getHeight(); 
       int w = bitmap.getWidth(); 
       int newWidth=0; 
       int newHeight=0; 

       if(h>w){ 
        newWidth = 600; 
        newHeight = 800; 
       } 

       if(w>h){ 
        newWidth = 800; 
        newHeight = 600; 
        } 

       float scaleWidth = ((float) newWidth)/w; 
       float scaleHeight = ((float) newHeight)/h; 



       Matrix matrix = new Matrix(); 
       // resize the bit map 
       matrix.preScale(scaleWidth, scaleHeight); 

       resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); 



       Paint paint = new Paint(); 
       paint.setAntiAlias(true); 
       paint.setFilterBitmap(true); 
       paint.setDither(true); 

       canvas.drawBitmap(resizedBitmap, matrix, paint); 



      } 


     return resizedBitmap; 

的功能,這就是我從活動結果獲得圖像

protected void onActivityResult(int requestCode, int resultCode, 
      Intent data) { 
     if(resultCode != RESULT_CANCELED){ 

     if (requestCode == 0) { 
      if (resultCode == RESULT_OK) { 


        getContentResolver().notifyChange(mImageUri, null); 
        ContentResolver cr = this.getContentResolver(); 

        try 
        { 
         b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri); 
         Log.d("foto", Integer.toString(b.getWidth())); 
         Log.d("foto", Integer.toString(b.getHeight())); 
         addPhoto.setImageBitmap(b); 

        } 
        catch (Exception e) 
        { 
         Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); 
         Log.d("TAG", "Failed to load", e); 
        } 



      } 
     } 

我開始認爲最好的方式來獲得小圖片尺寸是設置相機分辨率。任何人都可以幫忙嗎?

回答

1

嘗試Bitmap.createScaledBitmap。 它也有一個選項來過濾源。

+2

謝謝...但我已經試過: resizedBitmap = Bitmap.createScaledBitmap(bitmap,newWidth,newHeight,true); 但它是一樣的...仍然塊狀和像素化 – Vahn84 2013-05-09 15:51:23

+0

我明白了。我記住的最後一個提示是你檢查BitmapFactory.Options類,初始化和解碼你的位圖時使用質量相關的選項。這裏的API鏈接:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html – Mena 2013-05-09 17:47:31

+0

我已經達到了至少可以接受的解決從位圖uri bitmap.factory選項的位圖。但它仍然不是我想的質量。 Facebook和G +如何處理照片上傳?我看到他們調整了您上傳的所有圖片。他們如何做到這一點,而不會丟失一個質量像素? – Vahn84 2013-05-10 07:23:23

2

請嘗試下面提到的調整位圖大小的代碼。

public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) { 
     int width = bmp.getWidth(); 
     int height = bmp.getHeight(); 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     // CREATE A MATRIX FOR THE MANIPULATION 
     Matrix matrix = new Matrix(); 
     // RESIZE THE BIT MAP 
     matrix.postScale(scaleWidth, scaleHeight); 

     // "RECREATE" THE NEW BITMAP 
     Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false); 
     return newBitmap ; 
    } 

我用這個代碼來縮小我的位圖,它的質量,以及.. ..是可以接受的。

希望這會有所幫助。

+1

不,嘗試,但它是相同的結果像以前一樣 – Vahn84 2013-05-09 16:03:13

+0

沒有改進過的位圖的品質,得到相同的結果,以前 – sumeet 2018-01-03 06:53:42

6

良好縮小算法(如不近鄰)由僅2步驟(加上用於輸入/輸出圖像作物確切矩形的計算):

  1. 縮減使用BitmapFactory.Options :: inSampleSize- > BitmapFactory.decodeResource()儘可能接近到你所需要的分辨率,但不低於其
  2. 得到精確的分辨率,通過縮減使用帆布:: drawBitmap()一點點

下面是詳細的解釋SonyMobile如何解決這個任務:http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

這裏是SonyMobile規模utils的源代碼:http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/

+2

這應該是公認的答案。剛剛實現了鏈接文章中描述的內容,結果明顯好於使用簡單的createScaledBitmap,並且在縮小時使用得更少。 結果仍然不如降尺度時的好,例如,在Photoshop中。 – 2015-04-30 16:06:23

+0

我已經嘗試了FIT方法,它與使用默認Bitmap.createScaledBitmap的質量完全相同。可能會更快/更少的內存消耗,但質量是相同的。 – lxknvlk 2017-01-04 22:36:35

+0

upd - 只是檢查時間,兩種方法使用相同的時間,在我的情況下,這些是2-3毫秒。我沒有使用他們的解碼方法,因爲我有一個準備好的位圖作爲輸入,所以也沒有改變內存使用情況。 – lxknvlk 2017-01-04 22:48:02

相關問題