用android处理图片的时候,由于得到的资源不一样,所以经常要在各种格式中相互转化,以下介绍了 Bitmap Drawable byte[] InputStream 之间的转换方法:


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中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  2. Android中Bitmap, Drawable, Byte之间的转化

    1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap. ...

  3. Android bitmap转byte

    逆转start协议输出 private static byte[] bitmap2byte(Bitmap bmp) throws Exception{ return bitmap2byte(bmp, ...

  4. Android Bitmap Drawable 常用摘要

    1.缩放 public Bitmap scalingBitmap(Bitmap bitmap, int newW, int newH) { int w = bitmap.getWidth(); int ...

  5. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  6. Bitmap byte[] InputStream Drawable 互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...

  7. 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]

    原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...

  8. Android Drawable、Bitmap、byte[]之间的转换

    转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...

  9. Bitmap和Drawable相互转换方法

    很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换.下面给大家两种比较简单高效的方法. 一.Bitmap转Drawable Bitmap bm=xxx; //xxx根 ...

随机推荐

  1. c# winform播放声音的两种方式

    axWindowsMediaPlayer1.settings.setMode("", true); axWindowsMediaPlayer1.URL = "skcg.m ...

  2. [bug]不包含“AsNoTracking”的定义

    摘要 在使用ef做查询优化的时候我们会用到AsNoTracking方法,但如果不引入命名空间,你就会出现不包含“AsNoTracking”的定义的错误. 解决办法 引入命名空间:System.Data ...

  3. Java虚拟机 - 语法糖

    [深入Java虚拟机]之六:Java语法糖 语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家Peter.J.Landin发明的一个术语,指在计算机语言中添加的某种语法,这种语 ...

  4. TFS 打得你措手不及!TF53001:管理员已取消数据库操作

    心塞.公司TFS突然挂了.签入获取 一直报 TF53001:管理员已取消数据库操作.公司开发部开发进度一下就受阻了.刚好有时关键时期. 在 老总的帮助下根据搜到的资料 .搞定了这个问题!问题出在数据库 ...

  5. 转载文章CSS3的calc()使用

    calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性 ...

  6. 使用PHPExcel实现数据批量导出为excel表格

    首先需要下载PHPExecel类文件,帮助文档可以参考PHPExcel中文帮助手册|PHPExcel使用方法. 下面直接上例子,是我自己写的一个简单的批量导出数据为excel的例子 前台页面 比较简单 ...

  7. MySQL两种存储引擎: MyISAM和InnoDB

    MySQL两种存储引擎: MyISAM和InnoDB 简单总结   MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Me ...

  8. BZOJ5323:[JXOI2018]游戏

    传送门 不难发现,所有不能被其他数筛掉的数是一定要选的,只有选了这些数字才能结束 假设有 \(m\) 个,枚举结束时间 \(x\),答案就是 \(\sum \binom{x-1}{m-1}m!(n-m ...

  9. CSS中文乱码解决方法

    原文链接:http://caibaojian.com/css-unicode.html 我的CSS里面有一个content用到了中文,作用主要是在前端日报文章中显示出“网页链接”这四个字,然而打开百度 ...

  10. 数据库查询字段为null 时,返回0

    oracle select nvl(字段名,0) from 表名; sqlserver select isnull(字段名,0) from 表名; mysql select ifnull(字段名,0) ...