zoukankan      html  css  js  c++  java
  • Drawable、Bitmap、byte[]之间的转换

    1、Drawable → Bitmap

    1)

    Java代码  收藏代码
    1. public static Bitmap drawableToBitmap(Drawable drawable) {  
    2.           
    3.         Bitmap bitmap = Bitmap  
    4.                         .createBitmap(  
    5.                                         drawable.getIntrinsicWidth(),  
    6.                                         drawable.getIntrinsicHeight(),  
    7.                                         drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
    8.                                                         : Bitmap.Config.RGB_565);  
    9.         Canvas canvas = new Canvas(bitmap);  
    10.         //canvas.setBitmap(bitmap);  
    11.         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
    12.         drawable.draw(canvas);  
    13.         return bitmap;  
    14. }  

    2)

    Drawable d = ***;
    BitmapDrawable bd = (BitmapDrawable) d;
    Bitmap bm = bd.getBitmap(); 

    2、从资源中获取Bitmap

    Java代码  收藏代码
    1. Resources res=getResources();  
    2.   
    3. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  

    3、Bitmap → byte[]

    Java代码  收藏代码
    1. private byte[] Bitmap2Bytes(Bitmap bm){  
    2.     ByteArrayOutputStream baos = new ByteArrayOutputStream();    
    3.     bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
    4.     return baos.toByteArray();  
    5.    }  

     
    4、 byte[] → Bitmap

    Java代码  收藏代码
      1. private Bitmap Bytes2Bimap(byte[] b){  
      2.             if(b.length!=0){  
      3.                 return BitmapFactory.decodeByteArray(b, 0, b.length);  
      4.             }  
      5.             else {  
      6.                 return null;  
      7.             }  
      8.       }  

    原文:http://bingtian.iteye.com/blog/642128

    参考:http://blog.sina.com.cn/s/blog_4607d856010125sl.html

  • 相关阅读:
    Lodop客户端本地角色注册号常见误区
    Spring中加载xml配置文件的六种方式
    源程序出现各种奇怪的符号P
    MyEclipse项目中的java文件的图标变成空心的问题
    servlet中的相对路径和绝对路径 及/, ./, ../的区别
    Thread 与 Runnable
    Class.forName()用法详解
    chain.doFilter(request,response)含义
    jsp简单标签开发(一)
    createStatement()的用法
  • 原文地址:https://www.cnblogs.com/veins/p/3863796.html
Copyright © 2011-2022 走看看