【问题标题】:Camera Intent show blurred image preview after take a picture for some devices在某些设备上拍照后,Camera Intent 显示模糊的图像预览
【发布时间】:2019-04-15 22:30:23
【问题描述】:

我在使用相机意图拍照时遇到了一些问题。在我和我朋友的一些设备中没问题,但不知何故它在三星设备中显示了一个小错误(大部分)。当我拍照时,捕捉到的图像的预览看起来很模糊。我已经在互联网上搜索了所有内容,但没有找到答案。

我唯一不明白的是,我的代码在我的设备测试中运行良好,顺便说一句,我使用的是谷歌代码,所以我很确定在所有设备上都可以。现在我很困惑,不知道该怎么办。

这是我的代码:

我的意图方法:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.codelabs.refillmybottle.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, 1);
        }
    }
}

onActivityResult :

if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            File imgFile = new  File(mCurrentPhotoPath);
            Log.d(TAG, "imgfile size : " + imgFile.length());
            if(imgFile.exists()){
                Uri bp = Uri.fromFile(imgFile);
                Bitmap mybitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                Bitmap bitmap = decodeUri(bp,300);
                chipdata++;
                encodedeImageData[chipdata - 1] = getEncoded64ImageStringFromBitmap(bitmap);
                checkChipData();
                checkData();
            }
}

decodeUri 是一种将 Uri 解码为位图并调整其大小的方法。

我真的希望任何人都可以回答我的错误,因为我的老板对我很生气,我很困惑 :D。谢谢。

【问题讨论】:

  • 找到解决方案了吗?

标签: android android-intent camera blur


【解决方案1】:

相机拍照的最佳代码

private int REQUEST_CAMERA = 0;
String realPath;
private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
  }

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

    if (resultCode == Activity.RESULT_OK && data != null) {


           if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);


    }

private void onCaptureImageResult(Intent data) {

    Bitmap photo = (Bitmap) data.getExtras().get("data");
    binding.profileImage.setImageBitmap(photo);


    // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
    Uri tempUri = getImageUri(getApplicationContext(), photo);

    // CALL THIS METHOD TO GET THE ACTUAL PATH


}

 private Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    realPath =MediaStore.Images.Media.insertImage(inContext.getContentResolver(), 
        inImage, "Title", null);
    return Uri.parse(realPath);
}

【讨论】:

  • 谢谢你的回答,'data.getExtras().get("data")' 不是只给你一个缩略图吗?
  • 位图 inImage ; ByteArrayOutputStream 字节 = 新的 ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
猜你喜欢
  • 2013-12-16
  • 2020-01-03
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多