为什么简单的 android 相机应用程序中会出现模糊图像?

Why does a blurry image appear in a simple android camera app?

我尝试制作一个简单的相机应用程序来捕捉图像并在图像视图中查看图像: 我在 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);
        }
    }

}

该应用程序可以运行并捕获图像,但在 ImageView 中查看图像后图像会变得模糊。 我尝试将 ImageView 的 height 和 width 属性设置为 wrap_content 并且在测试应用程序后,我发现捕获的图像分辨率非常小!因为看到的图片太小了!

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

这就是您的代码所要求的。引用 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.

如果您需要全分辨率图像,请使用 EXTRA_OUTPUT 指示您希望相机应用在其中写入全分辨率图像的外部存储文件:

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

(来自 this sample project