Android Raw to BMP

Source: Internet
Author: User
Tags bmp image

Android RAW format to BMP image

Raw is stored as bare data, and it needs to be converted into an RGBA mode to display it. which

8-bit raw: four-bit rgba to represent a grayscale;

24-bit raw: three-bit RGB is the same, A (Alpha) is represented by 0XFF.

Bitmap.config Enumeration Description:

  The higher the number of bitmap bits, the more color information they can store, and the more realistic the image will be.

  • Alpha_8 represents a 8-bit alpha bitmap
  • argb_4444 represents a 16-bit ARGB bitmap
  • argb_8888 represents a 32-bit ARGB bitmap
  • rgb_565 represents a 8-bit RGB bitmap

The implementation is as follows :

3 ImportJava.io.ByteArrayOutputStream;4 Importjava.io.IOException;5 ImportJava.io.InputStream;6 ImportJava.nio.ByteBuffer;7 8 ImportAndroid.graphics.Bitmap;9 Ten /** One * Raw Turn bitmap A  *  -  * @authorChenrui -  *  the  */ -  Public classRawtobitmap - { -     /** + * Reading an array from the stream -      *  +      * @paramStream A * Input Stream at      * @return -      */ -      Public Static byte[] Readbytearrayformstream (InputStream stream) -     { -         Try -         { inBytearrayoutputstream OutStream =NewBytearrayoutputstream (); -             intLen = 0; to             byte[] tmp =New byte[1024]; +              while(len = stream.read (tmp))! =-1) -             { theOutstream.write (TMP, 0, Len); *             } $             Panax Notoginseng             byte[] data =Outstream.tobytearray (); -              the             returndata; +         } A         Catch(IOException e) the         { + e.printstacktrace (); -             return New byte[0]; $         } $     } -      -     /** the * 8-bit grayscale turn bitmap -      * Wuyi * Image width must be divisible by 4 the      *  -      * @paramData Wu * Bare Data -      * @paramwidth About * Image Width $      * @paramHeight - * Image Height -      * @return -      */ A      Public StaticBitmap Convert8bit (byte[] Data,intWidthintheight) +     { the         byte[] Bits =New byte[Data.length * 4];//RGBA Array -          $         inti; the          for(i = 0; i < data.length; i++) the         { the             //principle: 4 bytes represents a grayscale, then RGB = grayscale value, the last alpha = 0xFF; theBits[i * 4] = bits[i * 4 + 1] = bits[i * 4 + 2] =Data[i]; -Bits[i * 4 + 3] =-1;//0xFF in         } the          the         //Bitmap.Config.ARGB_8888: Image mode is 8 bits AboutBitmap BMP =Bitmap the . CreateBitmap (width, height, Bitmap.Config.ARGB_8888); the Bmp.copypixelsfrombuffer (Bytebuffer.wrap (Bits)); the          +         returnbmp; -     } the     Bayi     /** the * 24-bit grayscale turn bitmap the      *  - * Image width must be divisible by 4 -      *  the      * @paramData the * Bare Data the      * @paramwidth the * Image Width -      * @paramHeight the * Image Height the      * @return the      */94      Public StaticBitmap Convert24bit (byte[] Data,intWidthintheight) the     { the         byte[] Bits =New byte[Data.length * 4];//RGBA Array the         98         inti; About          -         //DATA.LENGTH/3 represents a group of 3 bits101          for(i = 0; i < DATA.LENGTH/3; i++)102         {103             //principle: 24 bits are colored, so to copy 3 bits, the last one alpha = 0xFF;104Bits[i * 4] = Data[i * 3]; theBits[i * 4 + 1] = data[i * 3 + 1];106Bits[i * 4 + 2] = data[i * 3 + 2];107Bits[i * 4 + 3] = 1;108         }109          the         //Bitmap.Config.ARGB_8888: Image mode is 8 bits111Bitmap BMP =Bitmap the . CreateBitmap (width, height, Bitmap.Config.ARGB_8888);113 Bmp.copypixelsfrombuffer (Bytebuffer.wrap (Bits)); the          the         returnbmp; the     }117     118     /**119 * 8-bit grayscale turn bitmap -      * 121      * @paramStream122 * Input Stream123      * @paramwidth124 * Image Width the      * @paramHeight126 * Image Height127      * @return -      */129      Public StaticBitmap Convert8bit (InputStream stream,intWidthintheight) the     {131         returnconvert8bit (Readbytearrayformstream (stream), width, height); the     }133     134     /**135 * 24-bit grayscale turn bitmap136      * 137      * @paramData138 * Input Stream139      * @paramwidth $ * Image Width141      * @paramHeight142 * Image Height143      * @return144      */145      Public StaticBitmap Convert24bit (InputStream stream,intWidthintheight)146     {147         returnconvert24bit (Readbytearrayformstream (stream), width, height);148     }149}

The width and height must match correctly when displayed.

Imageview1.setimagebitmap (Rawtobitmap.convert8bit (Getassets (). Open ("8bit.raw"), 512, 512));

Android Raw to BMP

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.