zoukankan      html  css  js  c++  java
  • 从网上收集的一些图片方面的操作

    android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:

     

    1、Drawable → Bitmap

    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(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
    12.         drawable.draw(canvas);  
    13.         return bitmap;  
    14. }  

     

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

     

  • 相关阅读:
    【LeetCode】650. 只有两个键的键盘
    【LeetCode】70. 爬楼梯
    【LeetCode】746. 使用最小花费爬楼梯
    【LeetCode】198. 打家劫舍
    【LeetCode】53.最大子序和
    【LeetCode】1056-易混淆数
    【人工智能系列】python的Quepy库的学习
    【计划书】关于运营商的人工智能的想法
    【转】经典!python中使用xlrd、xlwt操作excel表格详解
    转]python 结巴分词(jieba)学习
  • 原文地址:https://www.cnblogs.com/weeds/p/2425603.html
Copyright © 2011-2022 走看看