Android图像裁剪会减小图片的尺寸。

8

我需要从安卓手机相册中裁剪一张照片并进行编辑。 照片的原始尺寸为3264*2448,在我的1280*720尺寸屏幕上显示为缩小的图片。 当我进行裁剪时,得到的图像尺寸为185*139。

我用于裁剪的代码是:

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, PIC_CROP);

当我在imageView中显示裁剪后的图像时,它显示为一个更小的图像。

我应该裁剪原始大小的图像而不是缩小版本。你们能否指导我如何做到这一点?


我也遇到了同样的问题...你解决了吗? - Bhavesh Hirpara
+1 你的问题。我已经在下面给出了答案,请检查一下。 - Akanksha Rathore
3个回答

13
请尝试这个方法,我知道现在可能有点晚了,但可能会对某些人有帮助。
private void performCrop() {
try {
    //call the standard crop action intent (the user device may not support it)
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    //indicate image type and Uri
    cropIntent.setDataAndType(mImageCaptureUri, "image/*");
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 4);
    cropIntent.putExtra("aspectY", 3);
    //indicate output X and Y
    cropIntent.putExtra("outputX", 800);
    cropIntent.putExtra("outputY", 800);

File f = new File(Environment.getExternalStorageDirectory(),
        "/temporary_holder.jpg");
    try {
        f.createNewFile();
    } catch (IOException ex) {
    Log.e("io", ex.getMessage());  
    }

uri = Uri.fromFile(f);

  cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

    startActivityForResult(cropIntent, PIC_CROP);

} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    //display an error message
    String errorMessage = "Your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}
}

将您的onActivityResult修改为如下所示:-

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

     if (requestCode == PIC_CROP) {

              String filePath = Environment.getExternalStorageDirectory()
                    + "/temporary_holder.jpg";

            thumbnail = BitmapFactory.decodeFile(filePath);



                   //thumbnail =    BitmapFactory.decodeFile(filePath);
           //    Log.i("",String.valueOf(thumbnail.getHeight()));

                ImageView image = (ImageView) findViewById(R.id.pestImage);
                image.setImageBitmap(thumbnail);
                }
}

2

添加:

intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);

然后您可以获得一个未缩小的图像文件。并使用此保存的文件替代:

Intent.getExtras().getParcelable("data")

忽略返回的位图。


0
首先,您应该知道裁剪意图不能保证跨设备兼容性。(任何OEM都可以用自己的版本替换原始相机/画廊应用程序)。除此之外,我认为您错过了一些额外的内容,应该在意图中添加它们以获得所需的效果。
// width & height are your desired width/height for the cropped result
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
cropIntent.putExtra("aspectX", width);
cropIntent.putExtra("aspectY", height);
cropIntent.putExtra("scaleUpIfNeeded", true);

尝试将它们添加到您的意图中,看看效果如何。


1
我在裁剪意图中添加了额外的参数 cropIntent.putExtra("outputX", 480); cropIntent.putExtra("outputY", 640); cropIntent.putExtra("aspectX", 480); cropIntent.putExtra("aspectY", 640); cropIntent.putExtra("scaleUpIfNeeded", true);。裁剪后返回的位图大小为139 *185。 - namrathu1
1
处理宽高比时,您应始终使用最小公共分母。 - NodeDad

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