android Bitmap与Drawable与byte[]与InputStream之间的转换工具类

[java]  view plain copy
  1. package com.shntec.xmm.platform;  
  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. public class FormatTools {  
  15.  private static FormatTools tools = new FormatTools();  
  16.   
  17.  private FormatTools() {  
  18.  }  
  19.   
  20.  public static FormatTools getInstance() {  
  21.   if (tools == null) {  
  22.    tools = new FormatTools();  
  23.    return tools;  
  24.   }  
  25.   return tools;  
  26.  }  
  27.   
  28.  // 将byte[]转换成InputStream  
  29.  public InputStream Byte2InputStream(byte[] b) {  
  30.   ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  31.   return bais;  
  32.  }  
  33.   
  34.  // 将InputStream转换成byte[]  
  35.  public byte[] InputStream2Bytes(InputStream is) {  
  36.   String str = "";  
  37.   byte []readByte = new byte[1024];  
  38.   int readCount = -1;  
  39.   try {  
  40.    while ((readCount = is.read(readByte,0,1024)) != -1) {  
  41.     str += new String(readByte).trim();  
  42.    }  
  43.    return str.getBytes();  
  44.   } catch (Exception e) {  
  45.    e.printStackTrace();  
  46.   }  
  47.   return null;  
  48.  }  
  49.   
  50.  // 将Bitmap转换成InputStream  
  51.  public InputStream Bitmap2InputStream(Bitmap bm) {  
  52.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  53.   bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  54.   InputStream is = new ByteArrayInputStream(baos.toByteArray());  
  55.   return is;  
  56.  }  
  57.   
  58.  // 将Bitmap转换成InputStream  
  59.  public InputStream Bitmap2InputStream(Bitmap bm, int quality) {  
  60.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  61.   bm.compress(Bitmap.CompressFormat.PNG, quality, baos);  
  62.   InputStream is = new ByteArrayInputStream(baos.toByteArray());  
  63.   return is;  
  64.  }  
  65.  // 将InputStream转换成Bitmap  
  66.  public Bitmap InputStream2Bitmap(InputStream is) {  
  67.   return BitmapFactory.decodeStream(is);  
  68.  }  
  69.   
  70.  // Drawable转换成InputStream  
  71.  public InputStream Drawable2InputStream(Drawable d) {  
  72.   Bitmap bitmap = this.drawable2Bitmap(d);  
  73.   return this.Bitmap2InputStream(bitmap);  
  74.  }  
  75.   
  76.  // InputStream转换成Drawable  
  77.  public Drawable InputStream2Drawable(InputStream is) {  
  78.   Bitmap bitmap = this.InputStream2Bitmap(is);  
  79.   return this.bitmap2Drawable(bitmap);  
  80.  }  
  81.   
  82.  // Drawable转换成byte[]  
  83.  public byte[] Drawable2Bytes(Drawable d) {  
  84.   Bitmap bitmap = this.drawable2Bitmap(d);  
  85.   return this.Bitmap2Bytes(bitmap);  
  86.  }  
  87.   
  88.  // byte[]转换成Drawable  
  89.  public Drawable Bytes2Drawable(byte[] b) {  
  90.   Bitmap bitmap = this.Bytes2Bitmap(b);  
  91.   return this.bitmap2Drawable(bitmap);  
  92.  }  
  93.   
  94.  // Bitmap转换成byte[]  
  95.  public byte[] Bitmap2Bytes(Bitmap bm) {  
  96.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  97.   bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  98.   return baos.toByteArray();  
  99.  }  
  100.   
  101.  // byte[]转换成Bitmap  
  102.  public Bitmap Bytes2Bitmap(byte[] b) {  
  103.   if (b.length != 0) {  
  104.    return BitmapFactory.decodeByteArray(b, 0, b.length);  
  105.   }  
  106.   return null;  
  107.  }  
  108.   
  109.  // Drawable转换成Bitmap  
  110.  public Bitmap drawable2Bitmap(Drawable drawable) {  
  111.   Bitmap bitmap = Bitmap  
  112.     .createBitmap(  
  113.       drawable.getIntrinsicWidth(),  
  114.       drawable.getIntrinsicHeight(),  
  115.       drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  116.         : Bitmap.Config.RGB_565);  
  117.   Canvas canvas = new Canvas(bitmap);  
  118.   drawable.setBounds(00, drawable.getIntrinsicWidth(),  
  119.     drawable.getIntrinsicHeight());  
  120.   drawable.draw(canvas);  
  121.   return bitmap;  
  122.  }  
  123.   
  124.  // Bitmap转换成Drawable  
  125.  public Drawable bitmap2Drawable(Bitmap bitmap) {  
  126.   BitmapDrawable bd = new BitmapDrawable(bitmap);  
  127.   Drawable d = (Drawable) bd;  
  128.   return d;  
  129.  }  
  130. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值