2017-08-11 51 views
1

在Android應用程序中拍攝照片時,顯示模糊/縮小的圖像,我意識到這將來自位圖..... data.getExtras()。得到(「數據」)代碼,但我已經改變的任何東西都沒有工作,顯示的圖像仍然模糊。動作圖像捕捉在觀看時造成圖像模糊

我怎樣才能讓它顯示完整的資源圖片?

相機意向代碼

private void cameraIntent() 
{ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 
} 

在活動代碼

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

    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_CODE_GALLERY) 
      onSelectFromGalleryResult(data); 
     else if (requestCode == REQUEST_IMAGE_CAPTURE) 
      onCaptureImageResult(data); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

終於oncaptureImageResult - 在那裏,我相信我錯了模糊圖像

private void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".jpg"); 

    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    imageView.setImageBitmap(thumbnail); 
} 

回答

0

改變這條線。

thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
+0

這似乎並不很不幸改變什麼,一個電話 –

0

試試這個,這將返回精確的圖像,而不降低質量..

Uri u; 

private void cameraIntent() { 
     long time = System.currentTimeMillis(); 
     String stvalue = Long.toString(time); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
     File output = new File(dir, stvalue); 
     u = Uri.fromFile(output); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, u); 
     startActivityForResult(intent, REQUEST_CAMERA); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putParcelable("file_uri", u); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    u = savedInstanceState.getParcelable("file_uri"); 
} 

然後在onActivityResult

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

    if (resultCode == RESULT_OK) {   
     camerastore(u); 
     break; 
    } else { 
     utils.Toast("Error in importing file"); 
    } 
} 


void camerastore(Uri u) { 
    Bitmap bitmap = null; 
    File pictureFile = new File("yourpath/image.jpg"); 

    try { 
     bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), u); 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos) 
     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
+0

我可以問哪裏camerapath已經從上觀看時,它不會改變任何決議的? –

+0

我編輯過了,我剛剛從我的項目中複製過來,我面臨類似的問題..這段代碼對我很好用。可能有一些不需要的代碼,不要打擾它,我只是張貼瞭解你的理解 –

0

您正在壓縮的使用圖像太多JPEG通過在質量中指定90來壓縮10%額外圖像質量損失的格式。基本上你做了有損壓縮。要做一個無損壓縮,你應該選擇100%質量的PNG壓縮格式。你可以這樣做:

Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 

而且還有一個建議。在File Provider的幫助下,在cameraIntent()方法本身中創建圖像文件,在onActivity的幫助下僅顯示來自文件的圖像。它提供了很好的質量。

2

嘗試了WeakReference,這將返回清晰的圖像,而不降低質量

WeakReference<Bitmap> result1 = new WeakReference<Bitmap>(Bitmap.createScaledBitmap(src, 
      src.getWidth(), src.getHeight(), false).copy(
      Bitmap.Config.RGB_565, true)); 
Bitmap bm=result1.get(); //Set this bitmap to any imageview that you want to display your image. 

在onCaptureImageResult把上面的代碼()方法用於離。

private void onCaptureImageResult(Intent data) { 
Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
WeakReference<Bitmap> result1 = new WeakReference<Bitmap>(Bitmap.createScaledBitmap(thumbnail, 
     thumbnail.getWidth(), thumbnail.getHeight(), false).copy(
     Bitmap.Config.RGB_565, true)); 
    Bitmap bm=result1.get(); 
    imageView.setImageBitmap(bm);} 
+0

關於我的代碼在哪裏坐? –

+0

onCaptureImageResult()放在那裏 –

+0

以及我有更新我的代碼,讓我知道如果它不工作 –