有一个RGB24图像的数据存储在一个Byte[]里,如何将其显示到界面?
解决方案 »
- 关于数据库更新的问题
- 安卓音乐播放器运行时候出现如下情况,不能播放音乐
- android应该嵌入广告
- 关于响应button的单击事件响应问题,很诡异
- 请问android gingerbread下如何用命令行修改休眠时间的长短?
- 请问下 怎么设置AlertDialog的宽度呢?
- android 关闭进程问题 killBackgroundProcesses无效
- post数据总有问题,大神来看一下
- Eclipse中看不到ActivityThread.java的源码
- 安装了android studio,能启动模拟器但是运行程序却不能检测到模拟器怎么回事?
- 求教:Android 闹钟程序如何显示最近闹钟的时间
- 怎么设置EditView的默认值,要浅色的,不能编辑的那种,急
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView i = new ImageView(this);
// 设置图片给ImageView
i.setImageBitmap(bitmap);
Config里好像没有RGB24类型的
int []colors = convertByteToColor(data);
if (colors == null){
return null;
}
Bitmap bmp = Bitmap.createBitmap(colors, 0, width, width, height,
Bitmap.Config.ARGB_8888);
return bmp;
}
// 将一个byte数转成int
// 实现这个函数的目的是为了将byte数当成无符号的变量去转化成int
public int convertByteToInt(byte data){
int heightBit = (int) ((data>>4) & 0x0F);
int lowBit = (int) (0x0F & data);
return heightBit * 16 + lowBit;
}
// 将纯RGB数据数组转化成int像素数组
public int[] convertByteToColor(byte[] data){
int size = data.length;
if (size == 0){
return null;
}
int arg = 0;
if (size % 3 != 0){
arg = 1;
}
// 一般情况下data数组的长度应该是3的倍数,这里做个兼容,多余的RGB数据用黑色0XFF000000填充
int []color = new int[size / 3 + arg];
int red, green, blue;
if (arg == 0){
for(int i = 0; i < color.length; ++i){
red = convertByteToInt(data[i * 3]);
green = convertByteToInt(data[i * 3 + 1]);
blue = convertByteToInt(data[i * 3 + 2]);
// 获取RGB分量值通过按位或生成int的像素值
color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;
}
}else{
for(int i = 0; i < color.length - 1; ++i){
red = convertByteToInt(data[i * 3]);
green = convertByteToInt(data[i * 3 + 1]);
blue = convertByteToInt(data[i * 3 + 2]);
color[i] = (red << 16) | (green << 8) | blue | 0xFF000000;
}
color[color.length - 1] = 0xFF000000;
} return color;
}
}这个类可以转