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. Unix awk的流程控制BEGIN和END的讲解

    你可能对Unix比较熟悉,但你可能对Unix awk很陌生,这一点也不奇怪,的确,与其优秀的功能相比,awk还远没达到它应有的知名度. 流程控制语句是任何程序设计语言都不能缺少的部分.任何好的语言都有 ...

  2. linux的 .bashrc文件是干什么的?

    使用man bash命令查看到的联机帮助文件中的相关解释如下: .bashrc - The individual per-interactive-shell startup file. 这个文件主要保 ...

  3. viewer.js 视图预览demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. CSS笔记1:属性--定位

    相对定位是“相对于”元素在文档中的初始位置,而绝对定位是“相对于”最近的已定位祖先元素,如果不存在已定位的祖先元素,那么“相对于”最初的包含块. 元素定位 属性 版本 继承 描述 position c ...

  5. SpringBoot设置文件上传大小限制--默认为1M

    SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeL ...

  6. JS之onunload、onbeforeunload事件详解

    简介 onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来调用.区别在于onbeforeunload在o ...

  7. Android包管理机制(二)PackageInstaller安装APK

    前言 在本系列上一篇文章Android包管理机制(一)PackageInstaller的初始化中我们学习了PackageInstaller是如何初始化的,这一篇文章我们接着学习PackageInsta ...

  8. [20190214]11g Query Result Cache RC Latches.txt

    [20190214]11g Query Result Cache RC Latches.txt --//昨天我重复链接http://www.pythian.com/blog/oracle-11g-qu ...

  9. [20181124]关于降序索引问题2.txt

    [20181124]关于降序索引问题2.txt --//链接:blog.itpub.net/267265/viewspace-2221425/,探讨降序索引中索引的键值.--//实际上使用函数sys_ ...

  10. margin的两个有趣现象:margin合并和margin塌陷

    margin合并 当两个元素在垂直方向并列,分别设置margin值时会发生一个margin合并的现象 举个例子,有两个div,垂直并列,box1设置margin-bottom:20px,box2设置m ...