
正文
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位图
实现如下:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer; import android.graphics.Bitmap; /**
* Raw转Bitmap
*
* @author ChenRui
*
*/
public class RawToBitMap
{
/**
* 从流中读取数组
*
* @param stream
* 输入流
* @return
*/
public static byte[] readByteArrayFormStream(InputStream stream)
{
try
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int len = 0;
byte[] tmp = new byte[1024];
while ((len = stream.read(tmp)) != -1)
{
outStream.write(tmp, 0, len);
} byte[] data = outStream.toByteArray(); return data;
}
catch (IOException e)
{
e.printStackTrace();
return new byte[0];
}
} /**
* 8位灰度转Bitmap
*
* 图像宽度必须能被4整除
*
* @param data
* 裸数据
* @param width
* 图像宽度
* @param height
* 图像高度
* @return
*/
public static Bitmap convert8bit(byte[] data, int width, int height)
{
byte[] Bits = new byte[data.length * 4]; //RGBA 数组 int i;
for (i = 0; i < data.length; i++)
{
// 原理:4个字节表示一个灰度,则RGB = 灰度值,最后一个Alpha = 0xff;
Bits[i * 4] = Bits[i * 4 + 1] = Bits[i * 4 + 2] = data[i];
Bits[i * 4 + 3] = -1; //0xff
} // Bitmap.Config.ARGB_8888 表示:图像模式为8位
Bitmap bmp = Bitmap
.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(ByteBuffer.wrap(Bits)); return bmp;
} /**
* 24位灰度转Bitmap
*
* 图像宽度必须能被4整除
*
* @param data
* 裸数据
* @param width
* 图像宽度
* @param height
* 图像高度
* @return
*/
public static Bitmap convert24bit(byte[] data, int width, int height)
{
byte[] Bits = new byte[data.length * 4]; //RGBA 数组 int i; // data.length / 3 表示 3位为一组
for (i = 0; i < data.length / 3; i++)
{
// 原理:24位是有彩色的,所以要复制3位,最后一位Alpha = 0xff;
Bits[i * 4] = data[i * 3];
Bits[i * 4 + 1] = data[i * 3 + 1];
Bits[i * 4 + 2] = data[i * 3 + 2];
Bits[i * 4 + 3] = -1;
} // Bitmap.Config.ARGB_8888 表示:图像模式为8位
Bitmap bmp = Bitmap
.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(ByteBuffer.wrap(Bits)); return bmp;
} /**
* 8位灰度转Bitmap
*
* @param stream
* 输入流
* @param width
* 图像宽度
* @param height
* 图像高度
* @return
*/
public static Bitmap convert8bit(InputStream stream, int width, int height)
{
return convert8bit(readByteArrayFormStream(stream), width, height);
} /**
* 24位灰度转Bitmap
*
* @param data
* 输入流
* @param width
* 图像宽度
* @param height
* 图像高度
* @return
*/
public static Bitmap convert24bit(InputStream stream, int width, int height)
{
return convert24bit(readByteArrayFormStream(stream), width, height);
}
}
显示时宽度和高度必须匹配正确。
ImageView1.setImageBitmap(RawToBitMap.convert8bit(getAssets().open("8bit.raw"), 512, 512));







