android 将图片内容解析成字节数组,将字节数组转换为Ima ...

来源:互联网 发布:中华大字典软件 编辑:程序博客网 时间:2024/05/21 12:48

http://www.eoeandroid.com/thread-91650-1-1.html


  1. package com.bingo.util;

  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. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Matrix;

  11. public class ImageDispose {
  12.         
  13.         
  14.         
  15.         /**
  16.          * @param 将图片内容解析成字节数组
  17.          * @param inStream
  18.          * @return byte[]
  19.          * @throws Exception
  20.          */
  21.         public static byte[] readStream(InputStream inStream) throws Exception {
  22.                 byte[] buffer = new byte[1024];
  23.                 int len = -1;
  24.                 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  25.                 while ((len = inStream.read(buffer)) != -1) {
  26.                         outStream.write(buffer, 0, len);
  27.                 }
  28.                 byte[] data = outStream.toByteArray();
  29.                 outStream.close();
  30.                 inStream.close();
  31.                 return data;

  32.         }
  33.         /**
  34.          * @param 将字节数组转换为ImageView可调用的Bitmap对象
  35.          * @param bytes
  36.          * @param opts
  37.          * @return Bitmap
  38.          */
  39.         public static Bitmap getPicFromBytes(byte[] bytes,
  40.                         BitmapFactory.Options opts) {
  41.                 if (bytes != null)
  42.                         if (opts != null)
  43.                                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
  44.                                                 opts);
  45.                         else
  46.                                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  47.                 return null;
  48.         }
  49.         /**
  50.          * @param 图片缩放
  51.          * @param bitmap 对象
  52.          * @param w 要缩放的宽度
  53.          * @param h 要缩放的高度
  54.          * @return newBmp 新 Bitmap对象
  55.          */
  56.         public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){
  57.                 int width = bitmap.getWidth();
  58.                 int height = bitmap.getHeight();
  59.                 Matrix matrix = new Matrix();
  60.                 float scaleWidth = ((float) w / width);
  61.                 float scaleHeight = ((float) h / height);
  62.                 matrix.postScale(scaleWidth, scaleHeight);
  63.                 Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
  64.                                 matrix, true);
  65.                 return newBmp;
  66.         }
  67.         
  68.         /**
  69.      * 把Bitmap转Byte
  70.      * @Author HEH
  71.      * @EditTime 2010-07-19 上午11:45:56
  72.      */
  73.         public static byte[] Bitmap2Bytes(Bitmap bm){
  74.                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
  75.                 bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  76.                 return baos.toByteArray();
  77.         }
  78.         /**
  79.      * 把字节数组保存为一个文件
  80.      * @Author HEH
  81.      * @EditTime 2010-07-19 上午11:45:56
  82.      */
  83.     public static File getFileFromBytes(byte[] b, String outputFile) {
  84.         BufferedOutputStream stream = null;
  85.         File file = null;
  86.         try {
  87.             file = new File(outputFile);
  88.             FileOutputStream fstream = new FileOutputStream(file);
  89.             stream = new BufferedOutputStream(fstream);
  90.             stream.write(b);
  91.         } catch (Exception e) {
  92.             e.printStackTrace();
  93.         } finally {
  94.             if (stream != null) {
  95.                 try {
  96.                     stream.close();
  97.                 } catch (IOException e1) {
  98.                     e1.printStackTrace();
  99.                 }
  100.             }
  101.         }
  102.         return file;
  103.     }
  104.                 
  105. }

0 2