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

简介:

http://blog.csdn.net/z104207/article/details/6634774


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


相关文章
|
15天前
|
存储 Java API
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
66 0
|
29天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
2月前
|
存储 安全 数据管理
Linux文件时间戳:解析时区与修改时间的相互作用
Linux文件时间戳:解析时区与修改时间的相互作用
48 2
|
2月前
|
算法 Linux C++
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
34 0
|
2月前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
21 0
|
1天前
|
Android开发
Android中Glide加载Https图片失败的解决方案
Android中Glide加载Https图片失败的解决方案
9 1
|
7天前
|
弹性计算 运维 Shell
|
11天前
|
移动开发 数据可视化 Linux
Linux 中的文件与目录管理解析
当谈到Linux系统,文件与目录管理是其中最基本和重要的部分之一。Linux提供了一种强大而灵活的方式来组织和管理文件和目录,让用户能够轻松地访问和操作系统中的各种数据。上一节我们说到文件的属性,本文将详细介绍Linux中的文件与目录管理的各个方面。
|
11天前
|
Linux Go 数据安全/隐私保护
Linux 中的文件属性解析
在 Linux 系统中,每个文件和目录有一组属性控制其操作和访问权限。了解这些属性对有效管理文件至关重要。文件属性包括:文件类型(如 `-` 表示普通文件,`d` 表示目录),权限(如 `rwx` 表示所有者权限,`r-x` 表示组和其他用户权限),所有者,组,硬链接数,文件大小和最后修改时间。通过 `chown` 和 `chmod` 命令可更改文件所有者、所属组及权限。此外,还有特殊权限(如 SUID、SGID)和 ACL(访问控制列表)提供更精细的访问控制。
|
19天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
24 1

推荐镜像

更多