The bitmap class for Android image processing

Source: Internet
Author: User

Bitmap is one of the most important classes of image processing in an Android system. It can get image file information, make image cut, rotate, zoom and so on, and can save image file in specified format. From the perspective of application, this paper emphatically introduces how to use bitmap to realize these functions.

First, the generation of bitmap

1.1 Bitmapfactory decode out bitmap

The bitmap is implemented in the Android.graphics package. However, the constructors of the bitmap class are private and cannot be instantiated outside, only through JNI instantiation. This is necessarily a helper class that provides an interface for creating bitmap, and the implementation of this class is instantiated bitmap by the JNI interface, which is bitmapfactory.


Figure I, bitmapfactory main methods and options

Using bitmapfactory , you can use DecodeFile () to solve the bitmap from a specified file, or you can define a picture resource by using Decoderesource () to solve the bitmap.

Options when 1.2 decode

When you use Method DecodeFile ()/decoderesource (), you can specify a bitmapfacotry. Options.

You can specify options for decode by using the following properties of the options:

    • INPREFERREDCONFIG specifies decode to memory, the encoding used in the phone, and the optional value defined in Bitmap.config. The default value is argb_8888.
    • Injustdecodebounds if set to true, does not completely decode the image data, that is, the DECODEXYZ () return value is null, but the options of the OUTABC to solve the basic information of the image.
    • Insamplesize the scale at which the decode is set.

Using these values of the options, you can efficiently get a thumbnail image.


Figure II, Bitmapfactory.decodefile ()

First set injustdecodebounds= true, call DecodeFile () to get the basic information of the image [Step#2~4];

Using the width of the image (or height, or synthesis) and the width of the target, get the Insamplesize value, set injustdecodebounds= false, call DecodeFile () to get the complete image data [step#5~8].

To get the scale, and then read the data, if you want to read into the large scale reduction of the graph, will significantly save the content resources. Sometimes you read a lot of thumbnails, which is even more obvious.

Second, using bitmap and matrix to achieve image transformation

Bitmap can be combined with matrix to achieve the cutting, rotation, zooming and other operations of the image.


Figure III, bitmap method

The method of generating a new bitmap with the source bitmap by transformation:

?
12345 public static Bitmap createBitmap(Bitmap source, int x, int y, intwidth, int height,             Matrix m, boolean filter) public static Bitmap createBitmap(Bitmap source, int x, int y, intwidth, int height) public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,             int dstHeight,boolean filter)

The first method is the final implementation, and the latter two are just the encapsulation of the first method.

The second method can cut a piece from the specified region (x, y, width, height) in the source bitmap, and the third method can scale the source bitmap to Dstwidth x dstheight bitmap.

Set the matrix's rotate (via Setrotate ()) or scale (via Setscale ()) and pass in the first method, which can be rotated or scaled.


Figure IV, bitmap to achieve rotation

Third, save the image file

The data in the bitmap after the image transformation can be saved to the image compression file (jpg/png).

Figure v. Saving bitmap data to a file

During this operation, the parameter format of the Bitmap.compress () method can be set in JPEG or PNG format; Quality can choose the compression quality; Fout is the output stream (OutputStream). The FileOutputStream here is a subclass of OutputStream.

Summing up, this article introduces the use of bitmap---------bitmap to realize the reading and writing of image files, and to realize the shearing, rotation and zooming transformation of the image with bitmap.

The bitmap class for Android image processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.