如何在安卓中载入并获取单通道16bit PNG 图片的像素值

废柴杰瑞 2018-07-16 10:29:50
当我直接用“BitmapFactory.decodeStream” 函数载入单通道16bit PNG 图片 并利用“getPixel”获取每个点的像素值。
得到的像素值都是类似"-16250872"数字。

Bitmap bmp = BitmapFactory.decodeStream(<...data input stream...>);
pixel_value = bmp.getPixel(j, i);

安卓中bitmap的格式好像都是RGBA类型的,请问怎么才能正确的提取出PNG图像中各点的像素组成一个矩阵。
...全文
179 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
废柴杰瑞 2018-07-18
  • 打赏
  • 举报
回复
引用 1 楼 gl74gs48 的回复:
如果从位图工厂直接加载16位greylevel PNG像素格式将是不正确的。您需要使用一个RGBA 32位颜色格式设置像素格式ARGB_8888。然后,你必须得有与getPixel(I,J)的所有像素并屏蔽与0xFFFF的整数值。通过这种方式,您将获得预期的16位值。

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig= Bitmap.Config.ARGB_8888;
Bitmap bmp=BitmapFactory.decodeStream(entity.getContent(),null,opt);
int W=bmp.getWidth();
int H=bmp.getHeight();
int px;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
{
px= bmp.getPixel(j, i)&0x0000ffff;//in px you will find a value between 0 and 65535
...
}
}


感谢回复,我之前试过这种方法,这种方法的确可以将像素归一化到0-2^16 的范围中。但是得到的像素值与原始值并不一样。
比如原始像素值为 305 305 306 305 305 302 300 299 297 296 294 294 294
转换后全部变成了 257 257 257 257 257 257 257 257 257 257 257 257 257.
请问如何获取准确的像素值。
废柴杰瑞 2018-07-18
  • 打赏
  • 举报
回复
感谢回复,我之前试过这种方法,这种方法的确可以将像素归一化到0-2^16 的范围中。但是得到的像素值与原始值并不一样。
比如原始像素值为 305 305 306 305 305 302 300 299 297 296 294 294 294
转换后全部变成了 257 257 257 257 257 257 257 257 257 257 257 257 257.
请问如何获取准确的像素值。
阿甘1976 2018-07-17
  • 打赏
  • 举报
回复
如果从位图工厂直接加载16位greylevel PNG像素格式将是不正确的。您需要使用一个RGBA 32位颜色格式设置像素格式ARGB_8888。然后,你必须得有与getPixel(I,J)的所有像素并屏蔽与0xFFFF的整数值。通过这种方式,您将获得预期的16位值。

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig= Bitmap.Config.ARGB_8888;
Bitmap bmp=BitmapFactory.decodeStream(entity.getContent(),null,opt);
int W=bmp.getWidth();
int H=bmp.getHeight();
int px;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
{
px= bmp.getPixel(j, i)&0x0000ffff;//in px you will find a value between 0 and 65535
...
}
}

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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