简体   繁体   English

从android中的库中选择时裁剪图像

[英]Crop an image when selected from gallery in android

I want to crop an image in my application when it is selected from gallery. 我想从图库中选择一个图像在我的应用程序中。 ie if I launch the gallery and select an image the cropping window should come like when we select an image from iPhone. 也就是说,如果我启动图库并选择图像,裁剪窗口应该像我们从iPhone中选择图像时那样。 Is it possible in Android. 是否可以在Android中使用。

I found one tutorial for cropping the image in android, but dont seem the way I wanted. 我发现了一个在android中裁剪图像的教程,但似乎不是我想要的方式。

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

Yes it's possible to crop image in android by using com.android.camera.action.CROP . 是的,可以使用com.android.camera.action.CROP在android中裁剪图像。 after picking image url from gallery.you will start Crop Editor as: 从gallery.you中选择图片网址后,您将启动裁剪编辑器:

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

When the picture select Activity return will be selected to save the contents.in onActivityResult : 当图片选择Activity return将被选中以保存contents.in onActivityResult

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
}

and see this post which is also help you for cropping image in android 并看到这篇文章 ,它也可以帮助你在android中裁剪图像

This tutorial is exactly what you need enjoy: 教程正是您需要的:

Picking image from gallery: 从图库中挑选图片:

在此输入图像描述

Crop image after Intent pick action 意图选择动作后裁剪图像

在此输入图像描述

Cheers 干杯

Although part of the internal API, the com.android.camera.action.CROP seems like it is well-supported on most Android devices. 虽然是内部API的一部分,但com.android.camera.action.CROP似乎在大多数Android设备上都得到了很好的支持。 This might get you started: 这可能会让你开始:

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

Then handle what you need to do in the onActivityResult() method of your Activity . 然后在ActivityonActivityResult()方法中处理您需要执行的操作。 Your output file should be the cropped image. 您的输出文件应该是裁剪后的图像。

Since this Intent action is part of the internal API, however, I would strongly advise that you have some sort of fallback behavior if some device does not support the Intent . 但是,由于此Intent操作是内部API的一部分,因此,如果某些设备不支持Intent ,我强烈建议您使用某种回退行为。 Some manufacturers provide their own Gallery apps and so there is no way of knowing whether or not the user's device will recognize the Intent . 一些制造商提供他们自己的Gallery应用程序,因此无法知道用户的设备是否会识别Intent PLEASE DON'T FORGET THIS!! 请不要忘记这个!! :) :)

You can already tell the Camera/Gallery-Intent to start the crop-editor after the image is selected/taken: 您可以在选择/拍摄图像后告诉Camera / Gallery-Intent启动裁剪编辑器:

Pick Image from Gallery: 从图库中选择图片:

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

Take Photo: 拍照:

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

I solved this problem this way 我这样解决了这个问题

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);
    } 
}

find my complete solution here in stackoverflow post 找到我的完整的解决方案在这里在后计算器

Nobody will tell you which extra is needed unless you try it: 除非你尝试,否则没有人会告诉你需要哪些额外的东西:

   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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自画廊的裁剪图像崩溃应用程序 - Android - Crop image from gallery crashes app - Android 来自库nullpointexception的android裁剪图像 - android crop image from gallery nullpointexception 从手机屏幕上的Android库中选择时无法加载图片 - Unable to load image when selected from the gallery on Android in phonegap Android - 从图库或相机中裁剪图像时,outputX / outputY和aspectX / aspectY是什么? - Android - What is outputX/outputY and aspectX/aspectY when crop an image from gallery or camera? 如何从画廊或相机像Android中的whatapp裁剪图像 - how to crop image from gallery or camera like whatapp in android android:从图库中选择图片,然后裁剪并显示在图片视图中……但不保存在sharedpreferce中 - android:select image from gallery then crop that and show in an imageview…but not saving in sharedpreferce 从图库中裁剪图像并将其设置为我的imageview背景Android - Crop Image from gallery and set it as my imageview background Android android:从图库中选择图像然后裁剪并在图像视图中显示 - android:select image from gallery then crop that and show in an imageview 从图库中为Android API 21及更高版本选择了裁剪图像 - Crop Image Chosed from Gallery for Android API 21 and above 如何在 Android 7.0 中从相机或图库中选择要裁剪的图像? - How to pick image for crop from camera or gallery in Android 7.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM