如何从原始图像中的位图 [英] How to get a Bitmap from a raw image
问题描述
我读来自网络的原始图像。这个图像已经由图像传感器读取,而不是从文件中。
I am reading a raw image from the network. This image has been read by an image sensor, not from a file.
这些事情是我知道的形象:
〜身高和放大器;宽度
〜总大小(以字节为单位)
〜8位灰度
〜1个字节/像素
These are the things I know about the image:
~ Height & Width
~ Total size (in bytes)
~ 8-bit grayscale
~ 1 byte/pixel
我想这个图像转换为位图,以在ImageView的显示。
I'm trying to convert this image to a bitmap to display in an imageview.
下面是我的尝试:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.outHeight = shortHeight; //360
opt.outWidth = shortWidth;//248
imageBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageSize, opt);
德codeByteArray回报空,因为它不能去code我的形象。
decodeByteArray returns null, since it cannot decode my image.
我也试过直接从输入流中读取它,没有它转换为一个字节数组第一:
I also tried reading it directly from the input stream, without converting it to a Byte Array first:
imageBitmap = BitmapFactory.decodeStream(imageInputStream, null, opt);
这将返回空和
我搜索这个和放大器;其他论坛,但无法找到一种方法来实现这一目标。
I've searched on this & other forums, but cannot find a way to achieve this.
任何想法?
编辑:我要补充的是,第一件事是检查是否流实际上包含了原始图像。我这样做是使用其他应用程序`(iPhone / Windows的MFC)及所以能读它并正确显示图像。我只需要想出一个办法做到这一点的Java / Android的。
I should add that the first thing I did was to check if the stream actually contains the raw image. I did this using other applications `(iPhone/Windows MFC) & they are able to read it and display the image correctly. I just need to figure out a way to do this in Java/Android.
推荐答案
Android不支持灰度位图。所以第一件事情,你必须每一个字节扩展到32位ARGB INT。 Alpha是0xff的,并且R,G和B是源图像的字节的像素值的副本。然后在该阵列上面创建位图。
Android does not support grayscale bitmaps. So first thing, you have to extend every byte to an 32-bit ARGB int. Alpha is 0xff, and R, G and B are copies of the source image's byte pixel value. Then create the bitmap on top of that array.
另外(见注释),似乎该设备认为0是白色的,1是黑 - 我们有反转源位
Also (see comments), it seems that the device thinks that 0 is white, 1 is black - we have to invert the source bits.
所以,让我们假设源图像是名为src字节数组中开始。这里的code:
So, let's assume that the source image is in the byte array called Src. Here's the code:
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] = -1;//0xff, that's 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));
这篇关于如何从原始图像中的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!