【问题标题】:Action Image Capture causing blurred images when viewingAction Image Capture 在查看时导致图像模糊
【发布时间】:2017-08-11 10:02:33
【问题描述】:

当我在 Android 应用程序中拍照时,显示模糊/缩小的图像,我确实意识到这将来自位图 .....data.getExtras().get ("data") 代码但是我更改的其他任何内容都不起作用,显示的图像仍然模糊。

如何让它显示完整的分辨率图片?

相机意图代码

private void cameraIntent()
{
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

关于活动代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CODE_GALLERY)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_IMAGE_CAPTURE)
            onCaptureImageResult(data);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

最后是 oncaptureImageResult - 我相信我会因为模糊图像而出错

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, 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();
    }

    imageView.setImageBitmap(thumbnail);
}

【问题讨论】:

    标签: java android android-camera android-camera-intent


    【解决方案1】:

    试试 WeakReference,这将返回清晰的图像而不会降低质量

      WeakReference<Bitmap> result1 = new WeakReference<Bitmap>(Bitmap.createScaledBitmap(src,
                src.getWidth(), src.getHeight(), false).copy(
                Bitmap.Config.RGB_565, true));
    Bitmap bm=result1.get(); //Set this bitmap to any imageview that you want to display your image.
    

    将上述代码放在 onCaptureImageResult() 方法中,例如

    private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    WeakReference<Bitmap> result1 = new WeakReference<Bitmap>(Bitmap.createScaledBitmap(thumbnail,
            thumbnail.getWidth(), thumbnail.getHeight(), false).copy(
            Bitmap.Config.RGB_565, true));
       Bitmap bm=result1.get();
       imageView.setImageBitmap(bm);}
    

    【讨论】:

    • onCaptureImageResult() 放在那里
    • 我已经更新了我的代码,如果它不起作用,请告诉我
    【解决方案2】:

    试试这个,这将返回准确的图像而不会降低质量..

    Uri u;
    
     private void cameraIntent() {
            long time = System.currentTimeMillis();
            String stvalue = Long.toString(time);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            File output = new File(dir, stvalue);
            u = Uri.fromFile(output);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
            startActivityForResult(intent, REQUEST_CAMERA);
    }
    
     @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("file_uri", u);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        u = savedInstanceState.getParcelable("file_uri");
    }
    

    然后在onActivityResult中

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (resultCode == RESULT_OK) {          
            camerastore(u);
            break;
        } else {
            utils.Toast("Error in importing file");
        }
    }
    
    
    void camerastore(Uri u) {
        Bitmap bitmap = null;
        File pictureFile = new File("yourpath/image.jpg");
    
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), u);
            FileOutputStream fos = new FileOutputStream(pictureFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    【讨论】:

    • 请问camerapath是从哪里来的?
    • 我已经编辑过,我只是从我的项目中复制了它,我遇到了类似的问题..这段代码对我很有用..可能有一些不需要的代码,不要打扰它,我刚刚发布让你理解这个概念
    • 对我来说很好:) 谢谢
    【解决方案3】:

    改变这一行。

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

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

    【讨论】:

    • 不幸的是,这似乎并没有改变任何东西,在手机上查看时不会改变任何分辨率
    【解决方案4】:

    您使用JPEG 压缩格式压缩图像过多,通过在质量中指定90 会额外损失10% 的图像质量。基本上你会进行有损压缩。要进行无损压缩,您应该选择 100% 质量的 PNG 压缩格式。你可以这样做:

    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    

    还有一条建议。在File Provider 的帮助下,在cameraIntent() 方法本身中创建图像文件,在onActivity 中仅显示来自文件的图像。它提供了卓越的品质。

    【讨论】:

    • @swativishnoi 你能解释一下你的问题吗?
    • ByteArrayOutputStream 和 100 压缩对我不起作用,我仍然得到图像模糊/压缩。但是使用uri打开捕获图像意图对我来说很好。 onActivityResult 直接从 fileUri 获取图像。 takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    • @swativishnoi 这就是预期的结果。如果您需要手机摄像头图像的全分辨率,您只需要从 fileUri 中获取。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多