Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
来源:互联网 发布:u盘怎么恢复数据 编辑:程序博客网 时间:2024/06/15 12:34
Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
<span style="font-size:18px;">public class FormatTransformUtil { private static FormatTools tools = new FormatTools(); public static FormatTools getInstance() { if (tools == null) { tools = new FormatTools(); return tools; } return tools; } // 将byte[]转换成InputStream public InputStream Byte2InputStream(byte[] b) { ByteArrayInputStream bais = new ByteArrayInputStream(b); return bais; } // 将InputStream转换成byte[] public byte[] InputStream2Bytes(InputStream is) { String str = ""; byte[] readByte = new byte[1024]; int readCount = -1; try { while ((readCount = is.read(readByte, 0, 1024)) != -1) { str += new String(readByte).trim(); } return str.getBytes(); } catch (Exception e) { e.printStackTrace(); } return null; } // 将Bitmap转换成InputStream public InputStream Bitmap2InputStream(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is; } // 将Bitmap转换成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; } // 将InputStream转换成Bitmap public Bitmap InputStream2Bitmap(InputStream is) { return BitmapFactory.decodeStream(is); } // Drawable转换成InputStream public InputStream Drawable2InputStream(Drawable d) { Bitmap bitmap = this.drawable2Bitmap(d); return this.Bitmap2InputStream(bitmap); } // InputStream转换成Drawable public Drawable InputStream2Drawable(InputStream is) { Bitmap bitmap = this.InputStream2Bitmap(is); return this.bitmap2Drawable(bitmap); } // Drawable转换成byte[] public byte[] Drawable2Bytes(Drawable d) { Bitmap bitmap = this.drawable2Bitmap(d); return this.Bitmap2Bytes(bitmap); } // byte[]转换成Drawable public Drawable Bytes2Drawable(byte[] b) { Bitmap bitmap = this.Bytes2Bitmap(b); return this.bitmap2Drawable(bitmap); } // Bitmap转换成byte[] public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } // byte[]转换成Bitmap public Bitmap Bytes2Bitmap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } return null; } // Drawable转换成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转换成Drawable public Drawable bitmap2Drawable(Bitmap bitmap) { BitmapDrawable bd = new BitmapDrawable(bitmap); Drawable d = (Drawable) bd; return d; } }</span>
0 0
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- android Bitmap与Drawable与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- android Bitmap与Drawable与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- 【android】bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Linux基础知识学习:Linux中修改环境变量及使环境变量生效的方法
- 算法-树(2)—深入红黑树
- java中构造函数总结
- 模仿Toast实现提示框
- Bitmap优化
- Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
- 【UVa】455 - Periodic Strings
- Stanford机器学习__Lecture notes CS229. Logistic Regression(逻辑回归)(2)Perceptron Learning Algorithm
- Java.lang包
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)
- leetcode 2:Add Two Numbers(C语言)
- HashMap源码剖析
- 2016最新版App Store应用审核指南完整版
- C++Primer(第六章课后习题)(程序题源代码)