Conversion Tool class between bitmap, drawable, byte [], and inputstream

Source: Internet
Author: User
Package COM. soai. imdemo; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. inputstream; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. pixelformat; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable;/*** Conversion Tool class between bitmap and drawable and byte [] and inputstream * @ Author administrator **/public class formattools {Private Static formattools tools = new formattools (); public static formattools getinstance () {If (tools = NULL) {tools = new formattools (); Return tools;} return tools;} // convert byte [] to inputstreampublic inputstream byte2inputstream (byte [] B) {bytearrayinputstream BAIS = new bytearrayinputstream (B); Return BAIS;} // convert inputstream to byte [] public byte [] in Putstream2bytes (inputstream is) {string STR = ""; byte [] readbyte = new byte [1024]; int readcount =-1; try {While (readcount = is. read (readbyte, 0, 1024 ))! =-1) {STR + = new string (readbyte ). trim ();} return Str. getbytes ();} catch (exception e) {e. printstacktrace ();} return NULL;} // converts bitmap to inputstreampublic inputstream bitmap2inputstream (Bitmap BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. JPEG, 100, baos); inputstream is = new bytearrayinputstream (baos. tobytearray (); return is;} // converts bitmap to inputstream Public inputstream bitmap2inputstream (Bitmap BM, int quality) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, quality, baos); inputstream is = new bytearrayinputstream (baos. tobytearray (); return is;} // convert inputstream to bitmappublic bitmap inputstream2bitmap (inputstream is) {return bitmapfactory. decodestream (is);} // drawable to be converted to inputstreampublic inputstr Draeam wable2inputstream (drawable d) {Bitmap bitmap = This. drawable2bitmap (d); return this. bitmap2inputstream (Bitmap);} // converts inputstream to drawablepublic drawable inputstream2drawable (inputstream is) {Bitmap bitmap = This. inputstream2bitmap (is); return this. bitmap2drawable (Bitmap);} // converts drawable to byte [] public byte [] drawable2bytes (drawable d) {Bitmap bitmap = This. drawable2bitmap (d); return this. bitmap2b Ytes (Bitmap);} // byte [] to drawablepublic drawable bytes2drawable (byte [] B) {Bitmap bitmap = This. bytes2bitmap (B); return this. bitmap2drawable (Bitmap);} // converts bitmap to byte [] public byte [] bitmap2bytes (Bitmap BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, 100, baos); Return baos. tobytearray ();} // byte [] is converted to bitmappublic bitmap bytes2bitmap (byte [] B) {if (B. length! = 0) {return bitmapfactory. decodebytearray (B, 0, B. length);} return NULL;} // drawable is converted to bitmappublic bitmap drawable2bitmap (drawable) {Bitmap bitmap = bitmap. createbitmap (drawable. getintrinsicwidth (), drawable. getintrinsicheight (), drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565); canvas = new canvas (Bitmap); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); drawable. draw (canvas); Return bitmap;} // converts bitmap to drawablepublic drawable bitmap2drawable (Bitmap bitmap) {bitmapdrawable BD = new bitmapdrawable (Bitmap); drawable d = (drawable) BD; return D ;}}

Reprinted from: http://my.oschina.net/547217475/blog/93485

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.