【问题标题】:Crop an image when selected from gallery in android从android中的图库中选择时裁剪图像
【发布时间】:2012-06-02 08:04:02
【问题描述】:

当从图库中选择图像时,我想在我的应用程序中裁剪图像。 即,如果我启动图库并选择图像,那么当我们从 iPhone 中选择图像时,裁剪窗口应该会出现。安卓可以吗?

我找到了一个在 android 中裁剪图像的教程,但似乎不是我想要的方式。

http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/

【问题讨论】:

  • 检查this希望对你有帮助。
  • 看我的回答希望这就是你要找的,如果你发现一些问题或者你不明白代码,让我知道
  • 我已在 stackoverflow 帖子 (stackoverflow.com/a/44089387/1448357) 上 [此处] 发布我的解决方案

标签: android


【解决方案1】:

是的,可以使用com.android.camera.action.CROP 在 android 中裁剪图像。从图库中选择图片网址后,您将启动裁剪编辑器:

Intent intent = new Intent("com.android.camera.action.CROP");  
intent.setClassName("com.android.camera", "com.android.camera.CropImage");  
File file = new File(filePath);  
Uri uri = Uri.fromFile(file);  
intent.setData(uri);  
intent.putExtra("crop", "true");  
intent.putExtra("aspectX", 1);  
intent.putExtra("aspectY", 1);  
intent.putExtra("outputX", 96);  
intent.putExtra("outputY", 96);  
intent.putExtra("noFaceDetection", true);  
intent.putExtra("return-data", true);                                  
startActivityForResult(intent, REQUEST_CROP_ICON);

当图片选择Activity返回时会选择保存内容。inonActivityResult:

Bundle extras = data.getExtras();  
if(extras != null ) {  
    Bitmap photo = extras.getParcelable("data");  
    ByteArrayOutputStream stream = new ByteArrayOutputStream();  
    photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);  
        // The stream to write to a file or directly using the photo
}

看看this post,它也可以帮助你在android中裁剪图像

【讨论】:

  • com.android.camera.action.CROP 操作是内部 API 的一部分,因此并非在所有情况下都有效。只是想提一下:)
  • 在哪些情况下它不起作用?制造商何时拥有遗留层?
  • 这在很多很多设备上都不起作用。 Android没有CROPIntentcommonsware.com/blog/2013/01/23/…
  • 在我的例子中,data.getExtras() 返回一个Bundlenull,那么我怎样才能得到目标Bitmap
  • 在 5.0+ 中无法使用照片应用(5.0 之后的 Nexus 设备中删除了图库)
【解决方案2】:

这个tutorial正是你需要的享受:

从图库中挑选图片:

在 Intent 选择操作后裁剪图像

干杯

【讨论】:

  • 这就像一个魅力,完美的答案,非常易于使用。
  • @K_Anas 您在此处描述的教程不适用于 API>19。它显示错误:无法加载图像。请在这里帮助我
  • 这不是一个好的解决方案,因为它不适用于许多设备,因为 Intent 不是 Android 的一部分。
【解决方案3】:

您已经可以在选择/拍摄图像后告诉 Camera/Gallery-Intent 启动裁剪编辑器:

从图库中选择图片:

Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

pickImageIntent.setType("image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("outputX", 200);
pickImageIntent.putExtra("outputY", 200);
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("scale", true);
pickImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
pickImageIntent.putExtra("outputFormat",

Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(pickImageIntent, RESULT_LOAD_IMAGE);

拍照:

Intent takePicIntent = new Intent("android.media.action.IMAGE_CAPTURE");

takePicIntent .putExtra("crop", "true");
takePicIntent .putExtra("outputX", 200);
takePicIntent .putExtra("outputY", 200);
takePicIntent .putExtra("aspectX", 1);
takePicIntent .putExtra("aspectY", 1);
takePicIntent .putExtra("scale", true);
takePicIntent .putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
takePicIntent .putExtra("outputFormat",

Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(takePicIntent , RESULT_LOAD_IMAGE);

【讨论】:

  • onActivityResult()对应你的code怎么办?
  • @AliParsa 如何在onActivityResult() 中获取裁剪后的图像?
  • 仅适用于 ACTION_PICK 不适用于 IMAGE_CAPTURE
  • 此解决方案仅适用于 ACTION_PICK 而不是 IMAGE_CAPTURE,IMAGE_CAPTURE 应该怎么做?
【解决方案4】:

虽然是内部 API 的一部分,com.android.camera.action.CROP 似乎在大多数 Android 设备上都得到了很好的支持。这可能会让你开始:

final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uriOfImageToCrop);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.fromFile(someOutputFile));
startActivityForResult(intent, SOME_RANDOM_REQUEST_CODE);

然后在你的ActivityonActivityResult() 方法中处理你需要做的事情。您的输出文件应该是裁剪后的图像。

由于此 Intent 操作是内部 API 的一部分,但是,如果某些设备不支持 Intent,我会强烈建议您有某种回退行为。一些制造商提供自己的图库应用程序,因此无法知道用户的设备是否会识别Intent请不要忘记这个!! :)

【讨论】:

  • 我在看的是intent.putExtra("output", Uri.fromFile(someOutputFile));
  • 你能说我如何在 logcat 中打印裁剪后的 (outputX,outputY...) 值
【解决方案5】:

我用这种方法解决了这个问题

private void pickUserImage() { 

if (doHavePermission()) { 
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.putExtra("crop", "true");
    photoPickerIntent.putExtra("scale", true);
    photoPickerIntent.putExtra("outputX", 256);
    photoPickerIntent.putExtra("outputY", 256);
    photoPickerIntent.putExtra("aspectX", 1);
    photoPickerIntent.putExtra("aspectY", 1);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
    startActivityForResult(photoPickerIntent, PICK_FROM_GALLERY);
    } 
}

在 stackoverflow 帖子中找到我的完整解决方案 here

【讨论】:

    【解决方案6】:

    除非你尝试,否则没有人会告诉你需要哪些额外的东西:

       val intent = Intent(Intent.ACTION_PICK)
       intent.apply {
           type = "image/*"
           putExtra("crop", "true") // NOTE: should be string
           putExtra("outputX", 300) // This is needed, editor can't close without these two
           putExtra("outputY", 300) // This is needed
    
           putExtra("scale", true)
           putExtra("aspectX", 1)
           putExtra("aspectY", 1)
           putExtra("return-data", true)
       }
       startActivityForResult(intent, YOUR_INT_CODE)
    

    【讨论】:

    • 它对我不起作用。你在 onActivityResult() 里面有什么特别的吗?
    • putExtra("return-data", true) 在做什么?
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2016-02-17
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多