How the Android program deals with image format classes and image conversion _android

Source: Internet
Author: User
Tags constant pngimage

In the process of developing Android programs, it is important to identify which image format classes (ImageFormat, PixelFormat, bitmapconfig, etc.) and the image (JPG, PNG, BMP, etc.) are converted. Will play a very important role in the future development of the process. In a project development process of a software development and image processing has a close relationship, especially in mobile applications, in terms of visual effects play a crucial role, because this is related to the user experience. Here's a code example to share with you:

About the image format class, introduce the following three: ImageFormat, PixelFormat and Bitmapconfig.

  1, ImageFormat (android.graphics.ImageFormat), format parameters have the following kinds:

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: (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 to a WxH Y plane followed by (W/2) x (H/2) Cr and Cb planes

Interpretation is always the most understandable in English, there is no shortcoming translation. For example, when you build an object of the ImageReader class, you use the image format object of the ImageFormat class. Such as

Copy Code code as follows:

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

The ImageReader object indicates that there are up to two frames in the cache for image streams of width and height and rgb_565 format.

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

2, PixelFormat (android.graphics.PixelFormat), format parameters have the following kinds:

int A_8, constant value: 8 (0x00000008)

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

int la_88, constant value: ten (0x0000000a)

int L_8, constant value: 9 (0x00000009)

int OPAQUE, constant value:-1 (0xFFFFFFFF), System chooses 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: one (0x0000000b)

int rgb_565, constant value: 4 (0x00000004)

int rgb_888, Constant value: 3 (0x00000003)

int translucent, constant value:-3 (0XFFFFFFFD), System chooses a format that supports translucency (many alpha bits)

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

int UNKNOWN, constant value: 0 (0x00000000)

int ycbcr_420_sp, constant value: (0x00000011), constant has declared disapproval of use using IMAGEFORMAT.NV21 instead

int Ycbcr_422_i, constant value: (0x00000014), constant has declared disapproval of use uses IMAGEFORMAT.YUY2 instead

int ycbcr_422_sp, constant value: (0x00000010), constant has declared disapproval of use uses imageformat.nv16 instead

Note that there are four types of image formats that have been declared disapproved of, and can be replaced with a imaggformat format. So there are similarities between the two image formats. Use examples, so that the window to achieve the effect of a gradient, such as

Copy Code code as follows:

GetWindow (). SetFormat (pixelformat.rgba_8888);

Supplemental Note: rgba_8888 is a 32-bit color format for Android, R, G, B, and a are represented by eight digits, and the Android default image format is pixelformat.opaque with no alpha value.

  3, Bitmap.config (Android.graphics.Bitmap internal Class)

  

Copy Code code as follows:

Possible bitmap configurations. A bitmap configuration Describes how pixels are stored. This affects the quality (color depth) as the "as" the ability to display transparent/translucent colors. (

Official website Introduction, the general meaning is said: influence a picture color chroma display quality mainly see bitmap configuration, show picture transparent or translucent.

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

Argb_4444:this field was deprecated at API level 13. Because of the poor quality of this configuration, it's advised to use argb_8888. (It was discarded after API13 and recommended to use 8888).

Argb_8888:each Pixel is stored on 4 bytes. Each channel (RGB and Alpha to 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, each color 8 bits, anyway very clear, look very comfortable).

Rgb_565:each pixel is stored on 2 bytes and only the RGB channels are encoded:red are stored with 5 bits of precision (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:

Copy Code code as follows:

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 information is the YUV image data through the mathematical operation of each pixel point of RGB encoding, deposit Bitmap object, and then call Bitmap class from the compression method generated JPG image. This method is extremely inefficient, with a picture with a 480x320 resolution of 200,000 bytes, so the operation needs to go through 200,000 cycles. In fact, there is a Yuvimage class under the Android.graphics package that can import data directly:

Yuvimage image = new Yuvimage (data, imageformat.nv21, img_width, img_height, NULL);
The previous two parameters determine the data source and image format, followed by a single parameter is 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

byte[] data = null;
 File pngimage = null;
 Bufferedoutputstream stream = null;
 try {
 pngimage = new File (outputfile);//outputfile for PNG image name
 fileoutputstream fstream = new FileOutputStream (pn  Gimage);
 stream = new Bufferedoutputstream (fstream);
 Stream.Write (data);
 } catch (Exception e) {
 e.printstacktrace ();
 } finally {
 if (stream!= null) {
 try {
 St    Ream.close ();
 catch (IOException e) {
 e.printstacktrace ();
 }
 }
 Bitmap bitmap=bitmapfactory.decodebytearray (data, 0, data.length);

If through the form of resources (drawable), that is more convenient, only need a sentence.

Copy Code code as follows:

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

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

  3, ARGB turn bitmap

 Bitmap bitmaporg = Bitmapfactory.decodebytearray (rawdata, 0, rawdata.length); 
 Bitmap bitmapnew = Bitmaporg.copy (config.argb_8888, true);
 if (bitmapnew = null) return; for (int i = 0;i<bitmapnew.getwidth (), i++) {for (int J =0;j<bitmapnew.getheight (); j + +) {int col = bi 
     Tmapnew.getpixel (i, j); 
     int alpha = col&0xff000000; 
     int red = (col&0x00ff0000) >>16; 
     int green = (col&0x0000ff00) >>8; 
     int blue = (COL&AMP;0X000000FF); 
     int gray = (int) ((float) red*0.3+ (float) green*0.59+ (float) blue*0.11); int Newcolor = alpha| (gray<<16) | 
   (gray<<8) |gray; 
 } sendmsg (Bitmapnew); 
 File File = new file (environment.getexternalstoragedirectory () +file.separator+ "Gray" +number+ ". jpg"); 
 OutputStream out; 
   try {out = new FileOutputStream (file); 
 if (Bitmapnew.compress (Bitmap.CompressFormat.JPEG, Out)) Out.close (); 
 catch (FileNotFoundException e) {e.printstacktrace (); catch (IoexCeption e) {e.printstacktrace (); 
 }

      All of the above is in the Android program development process to deal with image format classes and image conversion methods, it should be noted that in the code to do gray processing, if you want to get color maps, respectively, bitmap image R , G, b three channel assignment. I hope you can enjoy it.

Related Article

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.