繁体   English   中英

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

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

我想从图库中选择一个图像在我的应用程序中。 也就是说,如果我启动图库并选择图像,裁剪窗口应该像我们从iPhone中选择图像时那样。 是否可以在Android中使用。

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

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

是的,可以使用com.android.camera.action.CROP在android中裁剪图像。 从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);

当图片选择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
}

并看到这篇文章 ,它也可以帮助你在android中裁剪图像

教程正是您需要的:

从图库中挑选图片:

在此输入图像描述

意图选择动作后裁剪图像

在此输入图像描述

干杯

虽然是内部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 ,我强烈建议您使用某种回退行为。 一些制造商提供他们自己的Gallery应用程序,因此无法知道用户的设备是否会识别Intent 请不要忘记这个!! :)

您可以在选择/拍摄图像后告诉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);

我这样解决了这个问题

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

找到我的完整的解决方案在这里在后计算器

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

   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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM