【问题标题】:Android - Camera intent low bitmap quality [duplicate]Android - 相机意图低位图质量[重复]
【发布时间】:2016-01-05 10:20:37
【问题描述】:

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

编辑:图像未保存在任何地方。接受后在facebook上发布(snapchat)

【问题讨论】:

  • 不是通过拍摄照片时传回给您的额外信息。但是您应该能够为要保存的图像提供路径(包括不冲突的名称)。这样一来,您就知道在拍完照片后在哪里寻找照片。

标签: android bitmap android-camera-intent


【解决方案1】:

不要使用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();
                }
            }
        }
    }

【讨论】:

  • 数据返回null
  • @KyloRen 你需要添加权限
【解决方案2】:

在您的 cameraIntent 中,您需要指定相机 Activity 以全分辨率保存图片时的 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);

就是这样。

【讨论】:

    【解决方案3】:

    您必须将图像保存在存储中才能获得完整尺寸的图像。检查这个:http://developer.android.com/training/camera/photobasics.html#TaskPath

    【讨论】:

    • 好评论!感谢您指出!注意:最好更改答案并包含一些代码示例,而不是仅添加文档参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    相关资源
    最近更新 更多