Android - 相机意图低位图质量

8
使用Android相机Intent拍照时,我得到了一个低质量的位图图像。我想知道是否有可能使这个图像具有良好的质量。 我在谷歌上搜索了一些信息,我认为我必须使用'EXTRA_OUTPUT'(http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE)。 我很困惑,因为我不知道该把它放在哪里,也不知道它是否会解决我的问题。 按下btnTakePhoto后,我更改布局并打开Android相机。位图显示在第二个布局中(低质量)。 这是我的代码:
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends AppCompatActivity {
    ImageButton btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUEST = 1313;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        btnTakePhoto = (ImageButton) findViewById(R.id.buttonFoto);
        imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);


    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAM_REQUEST){
            setContentView(R.layout.share);
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);
            imgTakenPhoto.setImageBitmap(thumbnail);
        }
    }

    class btnTakePhotoClicker implements Button.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAM_REQUEST);
        }
    }

    //CameraShare layout -- back button - Go back to first layout
    public void ibBackToPhotograph(View v) {
        setContentView(R.layout.camera);
        btnTakePhoto = (ImageButton) findViewById(R.id.buttonFoto);
        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }
}

编辑:该图片不会被保存在任何地方。它的目的是在您接受后(Snapchat)发布到Facebook上。

不要通过拍照时传回给你的额外信息来保存图片。但是,你应该能够提供一个保存图像的路径(包括一个不冲突的名称)。这样,你就知道在拍照后在哪里查找照片了。 - Paulo Avelar
3个回答

11

不要使用bitmap,请改用uri

 ImageView imageView;
 Uri image;
 String mCameraFileName;

 private void cameraIntent() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("-mm-ss");

        String newPicFile = df.format(date) + ".jpg";
        String outPath = "/sdcard/" + newPicFile;
        File outFile = new File(outPath);

        mCameraFileName = outFile.toString();
        Uri outuri = Uri.fromFile(outFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
        startActivityForResult(intent, 2);
    }

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 2) {
                if (data != null) {
                    image = data.getData();
                    imageView.setImageURI(image);
                    imageView.setVisibility(View.VISIBLE);
                }
                if (image == null && mCameraFileName != null) {
                    image = Uri.fromFile(new File(mCameraFileName));
                    imageView.setImageURI(image);
                    imageView.setVisibility(View.VISIBLE);
                }
                File file = new File(mCameraFileName);
                if (!file.exists()) {
                    file.mkdir();
                }
            }
        }
    }

1
数据返回为空。 - Kylo Ren
1
@KyloRen,你需要添加权限。 - John Joe

8

在您的cameraIntent中,当相机活动以完整分辨率保存图片时,您需要指定uri:

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);

如果您在onActivityResult中没有指定只接收缩略图,那么您需要指定并从uri中读取图像。

因此,在您的onActivityResult中,您应该执行以下操作:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(capturedImageUri, options);

就是这样。


0

1
好的评论!感谢您指出这一点!请注意:如果您改变答案并包含一些代码示例而不仅仅是添加文档引用,那会更好。 - Filipe Brito

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接