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 = Bitmap
112                 .createBitmap(width, height, Bitmap.Config.ARGB_8888);
113         bmp.copyPixelsFromBuffer(ByteBuffer.wrap(Bits));
114         
115         return bmp;
116     }
117     
118     /**
119      * 8位灰度转Bitmap
120      * 
121      * @param stream
122      *            输入流
123      * @param width
124      *            图像宽度
125      * @param height
126      *            图像高度
127      * @return
128      */
129     public static Bitmap convert8bit(InputStream stream, int width, int height)
130     {
131         return convert8bit(readByteArrayFormStream(stream), width, height);
132     }
133     
134     /**
135      * 24位灰度转Bitmap
136      * 
137      * @param data
138      *            输入流
139      * @param width
140      *            图像宽度
141      * @param height
142      *            图像高度
143      * @return
144      */
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));

 

转载于:https://www.cnblogs.com/chenrui7/p/4561020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值