2013-07-18 156 views
3

我已經看過創建圖像的縮小大小位圖的許多不同方法,但它們中的任何一個都不能正常工作/我需要不同的東西。將圖像轉換爲位圖並減小其大小

這是有點難以解釋:-)

我需要的是,保持圖像的比率的位圖,但是是小於特定大小 - 例如1MB或在像素尺寸的當量(如這個位圖需要作爲putExtra()添加到intent中)。

問題我有這麼遠:

大部分我已經看了創建位圖的縮放版本的方法。所以:圖像 - >位圖1(未縮放) - >位圖2(縮放)。但是,如果圖像的分辨率非常高,則不會縮小。我認爲解決方案是創建一個精確大小的位圖,以便任何分辨率都可以降低。

但是,這種方法的副作用是已經小於所需大小的圖像將被調整大小(或調整大小將不起作用?)。因此,需要有一個「if」來檢查圖像是否可以轉換爲位圖而不調整大小。

我不知道如何去做這個,所以任何幫助都非常感謝! :-)

這是我使用的那一刻是什麼(它不會做我想做的事情):

// This is called when an image is picked from the gallery 
@Override 
    public void onActivityResult(int requestCode, int resultCode, 
      Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     switch (requestCode) { 
     case 0: 
      if (resultCode == Activity.RESULT_OK) { 
       selectedImage = imageReturnedIntent.getData(); 
       viewImage = imageReturnedIntent.getData(); 

       try { 
        decodeUri(selectedImage); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       iv_preview.setImageBitmap(mImageBitmap); 
      } 
      break; // The rest is unnecessary 

這是當前縮放大小的部分:

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException { 


     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; // 
     BitmapFactory.decodeStream(getActivity().getContentResolver() 
       .openInputStream(selectedImage), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 260; // Is this kilobites? 306 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
       break; 
      } 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 


     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     o2.inScaled = false; // Better quality? 
     mImageBitmap = BitmapFactory.decodeStream(getActivity() 
       .getContentResolver().openInputStream(selectedImage), null, o2); 
     return BitmapFactory.decodeStream(getActivity().getContentResolver() 
       .openInputStream(selectedImage), null, o2); 

    } 

如果有什麼需要解釋更多請說。

謝謝

+0

你的代碼,AFAIU,是錯誤的。保存寬高比的邏輯在哪裏? –

+0

檢查此:http://stackoverflow.com/questions/14710386/how-to-resize-bitmap-to-maximum-available-size?rq=1 你將能夠與它相關。 –

+0

@ManojAwasthi這段代碼是錯誤的 - 它沒有做我想做的事情,但它保持高寬比。並感謝您的鏈接 – RiThBo

回答

3

如何撥打:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     pho1.setImageBitmap(decodeSampledBitmapFromResource(picturePath, 
       80, 60)); 

方法:

public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      // Calculate ratios of height and width to requested height and 
      // width 
      final int heightRatio = Math.round((float) height 
        /(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 

      // Choose the smallest ratio as inSampleSize value, this will 
      // guarantee 
      // a final image with both dimensions larger than or equal to the 
      // requested height and width. 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 

     return inSampleSize; 
    } 

    public static Bitmap decodeSampledBitmapFromResource(String path, 
      int reqWidth, int reqHeight) { 
     Log.d("path", path); 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(path, options); 
    } 
+0

仍試圖讓它工作 – RiThBo

+0

非常感謝您的代碼,但我調用calculateInSampleSize(null, 100,100)時得到nullpointerexception;從我的onActivityResult()。我假設它爲空,我寫了,但我不知道該怎麼改它 – RiThBo

+1

你使用它略顯不對。 calculateInSampleSize是一種支持方法,你不需要在任何地方調用它。使用decodeSampledBitmapFromResource(pathToImage,100,100)。 – Yarh

相關問題