Android Drawable、Bitmap、byte[]之间的转换

来源:互联网 发布:优化相机 编辑:程序博客网 时间:2024/05/17 04:50
1、Drawable --> Bitmap

[java] view plaincopy
  1.     Bitmap drawable2Bitmap(Drawable drawable) {  
  2.         if (drawable instanceof BitmapDrawable) {  
  3.             return ((BitmapDrawable) drawable).getBitmap();  
  4.         } else if (drawable instanceof NinePatchDrawable) {  
  5.             Bitmap bitmap = Bitmap  
  6.                     .createBitmap(  
  7.                             drawable.getIntrinsicWidth(),  
  8.                             drawable.getIntrinsicHeight(),  
  9.                             drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  10.                                     : Bitmap.Config.RGB_565);  
  11.             Canvas canvas = new Canvas(bitmap);  
  12.             drawable.setBounds(00, drawable.getIntrinsicWidth(),  
  13.                     drawable.getIntrinsicHeight());  
  14.             drawable.draw(canvas);  
  15.             return bitmap;  
  16.         } else {  
  17.             return null;  
  18.         }  
  19.     }  


2、从资源中获取的Drawable --> Bitmap

[java] view plaincopy
  1.     Resources res = getResources();  
  2.     Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pic);  


3、Bitmap --> Drawable

[java] view plaincopy
  1.     Drawable bitmap2Drawable(Bitmap bitmap) {  
  2.         return new BitmapDrawable(bitmap);  
  3.     }  

4、Bitmap --> byte[]

[java] view plaincopy
  1.     byte[] Bitmap2Bytes(Bitmap bm) {  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  4.         return baos.toByteArray();  
  5.     }  


5、 byte[] --> Bitmap

[java] view plaincopy
  1.     Bitmap Bytes2Bimap(byte[] b) {  
  2.         if (b.length != 0) {  
  3.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  4.         } else {  
  5.             return null;  
  6.         }  
  7.     }



    1. /** 
    2.      * 得到本地或者网络上的bitmap url - 网络或者本地图片的绝对路径,比如: 
    3.      *  
    4.      * A.网络路径: url="http://blog.foreverlove.us/girl2.png" ; 
    5.      *  
    6.      * B.本地路径:url="file://mnt/sdcard/photo/image.png"; 
    7.      *  
    8.      * C.支持的图片格式 ,png, jpg,bmp,gif等等 
    9.      *  
    10.      * @param url 
    11.      * @return 
    12.      */  
    13.     public static Bitmap GetLocalOrNetBitmap(String url)  
    14.     {  
    15.         Bitmap bitmap = null;  
    16.         InputStream in = null;  
    17.         BufferedOutputStream out = null;  
    18.         try  
    19.         {  
    20.             in = new BufferedInputStream(new URL(url).openStream(), Constant.IO_BUFFER_SIZE);  
    21.             final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();  
    22.             out = new BufferedOutputStream(dataStream, Constant.IO_BUFFER_SIZE);  
    23.             copy(in, out);  
    24.             out.flush();  
    25.             byte[] data = dataStream.toByteArray();  
    26.             bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
    27.             data = null;  
    28.             return bitmap;  
    29.         }  
    30.         catch (IOException e)  
    31.         {  
    32.             e.printStackTrace();  
    33.             return null;  
    34.         }  

    1.     }  
    2. 说明:Constant.IO_BUFFER_SIZE 是一个常量而已,可以改成常数,比如2*1024,其实取决于你的图片大小,自己根据图片的大小自己设定吧。。。。


      附加的copy函数

      private static void copy(InputStream in, OutputStream out)
                  throws IOException {
              byte[] b = new byte[IO_BUFFER_SIZE];
              int read;
              while ((read = in.read(b)) != -1) {
                  out.write(b, 0, read);
              }
          }

     
0 0
原创粉丝点击