星空网 > 软件开发 > 操作系统

Android raw to bmp

Android raw 格式转 bmp 图像

 

raw 保存的为裸数据,转换时都需要把它转成RGBA 的方式来显示。其中:

   8位RAW: 四位RGBA 来表示一位灰度;

  24位RAW: 三位RGB相同,A(alpha)用0XFF表示。

 

 

Bitmap.Config 枚举说明:

  位图位数越高代表其可以存储的颜色信息越多,当然图像也就越逼真。

 

  • ALPHA_8        代表8位Alpha位图
  • ARGB_4444    代表16位ARGB位图
  • ARGB_8888    代表32位ARGB位图
  • RGB_565        代表8位RGB位图

 

 

实现如下

 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.nio.ByteBuffer; 7  8 import android.graphics.Bitmap; 9  10 /** 11  * Raw转Bitmap 12  *  13  * @author ChenRui 14  *  15 */ 16 public class RawToBitMap 17 { 18   /** 19    * 从流中读取数组 20    *  21    * @param stream 22    *      输入流 23    * @return 24   */ 25   public static byte[] readByteArrayFormStream(InputStream stream) 26   { 27     try 28     { 29       ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 30       int len = 0; 31       byte[] tmp = new byte[1024]; 32       while ((len = stream.read(tmp)) != -1) 33       { 34         outStream.write(tmp, 0, len); 35       } 36        37       byte[] data = outStream.toByteArray(); 38        39       return data; 40     } 41     catch (IOException e) 42     { 43       e.printStackTrace(); 44       return new byte[0]; 45     } 46   } 47    48   /** 49    * 8位灰度转Bitmap 50    *  51    * 图像宽度必须能被4整除 52    *  53    * @param data 54    *      裸数据 55    * @param width 56    *      图像宽度 57    * @param height 58    *      图像高度 59    * @return 60   */ 61   public static Bitmap convert8bit(byte[] data, int width, int height) 62   { 63     byte[] Bits = new byte[data.length * 4]; //RGBA 数组 64      65     int i; 66     for (i = 0; i < data.length; i++) 67     { 68       // 原理:4个字节表示一个灰度,则RGB = 灰度值,最后一个Alpha = 0xff; 69       Bits[i * 4] = Bits[i * 4 + 1] = Bits[i * 4 + 2] = data[i]; 70       Bits[i * 4 + 3] = -1; //0xff 71     } 72      73     // Bitmap.Config.ARGB_8888 表示:图像模式为8位 74     Bitmap bmp = Bitmap 75         .createBitmap(width, height, Bitmap.Config.ARGB_8888); 76     bmp.copyPixelsFromBuffer(ByteBuffer.wrap(Bits)); 77      78     return bmp; 79   } 80    81   /** 82    * 24位灰度转Bitmap 83    *  84    * 图像宽度必须能被4整除 85    *  86    * @param data 87    *      裸数据 88    * @param width 89    *      图像宽度 90    * @param height 91    *      图像高度 92    * @return 93   */ 94   public static Bitmap convert24bit(byte[] data, int width, int height) 95   { 96     byte[] Bits = new byte[data.length * 4]; //RGBA 数组 97      98     int i; 99     100     // data.length / 3 表示 3位为一组101     for (i = 0; i < data.length / 3; i++)102     {103       // 原理:24位是有彩色的,所以要复制3位,最后一位Alpha = 0xff;104       Bits[i * 4] = data[i * 3];105       Bits[i * 4 + 1] = data[i * 3 + 1];106       Bits[i * 4 + 2] = data[i * 3 + 2];107       Bits[i * 4 + 3] = -1;108     }109     110     // Bitmap.Config.ARGB_8888 表示:图像模式为8位111     Bitmap bmp = Bitmap112         .createBitmap(width, height, Bitmap.Config.ARGB_8888);113     bmp.copyPixelsFromBuffer(ByteBuffer.wrap(Bits));114     115     return bmp;116   }117   118   /**119    * 8位灰度转Bitmap120    * 121    * @param stream122    *      输入流123    * @param width124    *      图像宽度125    * @param height126    *      图像高度127    * @return128   */129   public static Bitmap convert8bit(InputStream stream, int width, int height)130   {131     return convert8bit(readByteArrayFormStream(stream), width, height);132   }133   134   /**135    * 24位灰度转Bitmap136    * 137    * @param data138    *      输入流139    * @param width140    *      图像宽度141    * @param height142    *      图像高度143    * @return144   */145   public static Bitmap convert24bit(InputStream stream, int width, int height)146   {147     return convert24bit(readByteArrayFormStream(stream), width, height);148   }149 }

 

 显示时宽度和高度必须匹配正确。

ImageView1.setImageBitmap(RawToBitMap.convert8bit(getAssets().open("8bit.raw"), 512, 512));

 




原标题:Android raw to bmp

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

备运提单:https://www.ikjzd.com/w/1573153239080542209
白俄罗斯:https://www.ikjzd.com/w/1573153239676133377
比利时电商:https://www.ikjzd.com/w/1573153240234319873
Bellnet:https://www.ikjzd.com/w/1573153243870781441
草莓蛋糕网:https://www.ikjzd.com/w/1573153247930523649
BESC认证:https://www.ikjzd.com/w/1573153256550162434
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流