Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- package com.soai.imdemo;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.PixelFormat;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- /**
- * Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- * @author Administrator
- *
- */
- public class FormatTools {
- 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;
- }
- }
Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】的更多相关文章
- Android单位转换 (px、dp、sp之间的转换工具类)
在Android开发中,涉及到屏幕视频问题的时候,px.dp.sp之间的转换比较重要的一部分,所以杨哥整理了一个工具类给大伙用. package com.zw.express.tool; import ...
- android开发之dip,dp与px像素之间的转换工具,可能用的不多,但是有总比没有好吧。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,转载请说明出处. 下面是介绍: 免积分下载地址:http://download.csdn.net/de ...
- 如何在Byte[]和String之间进行转换
源自C#与.NET程序员面试宝典. 如何在Byte[]和String之间进行转换? 比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲.它是计算机物理内存保存的最基本单元. 字节(B):8个比特, ...
- Byte[]和BASE64之间的转换
一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...
- (转)Android中px与dip,sp与dip等的转换工具类
功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...
- Android中Bitmap、Drawable、byte[]转换
public byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); ...
- Android Bitmap和Drawable互转及使用BitmapFactory解析图片流
一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...
- Android 图片Bitmap,drawable,res资源图片之间转换
一.知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等.一种逐像素的显示对象,其执行效率高,但缺点也 ...
- Android颜色转换工具类ColorUtil
项目中需要根据ScrollView的滚动距离来动态设置Topbar的背景透明度,网上有类似的开源库FadingActionBar,使用的是ActionBar做的.而我的项目中并没有使用ActionBa ...
随机推荐
- C#学习基础总结
概念:.net与c#.net/dontnet:一般指.net framework框架,一种平台,一种技术c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一种技术又是一种编 ...
- SQL事务与并发
1.Transaction(事务)是什么: 事务是作为单一工作单元而执行的一系列操作.包括增删查改. 2.事务的种类: 事务分为显示事务和隐式事务: 隐式事务:就是平常我们使用每一条sql 语句就是一 ...
- html系列教程--DOCTYPE a area
<!DOCTYPE>标签:<!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令.在 HTML 4.01 中,& ...
- Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
参考数据库链接串: <add key="data" value="server=192.168.1.123; Port=3306; uid=root; pwd=ro ...
- Eclipse 浏览文件插件- OpenExplorer
http://blog.csdn.net/w709854369/article/details/6599167 EasyExplorer 是一个类似于 Windows Explorer的Eclips ...
- C# - 自定义 DataSet 的使用
------------------------------------------------- SellProdectManager.cs -------------------------- ...
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- EBS 开发中如何动态启用和禁止请求(Current Request)的参数
EBS 开发中如何动态启用和禁止请求(Current Request)的参数 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 我们可以使用依赖 ...
- background-size使用
background-size: left center | 0% 50% | cover | contain backgound-size: left center | 0% 50%; 这个指的是背 ...
- < meta > 元素 概要
< meta > 元素 概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 we ...