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之间的转换工具类【转】的更多相关文章

  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. java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag

    异常原因:缺少jstl.jar包.一般在新建Web项目是jstl一项不要选none就不会出现这个问题.(还有可能就是缺少servlet-api.jar)

  2. oracle tablespace

    oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...

  3. Lua中强大的元方法__index详解

    今天要来介绍比较好玩的内容:__index元方法 我是备胎,记得回头看看 咳咳,相信每一位女生都拥有或者不知不觉中拥有了一些备胎,啊!当然,又或许是成为过别人的备胎. 没有备胎的人,就不是完整的人生. ...

  4. return break continue 的区别

    1)break     直接跳出当前的循环,从当前循环外面开始执行,忽略循环体中任何其他语句和循环条件测试.他只能跳出一层循环,如果你的循环是嵌套循环,那么你需要按照你嵌套的层次,逐步使用break来 ...

  5. 加密传输SSL协议2_传统加密

    原本到了睡觉的时间,但是做完了SSL的笔记还有GCC和Oracle等的好多的笔记,所以刻苦一点. The Priciple of Encryption/Decryption --conventiona ...

  6. Linux网络管理——子网掩码

    1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...

  7. Java判断字符串是否为空的三种方法

    方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.1: if(s == null || s.equals("")); 方法二: 比较字符串长度, 效率高, 是我知道的最 ...

  8. EPiServer网文

    ListItemCollection Rending: http://joelabrahamsson.com/episerver-7-and-mvc-how-to-customize-renderin ...

  9. MBG 相关资源链接

    MyBatis Generator(MBG)相关资源链接 http://mbg.cndocs.tk/quickstart.html http://www.mybatis.tk/ http://git. ...

  10. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)

    目的: 1. 通过网页读取watchdog的信息 2. 通过网页设置watchdog 准备工作: 1. 选择一个web框架,选用 cherrypy $ sudo apt-get install pyt ...