Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

  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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值