zoukankan      html  css  js  c++  java
  • Android中Bitmap对象和字节流之间的相互转换

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

     

  • 相关阅读:
    ASP.NET Core 静态资源的打包与压缩
    算法
    字符串反转
    js 获取随机数
    AspNetCore MVC 跨域
    add digits
    1-bit and 2-bit Characters
    删除字符串中出现次数最少的字符
    洗牌
    哈夫曼编码
  • 原文地址:https://www.cnblogs.com/yelanggu/p/10622313.html
Copyright © 2011-2022 走看看