Bitmap is one of the most important types of Image Processing in the Android system. It can be used to obtain image file information, perform image cutting, rotation, scaling, and other operations, and save image files in a specified format. This article focuses on how to use bitmap to implement these functions from the application perspective.
I. Generation of Bitmap
1.1 bitmapfactory decode bitmap
Bitmap is implemented in the Android. graphics package. However, bitmap constructor is private and cannot be instantiated outside. It can only be instantiated through JNI. This must be because a helper class provides an interface for creating bitmap, and the implementation of this class instantiates bitmap through the JNI interface. This class is bitmapfactory.
Figure 1 main bitmapfactory methods and options
Bitmapfactory can be used to calculate bitmap from a specified file, decodefile (), or decoderesource () in image resources.
1.2 decode options
You can specify a bitmapfacotry. options when using decodefile ()/decoderesource.
You can specify the decode option by using the following attributes of options:
Inpreferredconfig specifies the encoding used in the phone when the decode is in the memory. The optional value is defined in bitmap. config. The default value is argb_8888.
If the value of injustdecodebounds is set to true, the image data is not completely decoded, that is, the return value of decodexyz () is null, but the outabc of options solves the basic information of the image.
Insamplesize specifies the decode scaling ratio.
Using these options values, you can efficiently get a thumbnail.
Figure 2. bitmapfactory. decodefile ()
Set injustdecodebounds = true first, and call decodefile () to obtain the basic information of the image [step #2 ~ 4];
Use the image width (or height, or comprehensive) and target width to obtain the insamplesize value, set injustdecodebounds to false, and call decodefile () to obtain the complete image data [step #5 ~ 8].
First, get the proportion and then read the data. If you want to read a scaled-down graph, it will significantly save content resources. Sometimes a large number of thumbnails are read, which is more obvious.
Ii. Using bitmap and matrix for Image Transformation
Bitmap can be combined with matrix to achieve image cutting, rotation, scaling, and other operations.
Figure 3. Bitmap Method
Use the source bitmap to generate a new bitmap through transformation:
1
Public static bitmap createbitmap (bitmap source, int X, int y, intwidth, int height,
2
Matrix M, Boolean filter)
3
Public static bitmap createbitmap (bitmap source, int X, int y, intwidth, int height)
4
Public static bitmap createscaledbitmap (Bitmap SRC, int dstwidth,
5
Int dstheight, Boolean filter)
The first method is the final implementation, and the last two are only the encapsulation of the first method.
The second method can be used to dig out a part from the specified area (X, Y, width, height) in the source bitmap to achieve cutting. The third method can scale the source bitmap to the bitmap of dstwidth x dstheight.
Set the rotate (via setrotate () or scale (via setscale () of matrix to pass in the first method to achieve rotation or scaling.
Figure 4. Bitmap Rotation
3. Save image files
The data in the bitmap after image transformation can be saved to the image compression file (JPG/PNG ).
Figure 5. Save bitmap data to a file
During this operation, bitmap. the format parameter of the compress () method can be set to JPEG or PNG. The quality parameter can be set to compression quality. fout is the output stream, where fileoutputstream is a subclass of outputstream.
To sum up, this article introduces how to use Bitmap-using bitmap to read and write image files, and using bitmap to achieve image cutting, rotation, scaling, and transformation.