2012-03-14 33 views
12

Tôi đang sử dụng ý định của máy ảnh để chụp ảnh. Đây là mã của tôi và nó hoạt động tuyệt vời:Mục đích của máy ảnh trả về ảnh nhỏ

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 của tôi trông như thế này:

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

Vấn đề là, rằng trong khi ảnh được chụp bởi máy ảnh là 480 * 800 (Tôi đang sử dụng HTC Desire), bitmap được trả về chỉ là 194 * 324!

Bất kỳ ý tưởng nào tại sao điều đó xảy ra và cách giải quyết?

Cảm ơn!

Trả lời

10

đọc câu trả lời này để có được kích thước hình ảnh đầy đủ How to capture an image and store it with the native Android Camera


khi bạn sử dụng đọc Bitmap từ thêm, bạn sẽ nhận được Thumbnail của hình ảnh

+0

tôi đã theo dõi nó và tôi không thể lưu hình ảnh vào thư viện. nhưng hình ảnh được lưu trữ cũng với hình ảnh chất lượng kém .. làm thế nào để có được chất lượng tốt? – Kasnady

0

Nếu chúng tôi muốn tạo ra hình ảnh chất lượng cao từ máy ảnh Hoạt động chúng tôi không 'T có một lựa chọn nào khác để lưu nó vào tập tin và sau đó sử dụng:

  1. Vì vậy Trước hết như trước đây chúng ta cần phải tạo ra một int tĩnh rằng sẽ requestCode của chúng tôi:

public static cuối cùng int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;

  1. Tiếp theo chúng ta bắn một lần nữa mục đích để bắt đầu Hoạt động cho kết quả:

ý Ý định = Ý định mới ("android.media.action.IMAGE_CAPTURE"); Tệp tệp = new Tệp (Environment.getExternalStorageDirectory() + File.separator + "image.jpg"); intent.putExtra (MediaStore.EXTRA_OUTPUT, Uri.fromFile (tệp)); startActivityForResult (ý định, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

Ở đây, chúng tôi thực sự chuyển URI như một mục đích bổ sung để lưu hình ảnh.

  1. Cuối cùng chúng ta sẽ nhận được kết quả trong 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); 
          } 
        } 
    

phương pháp decodeSampledBitmapFromFile là:

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); 
} 

thêm các điều khoản camera có liên quan đến tệp kê khai:

<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" /> 

Hy vọng nó sẽ giúp bất cứ ai làm thế nào là serching cho các giải pháp. Vì nó làm việc 100% cho tôi.

Các vấn đề liên quan