1. android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
  2.  
    import java.io.BufferedOutputStream;  
  3.  
    import java.io.ByteArrayOutputStream;  
  4.  
    import java.io.File;  
  5.  
    import java.io.FileOutputStream;  
  6.  
    import java.io.IOException;  
  7.  
    import java.io.InputStream;  
  8.  
      
  9.  
    import android.graphics.Bitmap;  
  10.  
    import android.graphics.BitmapFactory;  
  11.  
    import android.graphics.Matrix;  
  12.  
      
  13.  
    public class ImageDispose {  
  14.  
          
  15.  
          
  16.  
          
  17.  
        /** 
  18.  
         * @param 将图片内容解析成字节数组 
  19.  
         * @param inStream 
  20.  
         * @return byte[] 
  21.  
         * @throws Exception 
  22.  
         */  
  23.  
        public static byte[] readStream(InputStream inStream) throws Exception {  
  24.  
            byte[] buffer = new byte[1024];  
  25.  
            int len = -1;  
  26.  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  27.  
            while ((len = inStream.read(buffer)) != -1) {  
  28.  
                outStream.write(buffer, 0, len);  
  29.  
            }  
  30.  
            byte[] data = outStream.toByteArray();  
  31.  
            outStream.close();  
  32.  
            inStream.close();  
  33.  
            return data;  
  34.  
      
  35.  
        }  
  36.  
        /** 
  37.  
         * @param 将字节数组转换为ImageView可调用的Bitmap对象 
  38.  
         * @param bytes 
  39.  
         * @param opts 
  40.  
         * @return Bitmap 
  41.  
         */  
  42.  
        public static Bitmap getPicFromBytes(byte[] bytes,  
  43.  
                BitmapFactory.Options opts) {  
  44.  
            if (bytes != null)  
  45.  
                if (opts != null)  
  46.  
                    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,  
  47.  
                            opts);  
  48.  
                else  
  49.  
                    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  
  50.  
            return null;  
  51.  
        }  
  52.  
        /** 
  53.  
         * @param 图片缩放 
  54.  
         * @param bitmap 对象 
  55.  
         * @param w 要缩放的宽度 
  56.  
         * @param h 要缩放的高度 
  57.  
         * @return newBmp 新 Bitmap对象 
  58.  
         */  
  59.  
        public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){  
  60.  
            int width = bitmap.getWidth();  
  61.  
            int height = bitmap.getHeight();  
  62.  
            Matrix matrix = new Matrix();  
  63.  
            float scaleWidth = ((float) w / width);  
  64.  
            float scaleHeight = ((float) h / height);  
  65.  
            matrix.postScale(scaleWidth, scaleHeight);  
  66.  
            Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,  
  67.  
                    matrix, true);  
  68.  
            return newBmp;  
  69.  
        }  
  70.  
          
  71.  
        /** 
  72.  
         * 把Bitmap转Byte 
  73.  
         */  
  74.  
        public static byte[] Bitmap2Bytes(Bitmap bm){  
  75.  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  76.  
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  77.  
            return baos.toByteArray();  
  78.  
        }  
  79.  
        /** 
  80.  
         * 把字节数组保存为一个文件 
  81.  
         */  
  82.  
        public static File getFileFromBytes(byte[] b, String outputFile) {  
  83.  
            BufferedOutputStream stream = null;  
  84.  
            File file = null;  
  85.  
            try {  
  86.  
                file = new File(outputFile);  
  87.  
                FileOutputStream fstream = new FileOutputStream(file);  
  88.  
                stream = new BufferedOutputStream(fstream);  
  89.  
                stream.write(b);  
  90.  
            } catch (Exception e) {  
  91.  
                e.printStackTrace();  
  92.  
            } finally {  
  93.  
                if (stream != null) {  
  94.  
                    try {  
  95.  
                        stream.close();  
  96.  
                    } catch (IOException e1) {  
  97.  
                        e1.printStackTrace();  
  98.  
                    }  
  99.  
                }  
  100.  
            }  
  101.  
            return file;  
  102.  
        }  
  103.  
              
  104.  
    }  

Android中Bitmap对象和字节流之间的相互转换的更多相关文章

  1. Android中Bitmap对象和字节流之间的相互转换(转)

    android 将图片内容解析成字节数组:将字节数组转换为ImageView可调用的Bitmap对象:图片缩放:把字节数组保存为一个文件:把Bitmap转Byte import java.io.Buf ...

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

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

  3. JS 中 JSON 对象与字符串之间的相互转换

    在开发的过程中,如果对于少量参数的前后台传递,可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,这样后台 接受的时候Request多个很麻烦 ...

  4. Json数组操作小记 及 JSON对象和字符串之间的相互转换

    [{"productid":"1","sortindex":"2"},{"productid":&q ...

  5. Android中传递对象的三种方法

    Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者I ...

  6. 【转】Android中dip(dp)与px之间单位转换

    Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...

  7. 在android中实现webview与javascript之间的交互(转)

    参见“在android中实现webview与javascript之间的交互”

  8. android中Bitmap的放大和缩小的方法

    android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34  CSDN博客原文  http://blog.csdn.net/ada168855/article/det ...

  9. JSON对象与字符串之间的相互转换

    <html> <head> <meta name="viewport" content="width=device-width" ...

随机推荐

  1. Eclipse中SVN插件的安装和配置(在线安装)

    公司项目中用到了svn来管理项目,然后需要在Eclipse中进行配置.网上参考了很多资料,离线安装的方式装上了,但是导入项目后报错,可能是离线安装包的问题.然后又采用了Eclipse在线安装的方式,总 ...

  2. 【Spring】16、注解事务 @Transactional

    概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...

  3. Retrofit2 原理解析

    Retrofit是什么 官网介绍是A type-safe HTTP client for Android and Java,是一个 RESTful 的 HTTP 网络请求框架的封装,但网络请求不是Re ...

  4. CSS实现移动端横向滑动

    html: <div class="chosen-container"> <div class="chosen-swiper"> < ...

  5. 09-HTML-form标签

    <html> <head>  <title>form标签学习</title>  <meta charset="utf-8"/& ...

  6. Tomcat post参数长处理

    如下图所示:增加maxPostSize="-1"属性即可

  7. VysorPro助手

    Vysor是一款非常强大而又好用的Android远程显示及控制软件,有Chrome插件版.Windows客户端版和Mac版,是Android开发和测试人员的必备神器.其中Windows客户端版相对Ch ...

  8. (后端)项目中的错误之java中判断字符里面含有某些字符

    数据库的数据出现了数据错误.找到原因是因为代码里面Spring的判断所导致的.其实就是判断字符里有01,走这里,有02,走那里,全是if,但是是类似indexOf的那种判断,偏偏有一个数据是0102, ...

  9. Javascript数组系列三之迭代方法2

    今天我们来继续 Javascript 数组系列的文章,上文 <Javascript数组系列二之迭代方法1> 我们说到一些数组的迭代方法,我们在开发项目实战的过程中熟练的使用可以大大提高我们 ...

  10. 「客户成功故事」OneAPM 助力网上办事大厅构建阳光、高效、安全的政务服务平台

    (一) 项目背景: 网上办事大厅是由省信息中心承建的电子政务核心业务系统,致力于为全省民众提供一站式网上办事服务,实现了政务信息网上公开.法人及个人事项网上办理.公共决策网上互动.政府效能网上监督五大 ...