In the first two articles, I wrote the Open source project Imagecropper Library, and how to call the system's picture clipping module, this article continues to analyze the development of Android image tailoring application needs to use the bitmap operation.
In the Android system, the operation of the picture is mainly through the bitmap class and The Matrix class, This article introduces some of the operation of bitmap in a clipping application, including: Open, save, cut, rotate, etc. I've encapsulated these operations into a Bitmaphelper.java class and put it on GitHub (click here), so you can easily integrate into your project.
Open picture
The opening of the picture is mainly to convert pictures of various formats into bitmap objects, Android through the Bitmapfactory class provides a series of static methods to assist with this operation, as follows:
public class bitmapfactory { public static bitmap DecodeFile (string pathname, options opts); public static Bitmap decodefile (String pathname); public static bitmap Decoderesourcestream (resources res, typedvalue value, inputstream is, rect pad, options opts) ; public static bitmap decoderesource (Resources res, int id, options opts) ; public static bitmap decoderesource ( Resources res, int id); public static bitmap Decodebytearray (byte[] data, int offset, int length, options opts); public static bitmap&nbsP;decodebytearray (byte[] data, int offset, int length); public static bitmap decodestream (inputstream is, rect outpadding, options opts); public static bitmap decodestream (Inputstream is) ; public static bitmap decodefiledescriptor (FileDescriptor fd, rect outpadding, options opts); public static bitmap Decodefiledescriptor (FILEDESCRIPTOR FD) ;}
Through these static methods, we can easily open images from files, resources, byte stream and other ways, and generate bitmap objects. Here is a function package that opens a picture from a file:
public static bitmap load ( String filepath ) { Bitmap bitmap = null; try { Fileinputstream fin = new fileinputstream (filepath); bitmap = bitmapfactory.decodestream (Fin); fin.close (); } catch (filenotfoundexception e) { } catch (ioexception e) { } return bitmap; }
2. Save the picture
The image preservation is mainly through the bitmap compress method, the prototype of the method is as follows:
/** * write a compressed version of the bitmap to the specified outputstream. * @param format The format of the compressed image * @param quality Hint to the compressor, 0-100. 0 meaning compress for * small size, 100 meaning compress for max quality. some * formats, like PNG which is lossless, will ignore the * quality setting * @param stream the outputstream to write the compressed data. * @return true if successfully compressed to the specified stream. */public boolean compress (compressformat format, int quality , outputstream stream)
The first parameter is the picture format, only JPEG, PNG and WEBP three, the second parameter is the compression quality (0~100), the larger the value of the image the smaller the loss of information, the third parameter is a file stream object.
Again, here is a function wrapper to save the image:
Public static void save ( Bitmap bitmap, String filepath ) { try { fileoutputstream fos = new fileoutputstream (filepath); bitmap.compress ( Compressformat.jpeg, 100, fos); bitmap.recycle (); fos.close (); } catch (filenotfoundexception e) { } catch ( Ioexception e)  {&Nbsp; } }
3. Crop the picture
There are 2 ways to crop a picture in Android, one is to create a cropped picture by Bitmap's CreateBitmap method, and the other is to "draw" a new image through a canvas object, where the code is given and then analyzed:
public static Bitmap crop (Bitmap Bitmap, Rect croprect) {return Bitmap.createbitmap (bitmap,croprect.left,croprect.to P,croprect.width (), Croprect.height ());} public static Bitmap Cropwithcanvas (Bitmap Bitmap, rect croprect) {rect destrect = new Rect (0,0,croprect.width (), CRO Prect.height ()); Bitmap cropped = Bitmap.createbitmap (Croprect.width (), Croprect.height (), Bitmap.Config.RGB_565); Canvas canvas = new canvas (cropped); Canvas.drawbitmap (Bitmap,croprect,destrect,null); return cropped;}
In fact, the first method of internal implementation is also the use of the canvas object to "draw" a new picture, the canvas object through a bitmap object to build, the bitmap is the "canvas", Drawbitmap is the source bitmap object "painting" Into the "canvas", In this way, the data is moved and the image is cut.
4. Rotate the picture
The rotated picture in Android is also generated by bitmap's CreateBitmap method, but the rotation of the image needs to be done with the help of the Matrix object, the code is as follows:
public static Bitmap rotate (Bitmap Bitmap, int degrees) {Matrix Matrix = new Matrix (); Matrix.postrotate (degrees); Return Bitmap.createbitmap (Bitmap,0,0,bitmap.getwidth (), Bitmap.getheight (), matrix,true);}
Of course, the rotation of the picture can also be "drawn" through the canvas, because the rotation of the picture will cause the boundary coordinates to change, so you need to focus on the image center point coordinates to rotate, concrete implementation see the following code:
Public static bitmap rotatewithcanvas ( Bitmap bitmap, int degrees ) { int destwidth, destheight; float centerx = bitmap.getwidth ()/2; float centery = bitmap.getheight ()/2; // we want to do the rotation at origin, but since the bounding // rectangle will be changed after rotation, so the delta values // are based on old & new width/height respectively. matrix matrix = new matrix (); matrix.pretranslate (-centerX,-centerY); matrix.postrotate (degrees); if ( degrees/90%2 == 0 ) { destwidth = bitmap.getwidth (); Destheight = bitmap.getheight (); matrix.posttranslate (centerx,centery); } else { destwidth = bitmap.getheight (); destheight = bitmap.getwidth (); Matrix.posttranslate (centery,centErX); } bitmap cropped = bitmap.createbitmap (destwidth,destheight,bitmap.config.rgb_ 565); canvas canvas = new canvas (cropped); canvas.drawbitmap (bitmap, matrix, null); return cropped;}
5. Summary
The relevant operations on bitmap are described here, more code examples and implementations can refer to my open source project Imagecropper,
The project's GitHub address: Https://github.com/Jhuster/ImageCropper, have any questions welcome message discussion or letter [email protected] exchange.
This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1604074
Android Development Practice: Write your own picture tailoring application (3)