开发者

Image cropping activity/widget

开发者 https://www.devze.com 2023-01-21 05:00 出处:网络
Is there an Image cropping Activity in Android?I know that when you save an images as your wallpaper, it pops up an image cropper... and I\'ve looked into the sourcecode for that, but it depends on a

Is there an Image cropping Activity in Android? I know that when you save an images as your wallpaper, it pops up an image cropper... and I've looked into the sourcecode for that, but it depends on a LOT of gallery specific stuff. Not very reusable.

Does anyone know how开发者_C百科 this might be done, or are there any third party libraries that can help me out on this one?


There's a reasonably well-supported Intent for this (worked on my testing on at least a half dozen different Android 2.1+ phones, including SenseUI and Motoblur devices, and I believe it's been around since Android 1.0 or earlier on the G1). 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 just handle what you need to do in the onActivityResult() method of your Activity; your output file should have the cropped image in it at that point.

NOTE: This code makes use of an internal API and will not work on all devices. To be safe, though, you may want to have a fallback behavior (autocrop? Don't crop?) if someone has a device that doesn't support this Intent.


Don't forget one important attribute:

intent.putExtra("scaleUpIfNeeded", true);

This is crucial if the photo youre cropping is smaller than output you want to get. If not provided you will get black frame around your cropped image

0

精彩评论

暂无评论...
验证码 换一张
取 消