2012-03-14 65 views
12

我正在使用相機意圖捕捉圖片。這是我的代碼,它的偉大工程:相機意圖返回小圖片

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

我onActivityResult看起來是這樣的:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap photo = (Bitmap) extras.get("data"); 
    } 
} 

的問題是,雖然由相機拍攝的照片是480 * 800(我使用HTC Desire),返回的位圖僅爲194 * 324!

任何想法爲什麼發生這種情況,以及如何解決它?

謝謝!

回答

0

如果我們想從 攝像機產生高質量圖像我們不會「T有一個選擇,但它保存到文件,然後使用:

  1. 所以首先像以前一樣,我們需要創建一個靜態的INT,這將是我們的requestCode:

公共靜態final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;

  • 接下來,我們再次觸發的意圖來啓動活動爲結果:
  • 意圖意圖=新意圖( 「android.media.action.IMAGE_CAPTURE」 );文件 file = new File(Environment.getExternalStorageDirectory()+ File.separator + 「image.jpg」); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

    在這裏,我們實際上是傳遞一個URI作爲額外的意圖,以保存圖像。

  • 最後,我們將接收的結果在onActivityResult:

     protected void onActivityResult(int requestCode, int resultCode, Intent data) 
         { 
          //Check that request code matches ours: 
          if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) 
          { 
           //Get our saved file into a bitmap object: 
    
           File file = new File(Environment.getExternalStorageDirectory()+File.separator + 
         "image.jpg"); 
           Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700); 
          } 
        } 
    
  • decodeSampledBitmapFromFile方法是:

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
    { // BEST QUALITY MATCH 
    
        //First decode with inJustDecodeBounds=true to check dimensions 
        final BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(path, options); 
    
        // Calculate inSampleSize, Raw height and width of image 
        final int height = options.outHeight; 
        final int width = options.outWidth; 
        options.inPreferredConfig = Bitmap.Config.RGB_565; 
        int inSampleSize = 1; 
    
        if (height > reqHeight) 
        { 
         inSampleSize = Math.round((float)height/(float)reqHeight); 
        } 
        int expectedWidth = width/inSampleSize; 
    
        if (expectedWidth > reqWidth) 
        { 
         //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
         inSampleSize = Math.round((float)width/(float)reqWidth); 
        } 
    
        options.inSampleSize = inSampleSize; 
    
        // Decode bitmap with inSampleSize set 
        options.inJustDecodeBounds = false; 
    
        return BitmapFactory.decodeFile(path, options); 
    } 
    

    的培訓相關相機權限添加到清單文件:

    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    希望它會幫助任何人解決問題的方式。因爲它爲我工作100%。