- 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 InputStream
- Public InputStream Byte2inputstream (byte[] b) {
- Bytearrayinputstream Bais = new Bytearrayinputstream (b);
- return Bais;
- }
- //Convert InputStream to byte[]
- public byte[] Inputstream2bytes (InputStream is) {
- String str = "";
- byte[] ReadByte = new byte[1024];
- int readcount =-1;
- try {
- While ((Readcount = Is.read (ReadByte, 0, 1024x768))! =-1) {
- str + = new String (readbyte). Trim ();
- }
- return str.getbytes ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return null;
- }
- //Convert bitmap to InputStream
- Public InputStream Bitmap2inputstream (Bitmap BM) {
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- Bm.compress (Bitmap.CompressFormat.JPEG, BAOs);
- InputStream is = new Bytearrayinputstream (Baos.tobytearray ());
- return is;
- }
- //Convert 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 bitmap
- Public Bitmap Inputstream2bitmap (InputStream is) {
- return Bitmapfactory.decodestream (IS);
- }
- //drawable converted to InputStream
- Public InputStream Drawable2inputstream (drawable d) {
- Bitmap Bitmap = This.drawable2bitmap (d);
- return this . Bitmap2inputstream (bitmap);
- }
- //InputStream converted to drawable
- Public drawable inputstream2drawable (InputStream is) {
- Bitmap Bitmap = this . Inputstream2bitmap (IS);
- return this.bitmap2drawable (bitmap);
- }
- //drawable convert to byte[]
- public byte[] Drawable2bytes (drawable d) {
- Bitmap Bitmap = This.drawable2bitmap (d);
- return this . Bitmap2bytes (bitmap);
- }
- ///byte[] converted to drawable
- Public drawable bytes2drawable (byte[] b) {
- Bitmap Bitmap = this . Bytes2bitmap (b);
- return this.bitmap2drawable (bitmap);
- }
- //Bitmap convert to byte[]
- public byte[] Bitmap2bytes (Bitmap BM) {
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
- return Baos.tobytearray ();
- }
- ///byte[] converted to bitmap
- Public Bitmap Bytes2bitmap (byte[] b) {
- if (b.length! = 0) {
- return Bitmapfactory.decodebytearray (b, 0, b.length);
- }
- return null;
- }
- //drawable converted to bitmap
- Public Bitmap Drawable2bitmap (drawable drawable) {
- Bitmap Bitmap = Bitmap
- . CreateBitmap (
- Drawable.getintrinsicwidth (),
- Drawable.getintrinsicheight (),
- Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565);
- Canvas canvas = new canvas (bitmap);
- Drawable.setbounds (0, 0, Drawable.getintrinsicwidth (),
- Drawable.getintrinsicheight ());
- Drawable.draw (canvas);
- return bitmap;
- }
- //Bitmap converted to drawable
- Public drawable bitmap2drawable (Bitmap Bitmap) {
- bitmapdrawable bd = new bitmapdrawable (bitmap);
- drawable d = (drawable) BD;
- return D;
- }
- }
Conversion Tool class "Go" between Android bitmap and drawable and byte[] and InputStream