Android image format class and image conversion method

Source: Internet
Author: User
Tags deprecated pngimage

  

Introduction to Android Image format class and image conversion method the development of a software is closely related to the image, especially the mobile application, which is very important in the visual effect, because it is directly related

to the user's experience effect. in the process of developing Android programs, learn what image format classes exist (ImageFormat, PixelFormat, bitmapconfig, etc.) and images (JPG, PNG, and

BMP, etc.) of the conversion method, for future development will be somewhat helpful.

  About the image format class, 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: (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: + (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

interpretation is always the most easy to understand in English, it is not caught dead translated here. For example, when constructing an object of the ImageReader class, the image format object of the ImageFormat class is used. as

1 ImageReader imagereader = imagereader.newinstance (width, height, imageformat.rgb_565, 2);

The ImageReader object represents a maximum of two frames in its cache where the width height is the width and height respectively, and the rgb_565 format of the image stream.

In the requirements of which image format, depending on the actual situation, the following similar.

2,PixelFormat(android.graphics.PixelFormat), the format parameters are as follows:

int A_8, constant value: 8 (0x00000008)

int JPEG, constant value: 0x00000100, constant, declared disapproved, use Imageformat.jpeg instead.

int la_88, constant value: ten (0x0000000a)

Intl_8, Constant Value: 9 (0x00000009)

Intopaque, constant value:-1 (0xFFFFFFFF), System chooses an opaque format (no alpha bits required)

intrgba_4444, Constant value: 7 (0x00000007)

I ntrgba_5551, Constant value: 6 (0x00000006)

intrgba_8888, Constant value: 1 (0x00000001)

intrgbx_8888, Constant value: 2 (0x00000002)

intrgb_332, constant value: one (0x0000000b)

intrgb_565, Constant value: 4 (0x00000004)

intrgb_888, Constant value: 3 (0x00000003)

Inttranslucent, Constant Value:-3 (0XFFFFFFFD), System chooses a format that supports translucency (many alpha bits)

Inttransparent, constant value: -2 (0xFFFFFFFE), System chooses a format that supports transparency (at least 1 alpha bit)

Intunknown, Constant value: 0 (0x00000000)

INTYCBCR_420_SP, Constant value: + (0x00000011), constant has declared disapproval using use IMAGEFORMAT.NV21 instead

Intycbcr_422_i, constant value: (0x00000014), constant has declared disapproval of using use IMAGEFORMAT.YUY2 instead

intycbcr_422_sp, constant value: (0x00000010), constant has declared disapproval of using use Imageformat.nv16 instead

  Note that there are four types of image formats that have been declared deprecated and can be replaced by the corresponding format of the Imaggformat. It is found that there are similarities between the two image formats. for example, let the window achieve a gradient effect, such as

1 GetWindow (). SetFormat (pixelformat.rgba_8888);

  Added: rgba_8888 is an Android 32-bit color format, R, G, B, a eight-bit, the Android default image format is Pixelformat.opaque, which is without alpha value.

3,bitmap.config(Android.graphics.Bitmap inner Class)

Possible bitmap configurations. A bitmap configuration Describes how pixels is stored. This affects the quality (color depth) as well as the

ability to display transparent/translucent colors. (Official website Introduction, the general meaning is said: affect a picture color chroma display quality mainly look at the bitmap configuration, show the picture when transparent or translucent).

Alpha_8:each Pixel is stored as a single translucency (ALPHA) channel. (each image of the original is translucent display)

Argb_4444:this field is deprecated in API level 13. Because of the poor quality of this configuration, it's advised to use argb_8888

instead. (deprecated after API13, recommended for use 8888).

Argb_8888:each Pixel is stored on 4 bytes. Each channel (RGB and Alpha for translucency) are stored with 8 bits of precision (possible

values). This configuration is very flexible and offers, the best quality. It should be used whenever possible. (4 bytes per pixel, 8 bits per color

Yuan, anyway very clear, looked very comfortable).

  Rgb_565:each pixel is stored on 2 bytes and only the RGB channels be encoded:red with 5 bits of stored (p Ossible values),

Green is stored with 6 bits of precision (possible values) and blue are stored with 5 bits of precision. (This should be easy to understand).

  For example, when building a bitmap object, a Bitmapconfig class image format object is used, such as:

1 Bitmap Bitmap = Bitmap.createbitmap (width, height,bitmap.config.rgb_565)

  

  Here's a look at the ways, differences, and commonalities between the various types of images.

1,yuv turn jpg

Most of the data is obtained from the YUV image data by mathematical operations to obtain the RGB encoding of each pixel point, deposited into the bitmap object, then call the bitmap class to generate a JPG image with the compression method. This method is extremely inefficient, and a picture with a 480x320 resolution has 200,000 bytes, so the operation needs to go through 200,000 cycles. In fact, the Android.graphics package has a Yuvimage class that can import data directly:

1 New null);

The first two parameters determine the data source and image format, and the following individual parameters are not explained.

The Yuvimage class has exactly one compresstojpeg (rect rect, int i, OutputStream) method that can store data directly in the output stream of a JPG file.

2,png turn bitmap

1 byte[] data =NULL;2File Pngimage =NULL;3Bufferedoutputstream stream =NULL;4 Try {5Pngimage =NewFile (OutputFile);//outputfile as PNG image name6FileOutputStream FStream =NewFileOutputStream (pngimage);7stream =NewBufferedoutputstream (fstream);8     stream.write (data);9}Catch(Exception e) {Ten     e.printstacktrace (); One}finally { A    if(Stream! =NULL) { -       Try { -       stream.close (); the}Catch(IOException e) { -         e.printstacktrace (); -       } -     } + } -Bitmap bitmap=bitmapfactory.decodebytearray (data, 0, data.length);

If it is in the form of resources (drawable), it is more convenient and requires only one sentence.

1 Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);

Although there is no gorgeous algorithm, but the effect is good oh, just want to change the image properties to be implemented separately.

3,argb turn bitmap

1Bitmap bitmaporg = Bitmapfactory.decodebytearray (rawdata, 0, rawdata.length); 2Bitmap bitmapnew = bitmaporg.copy (config.argb_8888,true); 3 if(Bitmapnew = =NULL) 4     return;5  for(inti = 0;i<bitmapnew.getwidth (); i++) 6 { 7      for(intJ =0;j<bitmapnew.getheight (); j + +) 8     { 9       intCol =Bitmapnew.getpixel (i, j);Ten       intAlpha = col&0xff000000;  One       intRed = (col&0x00ff0000) >>16;  A       intGreen = (col&0x0000ff00) >>8;  -       intBlue = (col&0x000000ff);  -       intGray = (int)((float) red*0.3+ (float) green*0.59+ (float) blue*0.11);  the       intNewcolor = alpha| (gray<<16) | (gray<<8) |Gray; -     }  - }  - sendmsg (bitmapnew); +File File =NewFile (Environment.getexternalstoragedirectory () +file.separator+ "Gray" +number+ ". jpg");  - OutputStream out; + Try {  Aout =Newfileoutputstream (file); at     if(Bitmapnew.compress (Bitmap.CompressFormat.JPEG, 100, out))  -     out.close (); -}Catch(FileNotFoundException e) { -     e.printstacktrace (); -}Catch(IOException e) { -     e.printstacktrace (); in}

Note that the code is done in grayscale processing, if you want to get a color map, respectively, the bitmap image R, G, b three channels to assign values.

Today, the introduction of this, the next with the depth of learning, image conversion has a new implementation and then supplemented.

Android image format class and image conversion method

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.