Preface-the cropping and uploading functions of user portraits are required in the project. As for cropping, I wanted to do it myself at the beginning, but I thought it should have been developed by Google. So I searched for official documents and found it. As a result, we use Android to customize photos. Because the time is too tight, although not very gorgeous, it can be used, saving time.
It is implemented through the Intent action.
The key code is as follows:
Public void imageCut (Uri uri) {Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); // enable intent for cropping. putExtra ("crop", "true"); // sets the intent ratio of the width to the height. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // you can specify the width and height of the cropped image. putExtra ("outputX", 100); intent. putExtra ("outputY", 100); // The intent data must be returned. putExtra ("return-data", true); startActivityForResult (intent, 100 );}
After the data is returned, you can directly display the returned image data in onActivityResult. Here, because the cut image is small, I think we should not consider OOM. The problem is, when you call the built-in cropping function, because you didn't see the source code, you don't know whether the image shown was compressed when you adjusted the cropping box. If not, when the image size is large, OOM is very easy to load. Obviously, I have no way to solve it because it calls the official API. The only way is to avoid this problem by writing one by yourself. Use the following code for compression to avoid OOM problems.
/** Compressed image, which returns the compressed image */public static Bitmap revitionImage (String path, int size) {Bitmap bitmap = null; try {// first open the image File in the image path and buffer it into a cache input stream BufferedInputStream in = new BufferedInputStream (new FileInputStream (new File (path ))); // use the parameter to represent the variable to record the information of the current photo, such as BitmapFactory. options options = new BitmapFactory. options (); // if you set inJustDecodeBounds to true, you can obtain bitmap information for the bitmap file without having to allocate memory. // after obtaining the size, you can perform 7 compression on it, and then generate a smaller bitmap in the memory, saving the memory options. inJustDecodeBounds = true; // decodes bitmap files only to obtain the information of the source image BitmapFactory. decodeStream (in, null, options); // remember to close the stream in. close (); int I = 0; while (true) {// shifts the I bit right because the following pow is half done, the new image is the original 1/2 if (options. outWidth> I) <= size) & (options. outHeight> I) <= size) {// at this time, the photo should be taken in = new BufferedInputStream (new FileInputStream (new File (path ))); // pow is the power method. the I power of 2. the inSampleSize image is evenly sampled. For example, // inSampleSize = 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. options. inSampleSize = (int) Math. pow (2.0, I); // at this time, the image size must be set to 256. Therefore, set false to allow output, create an image // truly generate a bitmapoptions with pixels scaled. inJustDecodeBounds = false; bitmap = BitmapFactory. decodeStream (in, null, options); // obtain the source image of one of n points, less than 256*256 break;} else {I ++ ;}} catch (IOException e) {Log. I (TAG, "compression image error" + e. toString ();} return bitmap ;}
At last, I can only use it first, and I have time to study it or try it myself. Below is, it looks okay.
Download