How to deal with image format classes and image conversion in Android program development

In the process of Android program development, it is very important to clarify which image format classes (imageformat, PixelFormat, bitmapconfig, etc.) and the conversion methods of images (jpg, PNG, BMP, etc.) will play a very important role in the future program development process. In a project development process, the development of a software is closely related to image processing, especially in mobile applications, which plays a vital role in visual effects, because it is related to user experience. Here's a code example to share with you:

About image format classes, the following three are introduced: imageformat, PixelFormat and bitmapconfig.

1. Imageformat (Android. Graphics. Imageformat). The format parameters are as follows:

Int JPEG, encoded formats, constant value: 256 (0x00000100)

    int NV16,YCbCr format,used for video,16 (0x00000010)

Int nv21, YCrCb format used for images, which uses the nv21 encoding format, constant value: 17 (0x00000011)

    int RGB_ 565,RGB format used for pictures encoded as RGB_ 565, constant value: 4 (0x00000004)

Int unknown, constant value: 0 (0x00000000)

    int YUY2,YCbCr format used for images,which uses YUYV (YUY2) encoding format,20 (0x00000014)

    int YV12,Android YUV format,This format is exposed to software decoders and applications

    YV12 is a 4:2:0 YCrCb planar format comprised of a WxH Y plane followed by (W/2) x (H/2) Cr and Cb planes

Explanation is always the most easy to understand in English. There is no ugly translation here. For example, when building the object of imagereader class, the image format object of imageformat class will be used. as

The image format used in the requirements depends on the actual situation, which is similar to the following.

2. PixelFormat (Android. Graphics. PixelFormat) has the following format parameters:

    int A_ 8. Constant value: 8 (0x00000008)

Int JPEG, constant value: 256 (0x00000100), constant, declared deprecated, use imageformat.jpeg instead

    int LA_ 88, constant value: 10 (0x0000000A)

    int L_ 8. Constant value: 9 (0x00000009)

Int opaque, constant value: - 1 (0xFFFFFFFF), system choices an opaque format (no alpha bits required)

    int RGBA_ 4444, constant value: 7 (0x00000007)

    int RGBA_ 5551, constant value: 6 (0x00000006)

    int RGBA_ 8888, constant value: 1 (0x00000001)

    int RGBX_ 8888, constant value: 2 (0x00000002)

    int RGB_ 332, constant value: 11 (0x0000000b)

    int RGB_ 565, constant value: 4 (0x00000004)

    int RGB_ 888, constant value: 3 (0x00000003)

Int translation, constant value: - 3 (0xfffffffd), system choices a format that supports transportation (many alpha bits)

Int transparent, constant value: - 2 (0xfffffffe), system choices a format that supports transparency (at least 1 alpha bit)

Int unknown, constant value: 0 (0x00000000)

    int YCbCr_ 420_ SP, constant value: 17 (0x00000011), constant has declared that it does not approve of using use imageformat.nv21 instead

    int YCbCr_ 422_ 1. Constant value: 20 (0x00000014), constant has declared that use imageformat.yuy2 instead is not allowed

    int YCbCr_ 422_ SP, constant value: 16 (0x00000010), constant has declared that use imageformat.nv16 instead is not supported

Note that four image formats have been declared not to be used, and can be replaced by the format corresponding to imagegformat. It can be seen that there are similarities between the two image formats. For example, let the window achieve the effect of gradient, such as

3. Bitmap. Config (android.graphics.bitmap internal class)

According to the official website, it roughly means that the color chroma display quality of a picture mainly depends on the bitmap configuration, whether the picture is transparent or translucent).

    ALPHA_ 8:Each pixel is stored as a single translucency (alpha) channel。 (each pixel of the original image is displayed in translucency)

    ARGB_ 4444:This field was deprecated in API level 13。 Because of the poor quality of this configuration,it is advised to use ARGB_ 8888 instead。 (abandoned after api13, 8888 is recommended).

    ARGB_ 8888 :Each pixel is stored on 4 bytes。 Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values) 。 This configuration is very flexible and offers the best quality。 It should be used whenever possible。 (each pixel occupies 4 bytes and each color is 8 bits. Anyway, it is very clear and comfortable to look at).

    RGB_ 565:Each pixel is stored on 2 bytes and only the RGB channels are encoded:red is stored with 5 bits of precision (32 possible values),green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision。 (this should be easy to understand).

For example, when building a bitmap object, the image format object of bitmapconfig class will be used, such as:

1. YUV to jpg

Most of the information consulted is to obtain the RGB code of each pixel through mathematical operation of YUV image data, store it in the bitmap object, and then call the compression method of bitmap class to generate JPG pictures. This method is extremely inefficient. A 480x320 resolution picture has 200000 bytes, so the operation needs to go through 200000 cycles. In fact, there is a yuvimage class under the android.graphics package, which can import data directly:

YuvImage image = new YuvImage(data,ImageFormat.NV21,IMG_WIDTH,IMG_HEIGHT,null); The first two parameters determine the data source and image format, and the following single parameters are not explained.

Yuvimage class happens to have a compresstojpeg (rect, rect, int i, OutputStream) method, which can directly save data in the output stream of JPG files.

2. PNG to bitmap

If it is in the form of drawable, it is much more convenient, just one sentence.

3. ARGB to bitmap

All the above contents are the methods of processing image format classes and image conversion in the process of Android program development. It should be noted that gray processing is done in the code. If you want to get color images, assign values to the three channels R, G and B of bitmap images respectively. I hope you like it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>