Oce*_*lue 7 android android-image
我正在从网络上读取原始图像.该图像已由图像传感器读取,而不是从文件读取.
这些是我对图像的了解:
〜高度和宽度
〜总大小(以字节为单位)
~8位灰度
~1字节/像素
我正在尝试将此图像转换为位图以在imageview中显示.
这是我试过的:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.outHeight = shortHeight; //360
opt.outWidth = shortWidth;//248
imageBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageSize, opt);
Run Code Online (Sandbox Code Playgroud)
decodeByteArray返回null,因为它无法解码我的图像.
我也尝试直接从输入流中读取它,而不是先将其转换为字节数组:
imageBitmap = BitmapFactory.decodeStream(imageInputStream, null, opt);
Run Code Online (Sandbox Code Playgroud)
这也返回null.
我在这个和其他论坛上搜索过,但找不到实现这个目标的方法.
有任何想法吗?
编辑:我应该补充说,我做的第一件事是检查流是否实际包含原始图像.我使用其他应用程序(iPhone/Windows MFC)执行此操作,他们能够读取并正确显示图像.我只需要想办法在Java/Android中做到这一点.
Sev*_*yev 14
Android不支持灰度位图.首先,您必须将每个字节扩展为32位ARGB int.Alpha是0xff,R,G和B字节是源图像的字节像素值的副本.然后在该数组的顶部创建位图.
另外(见注释),似乎设备认为0是白色,1是黑色 - 我们必须反转源位.
因此,我们假设源图像位于名为Src的字节数组中.这是代码:
byte [] src; //Comes from somewhere...
byte [] bits = new byte[src.length*4]; //That's where the RGBA array goes.
int i;
for(i=0;i<src.length;i++)
{
bits[i*4] =
bits[i*4+1] =
bits[i*4+2] = ~src[i]; //Invert the source bits
bits[i*4+3] = 0xff; // the alpha.
}
//Now put these nice RGBA pixels into a Bitmap object
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(ByteBuffer.wrap(bits));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18868 次 |
| 最近记录: |