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, 0, 1024)) != -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(0, 0, 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. }

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】的更多相关文章

  1. Android单位转换 (px、dp、sp之间的转换工具类)

    在Android开发中,涉及到屏幕视频问题的时候,px.dp.sp之间的转换比较重要的一部分,所以杨哥整理了一个工具类给大伙用. package com.zw.express.tool; import ...

  2. android开发之dip,dp与px像素之间的转换工具,可能用的不多,但是有总比没有好吧。

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 下面是介绍: 免积分下载地址:http://download.csdn.net/de ...

  3. 如何在Byte[]和String之间进行转换

    源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...

  4. Byte[]和BASE64之间的转换

    一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...

  5. (转)Android中px与dip,sp与dip等的转换工具类

    功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...

  6. Android中Bitmap、Drawable、byte[]转换

    public byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); ...

  7. Android Bitmap和Drawable互转及使用BitmapFactory解析图片流

    一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...

  8. Android 图片Bitmap,drawable,res资源图片之间转换

    一.知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等.一种逐像素的显示对象,其执行效率高,但缺点也 ...

  9. Android颜色转换工具类ColorUtil

    项目中需要根据ScrollView的滚动距离来动态设置Topbar的背景透明度,网上有类似的开源库FadingActionBar,使用的是ActionBar做的.而我的项目中并没有使用ActionBa ...

随机推荐

  1. C#学习基础总结

    概念:.net与c#.net/dontnet:一般指.net framework框架,一种平台,一种技术c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一种技术又是一种编 ...

  2. SQL事务与并发

    1.Transaction(事务)是什么: 事务是作为单一工作单元而执行的一系列操作.包括增删查改. 2.事务的种类: 事务分为显示事务和隐式事务: 隐式事务:就是平常我们使用每一条sql 语句就是一 ...

  3. html系列教程--DOCTYPE a area

    <!DOCTYPE>标签:<!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令.在 HTML 4.01 中,& ...

  4. Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置

    参考数据库链接串: <add key="data" value="server=192.168.1.123; Port=3306; uid=root; pwd=ro ...

  5. Eclipse 浏览文件插件- OpenExplorer

    http://blog.csdn.net/w709854369/article/details/6599167 EasyExplorer  是一个类似于 Windows Explorer的Eclips ...

  6. C# - 自定义 DataSet 的使用

    -------------------------------------------------  SellProdectManager.cs  -------------------------- ...

  7. Php5.3的lambda函数以及closure(闭包)

    从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...

  8. EBS 开发中如何动态启用和禁止请求(Current Request)的参数

    EBS 开发中如何动态启用和禁止请求(Current Request)的参数 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 我们可以使用依赖 ...

  9. background-size使用

    background-size: left center | 0% 50% | cover | contain backgound-size: left center | 0% 50%; 这个指的是背 ...

  10. < meta > 元素 概要

    < meta > 元素 概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 we ...