Android中的相机捕捉图像不清晰(模糊)

时间:2015-12-11 18:05:17

标签: android image bitmapimage android-camera-intent blurry

我遇到了相机捕捉Image.its设置模糊的问题。

我搜索了很多,但我无法获得解决方案

我不知道如何解决问题

这是我用于相机拍摄图像的代码

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

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 0, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


  img1.setImageBitmap(thumbnail);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    Uri selectedImageUri = data.getData();
    String[] projection = {MediaStore.MediaColumns.DATA};
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
            null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();

    String selectedImagePath = cursor.getString(column_index);

    Bitmap bm;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath, options);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(selectedImagePath, options);

    img1.setImageBitmap(bm);

}

图像集在Imageview中设置良好,但它只能与CameraCapture Imageview一起发生 Camera Capture Image is not clear(Blurry) in Android

帮我解决这个问题。

提前致谢

3 个答案:

答案 0 :(得分:2)

你的问题在这里

thumbnail.compress(Bitmap.CompressFormat.JPEG, 0, bytes);

.compress()方法中的第二个参数是质量,可以在0..100范围内。 您已将其设置为0 - >最大压缩。

将值更改为更高的值。

答案 1 :(得分:1)

你正在做的是从意图数据中获取缩略图,这就是它模糊的原因

试试这个:     文件mImageFile是您要存储摄像机图像文件的路径。

    mImageFile= new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "temp.png"); 

Uri tempURI = Uri.fromFile(mImageFile);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
activity.startActivityForResult(i, CHOOSE_CAMERA_RESULT);


then in your onActivityResult

@Override
public void onActivityResultRAW(int requestCode, int resultCode, Intent data)
{
    super.onActivityResultRAW(requestCode, resultCode, data);
    switch(requestCode)
    {
    case CHOOSE_CAMERA_RESULT:
    {
        if (resultCode == RESULT_OK)
        {
            // here you image has been save to the path mImageFile.
            Log.d("ImagePath", "Image saved to path : " + mImageFile.getAbsoultePath());
        }
    }
    break;
    }
}

答案 2 :(得分:0)

当您尝试从onActivityResult中的data.getExtras对象获取位图时,它会返回低质量的位图,下面的解决方案是完整的后续Android文档。不要忘记在清单中写许可。

lateinit var currentPhotoPath: String
var photoFile: File? = null
private fun takePhotoFromCamera() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        .also { takePictureIntent ->
            photoFile = try {
                createImageFile()
            } catch (ex: IOException) {
                null
            }

            photoFile?.also {
                fileUri = FileProvider.getUriForFile(
                    this, "com.example.fileprovider",
                    it
                )
                AppLogger.d("asad_fileUri", "$fileUri")
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri)
                startActivityForResult(takePictureIntent, CommonUtils.CAMERA)
            }
        }

}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    try {
        if (requestCode == CommonUtils.CAMERA) {
            galleryAddPic()
            setPic()?.let {
                fileUri = getImageUri(this, it)
            }
            postImagesAdapter.addItems(
                listOf(PostImagesModel(fileUri.toString(), false, fileUri)), false
            )

        } catch (e: Exception) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
            .show()
    }
    super.onActivityResult(requestCode, resultCode, data)
}

// File path xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images"
        path="/" />
</paths>

//AndroidManifest
<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.lamposcan.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_path"></meta-data>
    </provider>

Take Photos