Android Bitmap與DrawAble與byte[]與InputStream之間的轉換工具類【轉】

[java] view plaincopyprint?

  1. package com.soai.imdemo;  

  2.   

  3. import java.io.ByteArrayInputStream;  

  4. import java.io.ByteArrayOutputStream;  

  5. import java.io.InputStream;  

  6.   

  7. import android.graphics.Bitmap;  

  8. import android.graphics.BitmapFactory;  

  9. import android.graphics.Canvas;  

  10. import android.graphics.PixelFormat;  

  11. import android.graphics.drawable.BitmapDrawable;  

  12. import android.graphics.drawable.Drawable;  

  13.   

  14. /** 

  15.  * Bitmap與DrawAble與byte[]與InputStream之間的轉換工具類 

  16.  * @author Administrator 

  17.  * 

  18.  */  

  19. public class FormatTools {  

  20.     private static FormatTools tools = new FormatTools();  

  21.   

  22.     public static FormatTools getInstance() {  

  23.         if (tools == null) {  

  24.             tools = new FormatTools();  

  25.             return tools;  

  26.         }  

  27.         return tools;  

  28.     }  

  29.   

  30.     // 將byte[]轉換成InputStream  

  31.     public InputStream Byte2InputStream(byte[] b) {  

  32.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  

  33.         return bais;  

  34.     }  

  35.   

  36.     // 將InputStream轉換成byte[]  

  37.     public byte[] InputStream2Bytes(InputStream is) {  

  38.         String str = "";  

  39.         byte[] readByte = new byte[1024];  

  40.         int readCount = -1;  

  41.         try {  

  42.             while ((readCount = is.read(readByte, 01024)) != -1) {  

  43.                 str += new String(readByte).trim();  

  44.             }  

  45.             return str.getBytes();  

  46.         } catch (Exception e) {  

  47.             e.printStackTrace();  

  48.         }  

  49.         return null;  

  50.     }  

  51.   

  52.     // 將Bitmap轉換成InputStream  

  53.     public InputStream Bitmap2InputStream(Bitmap bm) {  

  54.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  55.         bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  

  56.         InputStream is = new ByteArrayInputStream(baos.toByteArray());  

  57.         return is;  

  58.     }  

  59.   

  60.     // 將Bitmap轉換成InputStream  

  61.     public InputStream Bitmap2InputStream(Bitmap bm, int quality) {  

  62.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  63.         bm.compress(Bitmap.CompressFormat.PNG, quality, baos);  

  64.         InputStream is = new ByteArrayInputStream(baos.toByteArray());  

  65.         return is;  

  66.     }  

  67.   

  68.     // 將InputStream轉換成Bitmap  

  69.     public Bitmap InputStream2Bitmap(InputStream is) {  

  70.         return BitmapFactory.decodeStream(is);  

  71.     }  

  72.   

  73.     // Drawable轉換成InputStream  

  74.     public InputStream Drawable2InputStream(Drawable d) {  

  75.         Bitmap bitmap = this.drawable2Bitmap(d);  

  76.         return this.Bitmap2InputStream(bitmap);  

  77.     }  

  78.   

  79.     // InputStream轉換成Drawable  

  80.     public Drawable InputStream2Drawable(InputStream is) {  

  81.         Bitmap bitmap = this.InputStream2Bitmap(is);  

  82.         return this.bitmap2Drawable(bitmap);  

  83.     }  

  84.   

  85.     // Drawable轉換成byte[]  

  86.     public byte[] Drawable2Bytes(Drawable d) {  

  87.         Bitmap bitmap = this.drawable2Bitmap(d);  

  88.         return this.Bitmap2Bytes(bitmap);  

  89.     }  

  90.   

  91.     // byte[]轉換成Drawable  

  92.     public Drawable Bytes2Drawable(byte[] b) {  

  93.         Bitmap bitmap = this.Bytes2Bitmap(b);  

  94.         return this.bitmap2Drawable(bitmap);  

  95.     }  

  96.   

  97.     // Bitmap轉換成byte[]  

  98.     public byte[] Bitmap2Bytes(Bitmap bm) {  

  99.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  

  100.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  

  101.         return baos.toByteArray();  

  102.     }  

  103.   

  104.     // byte[]轉換成Bitmap  

  105.     public Bitmap Bytes2Bitmap(byte[] b) {  

  106.         if (b.length != 0) {  

  107.             return BitmapFactory.decodeByteArray(b, 0, b.length);  

  108.         }  

  109.         return null;  

  110.     }  

  111.   

  112.     // Drawable轉換成Bitmap  

  113.     public Bitmap drawable2Bitmap(Drawable drawable) {  

  114.         Bitmap bitmap = Bitmap  

  115.                 .createBitmap(  

  116.                         drawable.getIntrinsicWidth(),  

  117.                         drawable.getIntrinsicHeight(),  

  118.                         drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  

  119.                                 : Bitmap.Config.RGB_565);  

  120.         Canvas canvas = new Canvas(bitmap);  

  121.         drawable.setBounds(00, drawable.getIntrinsicWidth(),  

  122.                 drawable.getIntrinsicHeight());  

  123.         drawable.draw(canvas);  

  124.         return bitmap;  

  125.     }  

  126.   

  127.     // Bitmap轉換成Drawable  

  128.     public Drawable bitmap2Drawable(Bitmap bitmap) {  

  129.         BitmapDrawable bd = new BitmapDrawable(bitmap);  

  130.         Drawable d = (Drawable) bd;  

  131.         return d;  

  132.     }  

  133. }  

轉載自:http://my.oschina.net/547217475/blog/93485

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章