We often need to crop the image, in fact, this feature in the Android system has a set of solutions, although the interface and effect is not very good, but the function is undoubtedly the perfect realization. As for how to customize the scheme without bringing it in, this is something. This article is mainly about the principle and process of cutting, take-out analysis of large-size image cropping and the difference between small-picture cropping, at the same time give a concrete implementation of the scheme.
First, principle + process
Andorid provides an action,Com.android.camera.action.CROP,
is Intent Intent = new Intent ("Com.android.camera.action.CROP");
Through this action can achieve the image clipping, specifically to achieve this intent, and then in this intent Putextra () put various parameters, and finally through to start a startactivityforresult (intent, Requestcode); , this is Crop the activity of the picture.
After cropping, return a bitmap and give it to the developer for processing.
That is to say, we are the main operation through the system written activity, we need only in the activity class Onactivityresult in accordance with the Requestcode to judge and processing.
Activate the activity of the System album:
Activate the system camera activity:
To start the activity of cropping a picture:
Ii. Major Issues
If the image we're intercepting is a large one, then we'll start by thinking about increasing the size of the output image.
// Outputx Outputy is a cropped image with a wide height of Intent.putextra ("outputx", and "Intent.putextra", 800);
However, this will cause problems, because the picture is too large, too much memory, the system will compress the image itself to avoid the problem of oom. Here is an excerpt from a blog post explaining the problem:
Original: http://blog.csdn.net/floodingfire/article/details/8144604
In Android, intent triggers the camera program, and when the picture is taken, the data is returned, but the camera does not return the full-size image to the calling activity, given the memory problem, and, in general, it is possible to return thumbnails, such as 120*160px.
What is this for? This is not a bug, but is well-designed, but not transparent to developers. take my Xiaomi phone as an example, camera 800W pixels, according to my current settings to shoot out the size of the picture for 3200*2400px. Some people say, that return Bai, big deal consumes 1-2m of memory, good, this size of picture really only about 1.8M size. But what you can't imagine is that the bitmap of this size will consume all the memory of your application. For security reasons, Android will only give you a shabby thumbnail image.
In Android2.3, the default bitmap is 32 bits, and the type is argb_8888, which means that one pixel occupies 4 bytes of memory. Let's do a simple computational question: 3200*2400*4 bytes = 30M.
Such a staggering number! Even if you're willing to spend so much memory on a bitmap that has a lifetime of over 10s, Android won't agree.
1 |
Mobile devices typically have constrained system resources. |
2 |
Android devices can have as little as 16MB of memory available to a single application. |
This is the original version of the Android doc, although different mobile phone system manufacturers may be around the 16M this number has slightly raised, but this 30M, the general mobile phone is really wasteful. Also only millet this cattle machine, memory comparable to personal PC, in the soil rich like a spendthrift to do the domineering.
The conclusion is that if you intercept a small graph then return a bitmap, but if it is a large map then return a URI. So there's no problem.
Third, from the album
Reference from: http://blog.csdn.net/floodingfire/article/details/8144615
Now the original author managed code has used the LIB package as an example, and the blog slightly different.
The original author blog post wrote two ways, I personally use is not very accustomed. I think that if the large image of the crop we do not bother to judge, then the direct unified return URI to solve all the problems, so use the following wording.
Explain in detail how to use Android to bring the image of the way to achieve the cutting--principle analysis + solution