关于Bitmap的问题。。

laolik 2015-10-10 09:59:34
我需要将一个byte数组转换为rgb565的bitmap,每两个数组元素拼成一个颜色值
按我的个人理解,这么做的(data为得到的byte数组):
int[] img_data = new int[data.length / 2];
for (int i = 0; i < data.length - 1; i += 2) {
int data1 = data[i];
int data2 = data[i + 1];
img_data[i / 2] = (data1 << 8) | data2;
// img_data[i / 2] = ((data1 << 24) >>> 16 ) | data2;
}

Bitmap bitmap = Bitmap.createBitmap(img_data, width, height,Config.RGB_565);

这么得到的图片不正常,有时覆盖绿色,有时黄色(效果图见下边)。。有知道是怎么回事的吗?
正常图片应该是这样的:

而生成的图片却是这样的:
...全文
238 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
laolik 2015-10-12
  • 打赏
  • 举报
回复
没法引用楼层了怎么。。。
Bitmap.createBitmap(width, height, config) 这个方法产生的是空的bitmap吧?最终还是得用颜色填充,于是又回到老问题了。。。
开发者_android 2015-10-10
  • 打赏
  • 举报
回复
引用 9 楼 laolik 的回复:
[quote=引用 7 楼 dalor 的回复:] [quote=引用 6 楼 laolik 的回复:] [quote=引用 5 楼 dalor 的回复:] [quote=引用 2 楼 laolik 的回复:] [quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。[/quote] 按664重新计算了一下rgb的颜色值,依然不行,由偏黄变为偏蓝了。。。感觉像是我的思路不对,或者说byte转int那一步不对。 求大神指教[/quote] 你不用这个create方法,换一个试试,不传color。[/quote] 目前的Bitmap和BitmapFactory都看了遍,没看到有适合这种情况的接口,好像必须得做个转换[/quote] Bitmap.createBitmap(width, height, config)这个方法不符合你的要求?
laolik 2015-10-10
  • 打赏
  • 举报
回复
引用 7 楼 dalor 的回复:
[quote=引用 6 楼 laolik 的回复:] [quote=引用 5 楼 dalor 的回复:] [quote=引用 2 楼 laolik 的回复:] [quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。[/quote] 按664重新计算了一下rgb的颜色值,依然不行,由偏黄变为偏蓝了。。。感觉像是我的思路不对,或者说byte转int那一步不对。 求大神指教[/quote] 你不用这个create方法,换一个试试,不传color。[/quote] 目前的Bitmap和BitmapFactory都看了遍,没看到有适合这种情况的接口,好像必须得做个转换
laolik 2015-10-10
  • 打赏
  • 举报
回复
引用 7 楼 dalor 的回复:
[quote=引用 6 楼 laolik 的回复:] [quote=引用 5 楼 dalor 的回复:] [quote=引用 2 楼 laolik 的回复:] [quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。[/quote] 按664重新计算了一下rgb的颜色值,依然不行,由偏黄变为偏蓝了。。。感觉像是我的思路不对,或者说byte转int那一步不对。 求大神指教[/quote] 你不用这个create方法,换一个试试,不传color。[/quote] 其他还有啥方法呢?
开发者_android 2015-10-10
  • 打赏
  • 举报
回复
引用 6 楼 laolik 的回复:
[quote=引用 5 楼 dalor 的回复:] [quote=引用 2 楼 laolik 的回复:] [quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。[/quote] 按664重新计算了一下rgb的颜色值,依然不行,由偏黄变为偏蓝了。。。感觉像是我的思路不对,或者说byte转int那一步不对。 求大神指教[/quote] 你不用这个create方法,换一个试试,不传color。
laolik 2015-10-10
  • 打赏
  • 举报
回复
引用 5 楼 dalor 的回复:
[quote=引用 2 楼 laolik 的回复:] [quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。[/quote] 按664重新计算了一下rgb的颜色值,依然不行,由偏黄变为偏蓝了。。。感觉像是我的思路不对,或者说byte转int那一步不对。 求大神指教
开发者_android 2015-10-10
  • 打赏
  • 举报
回复
引用 2 楼 laolik 的回复:
[quote=引用 1 楼 dalor 的回复:] 应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?[/quote] RGB565位数应该是 6 6 4。你试下。
laolik 2015-10-10
  • 打赏
  • 举报
回复
引用 3 楼 lj19851227 的回复:
RGB的取值范围是0~255,应该都是需要8位吧
按理说,RGB24才是各占8位,565的话,RGB分别5、6、5位才是。。不过按这样理解的写出来的代码转换的又不对,晕菜了
peter_nj_RD 2015-10-10
  • 打赏
  • 举报
回复
RGB的取值范围是0~255,应该都是需要8位吧
laolik 2015-10-10
  • 打赏
  • 举报
回复
引用 1 楼 dalor 的回复:
应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)
如果是RGB565的话,舍弃最高8位? 难道。。。安卓中RGB565的R、G、B都是各占8位?
开发者_android 2015-10-10
  • 打赏
  • 举报
回复
应该是RGB565不支持每个像素的alpha通道,所以colors数组中的alpha位被忽略。 下面是官文中的说明: colors Array of Color used to initialize the pixels. This array must be at least as large as width * height. width The width of the bitmap height The height of the bitmap config The bitmap config to create. If the config does not support per-pixel alpha (e.g. RGB_565), then the alpha bytes in the colors[] will be ignored (assumed to be FF)

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧