Android image formats and image conversion methods

Source: Internet
Author: User
Tags pngimage

Android image formats and image conversion methods
Android image formats and image conversion methods introduction the development of a software is closely related to images, especially mobile applications, which are crucial in terms of visual effects, this is directly related to the user experience. During Android program development, you can find out which image formats (such as ImageFormat, PixelFormat, and BitmapConfig) exist and how to convert images (such as JPG, PNG, and BMP, it will be more or less helpful for future development. The following three types of image formats are introduced: ImageFormat, PixelFormat, and BitmapConfig. 1. ImageFormat (android. graphics. imageFormat), the format parameters include 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 imag Es, 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 are always the most easy-to-understand English, so we will not provide ugly translations here. For example, an image format object in the ImageFormat class is used when constructing an ImageReader class object. For example, 1 ImageReader imageReader = ImageReader. newInstance (width, height, ImageFormat. RGB_565, 2); The imageReader object indicates that the cache contains a maximum of two frames of image streams in width, height, and RGB_565 formats. Which image format to use depends on the actual situation. 2. PixelFormat (android. graphics. pixelFormat), the format parameters include int A_8, constant value: 8 (0x00000008) int JPEG, constant value: 256 (0x00000100), constant, I have declared that I do not agree to use it. use ImageFormat. JPEG instead. int LA_88, constant value: 10 (0x0000000a) int L_8, constant value: 9 (0x00000009) int OPAQUE, constant value:-1 (0 xffffffff ), 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 (0x0000 0001) 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 TRANSLUCENT, constant value:-3 (0 xfffffffd), System chooses a format that supports trans0000cy (many alpha bits) int TRANSPARENT, constant value: -2 (0 xfffffffe), System chooses 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 its disapproval of using ImageFormat. NV21 instead int YCbCr_422_ I, constant value: 20 (0x00000014), constant has declared that it does not approve of use ImageFormat. YUY2 instead int YCbCr_422_SP, constant value: 16 (0x00000010), constant has declared that it does not approve of use ImageFormat. NV16 instead note that four image formats have been declared to be disapproved and can be replaced by the corresponding ImaggFormat format. From this we can see that there are similarities between the two image formats. For example, let the window implement gradient effect, such as 1 getWindow (). setFormat (PixelFormat. RGBA_8888); supplement: RGBA_8888 is A 32-bit color format for android. R, G, B, And A are represented in eight bits. The default image format for Android is PixelFormat. OPAQUE, without the Alpha value. 3. Bitmap. Config (Android. graphics. Bitmap internal class) Possible bitmap configurations. A bitmap configuration describes how pixels are stored. This affects the quality (color depth) as well as the ability to display transparent/translucent colors. (According to the official website, it generally means that the color and color quality of an image is mainly determined by the bitmap configuration, which is transparent or translucent when the image is displayed ). ALPHA_8: Each pixel is stored as a single trans0000cy (alpha) channel. (Each pixel in the source image is translucent.) 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. (API13 and later are discarded. We recommend that you use 8888 ). ARGB_8888: Each pixel is stored on 4 bytes. Each channel (RGB and alpha for trans0000cy) is stored with 8 bits of precision (256 possible values ). This configuration is very flexible and offers the best quality. It shoshould be used whenever possible. (Each pixel occupies 4 bytes and each color is 8 bits. It is clear and looks comfortable ). 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, BitmapConfig image format objects are used to construct Bitmap objects, for example, 1 Bitmap bitmap = Bitmap. createBitmap (width, height, Bitmap. config. RGB_565) The following describes the conversion methods, differences, and commonalities between different types of images. 1. Most of the materials obtained from YUV to JPG are obtained by mathematical operation of Yuv image data, and the RGB encoding of each pixel is obtained. The data is saved to the Bitmap object, and then the built-in compression method of Bitmap class is called to generate the JPG image. This method is extremely inefficient. A 0.2 million x320 resolution image contains 0.2 million bytes, so the computation requires cycles. In fact, android. there is a YuvImage class under the graphics package that can import data directly: 1 YuvImage image = new YuvImage (data, ImageFormat. NV21, IMG_WIDTH, IMG_HEIGHT, null); the first two parameters determine the data source and image format. The YuvImage class has a compressToJPEG (Rect rect, int I, OutputStream) method, which can directly store data in the output stream of the JPG file. 2. Converting PNG to Bitmap

1 byte [] data = null; 2 File pngImage = null; 3 BufferedOutputStream stream = null; 4 try {5 pngImage = new File (outputFile ); // outputFile is png Image name 6 FileOutputStream fstream = new FileOutputStream (pngImage); 7 stream = new BufferedOutputStream (fstream); 8 stream. write (data); 9} catch (Exception e) {10 e. printStackTrace (); 11} finally {12 if (stream! = Null) {13 try {14 stream. close (); 15} catch (IOException e) {16 e. printStackTrace (); 17} 18} 19} 20 Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length );

 

If you use a resource (drawable), it is more convenient and you only need one sentence. 1 Bitmap bmp = BitmapFactory. decodeResource (getResources (), R. drawable. icon); although there is no gorgeous algorithm, the effect is good, that is, when you want to change the image attributes, You have to implement it separately. 3. Convert ARGB to Bitmap
 1 Bitmap bitmapOrg = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);  2 Bitmap bitmapNew = bitmapOrg.copy(Config.ARGB_8888, true);  3 if(bitmapNew == null)  4   return; 5 for(int i = 0;i<bitmapNew.getWidth();i++)  6 {  7   for(int j =0;j<bitmapNew.getHeight();j++)  8   {  9     int col = bitmapNew.getPixel(i, j); 10     int alpha = col&0xFF000000; 11     int red = (col&0x00FF0000)>>16; 12     int green = (col&0x0000FF00)>>8; 13     int blue = (col&0x000000FF); 14     int gray = (int)((float)red*0.3+(float)green*0.59+(float)blue*0.11); 15     int newColor = alpha|(gray<<16)|(gray<<8)|gray; 16   } 17 } 18 sendMsg(bitmapNew); 19 File file = new File(Environment.getExternalStorageDirectory()+File.separator+"gray"+number+".jpg"); 20 OutputStream out; 21 try { 22   out = new FileOutputStream(file); 23   if(bitmapNew.compress(Bitmap.CompressFormat.JPEG, 100, out)) 24   out.close(); 25 } catch (FileNotFoundException e) { 26   e.printStackTrace(); 27 } catch (IOException e) { 28   e.printStackTrace(); 29 } 

 

Note: grayscale processing is performed in the Code. If you want to obtain a color image, assign values to the Bitmap image R, G, and B channels.

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.