博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android: Bitmap与DrawAble与byte[]与InputStream之间的转换
阅读量:4031 次
发布时间:2019-05-24

本文共 3767 字,大约阅读时间需要 12 分钟。

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;      }  }

转载地址:http://mqqbi.baihongyu.com/

你可能感兴趣的文章
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
jQuery性能优化指南
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>