【问题标题】:Android image cropping is reducing the size of the imageAndroid图像裁剪正在减小图像的大小
【发布时间】:2013-01-01 08:27:47
【问题描述】:

我需要从 Android 手机图库中裁剪一张照片,然后对其进行编辑。 照片的原始尺寸为 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 中显示裁剪后的图像时,它会显示为小得多的图像。

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

【问题讨论】:

  • 我也面临同样的问题...你解决了吗??
  • 你的问题+1。我已经给出了答案。请检查一下。

标签: java android image android-layout crop


【解决方案1】:

请试试这个,我知道为时已晚,但可能对某人有所帮助。

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")
    

    忽略返回的位图。

    【讨论】:

      【解决方案3】:

      首先,您应该知道,不能依赖裁剪意图来实现跨设备兼容性。 (任何 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);
      

      尝试将这些添加到您的意图中,看看结果如何。

      【讨论】:

      • 我在裁剪意图中添加了额外的 cropIntent.putExtra("outputX", 480); cropIntent.putExtra("outputY", 640); cropIntent.putExtra("aspectX", 480); cropIntent.putExtra("aspectY", 640); cropIntent.putExtra("scaleUpIfNeeded", true);。裁剪后返回的位图为 139 *185。
      • 在处理纵横比时,您应该始终使用最小公用支配符
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      相关资源
      最近更新 更多