在previewframe字节[]到int [] [英] onpreviewframe byte[] to int[]
问题描述
在previewframe我得到ImageFormat.RGB_565字节[]。
现在,我想转换此字节[]到int [],所以我可以做一些像素处理。
我怎么能这样做?
PS。到目前为止,我不喜欢这样,但它似乎很未经优化的:
上previewFrame公共无效(字节[]数据,摄像头摄像头){...
ByteBuffer的BF = ByteBuffer.wrap(数据); mBitmap = Bitmap.createBitmap(mWidth,mHeight,Bitmap.Config.RGB_565);
mBitmap.copyPixelsFromBuffer(BF);
然后我做这做让像素INT []:
INT bitmapArray [] =新的INT [originalWidth * originalHeight]。 mBitmap.getPixels(bitmapArray,0,originalWidth,0,0,
originalWidth,originalHeight);
}
我觉得code手动执行此操作应该大致如下:
的for(int i = 0; I< data.length;我+ = 2){
//来自两个字节重建16位RGB565值
INT RGB565 =(数据[1] - 安培; 255)| ((数据第[i + 1]&放大器; 255)所述;&下; 8); //提取原料成分值(0..31范围为G和B,0..63为G)
INT B5 = RGB565&安培; 0x1F的;
INT G6 =(RGB565>大于5)及0x3F的;
INT R5 =(RGB565>> 11)及0x1F的; //缩放组件升级到8位:
//左移,并在年底的最高位填补空位,
//所以00000延伸至000000000但11111延伸至11111111
INT B =(B5&下; 3;)| (B5>&→2);
INT G =(G6&下; 2)| (G6>→4);
INT R =(R5< 3;)| (R5>&→2); // RGB888的价值,存放于一个数组或缓冲...
INT RGB =(R<< 16)| (G<< 8)| b:
}
因此,它可能确实有一个中间位图快,除非你需要的颜色组件分开以后无论如何。
免责声明:我没有测试这一点。可以避免一些中间变量,但我想保持这种或多或少的可读性。
in on previewframe I get the byte[] in ImageFormat.RGB_565. Now I would like to convert this byte[] to int[] so I can do some pixel manipulation.
How could I do that?
ps. thus far I do it like this but it seems very unoptimized:
public void onPreviewFrame(byte[] data, Camera camera) { ...
ByteBuffer bf = ByteBuffer.wrap(data);
mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.RGB_565);
mBitmap.copyPixelsFromBuffer(bf);
and then I do this do get the pixels in int[]:
int bitmapArray[] = new int[originalWidth * originalHeight];
mBitmap.getPixels(bitmapArray, 0, originalWidth, 0, 0,
originalWidth, originalHeight);
}
I think the code to do this manually should look roughly like this:
for (int i = 0; i < data.length; i += 2) {
// Reconstruct 16 bit rgb565 value from two bytes
int rgb565 = (data[i] & 255) | ((data[i + 1] & 255) << 8);
// Extract raw component values (range 0..31 for g and b, 0..63 for g)
int b5 = rgb565 & 0x1f;
int g6 = (rgb565 >> 5) & 0x3f;
int r5 = (rgb565 >> 11) & 0x1f;
// Scale components up to 8 bit:
// Shift left and fill empty bits at the end with the highest bits,
// so 00000 is extended to 000000000 but 11111 is extended to 11111111
int b = (b5 << 3) | (b5 >> 2);
int g = (g6 << 2) | (g6 >> 4);
int r = (r5 << 3) | (r5 >> 2);
// The rgb888 value, store in an array or buffer...
int rgb = (r << 16) | (g << 8) | b;
}
So it may indeed be faster with an intermediate bitmap, unless you need the color components separately later on anyway.
Disclaimer: I did not test this. Some intermediate variables could be avoided, but I wanted to keep this more or less readable.
这篇关于在previewframe字节[]到int []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!