Why does a blurry image appear in a simple android

2019-02-20 04:03发布

问题:

I tried to make a simple camera app that captures an image and views the image in an imageview: I tried this code in MainActivity:

 ImageView myImageView;

public void myButtonCamera (View view){
    Intent cameraIntent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 10);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if (resultCode == RESULT_OK){
        if (requestCode == 10){
            Bitmap cameraCapture;
            cameraCapture = (Bitmap)data.getExtras().get("data");
            myImageView.setImageBitmap(cameraCapture);
        }
    }

}

The app works and captures the image but it blurs the image after viewing it in the ImageView. I tried to put the height and width attributes of ImageView to wrap_content and after testing the app, I noticed that the captured image is very small in resolution! Because the size of viewed image was very small!

回答1:

I noticed that the captured image is very small in resolution!

That is what your code asks for. Quoting the documentation for ACTION_IMAGE_CAPTURE:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.

If you want a full-resolution image, use EXTRA_OUTPUT to indicate a file on external storage where you want the camera app to write the full-resolution image:

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    output=new File(dir, "CameraContentDemo.jpeg");
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

(from this sample project)