我有一个性能问题。我目前正在开发一个实时应用程序,它需要以每秒 10 帧的速度处理相机图像。事实证明,我目前的瓶颈是转换为灰度(80 到 150 毫秒)。顺便说一句,我实际上需要一个灰度整数数组(一个像素一个整数)。所以可能饱和方法对我不起作用,但我对此并不完全确定。
我目前在做什么:
- 从相机预览中选择 YUV 数据
public void onPreviewFrame(byte[] data, Camera camera)
- 将 YUV 转换为彩色位图和灰度数组
static public void decodeYUV(int[] out,int[] outGrey, byte[] fg, int width, int height)
throws NullPointerException, IllegalArgumentException {
int sz = width * height;
if (out == null)
throw new NullPointerException("buffer out is null");
if (out.length < sz)
throw new IllegalArgumentException("buffer out size " + out.length
+ " < minimum " + sz);
if (fg == null)
throw new NullPointerException("buffer 'fg' is null");
if (fg.length < sz)
throw new IllegalArgumentException("buffer fg size " + fg.length
+ " < minimum " + sz * 3 / 2);
int i, j;
int Y, Cr = 0, Cb = 0;
for (j = 0; j < height; j++) {
int pixPtr = j * width;
final int jDiv2 = j >> 1;
for (i = 0; i < width; i++) {
Y = fg[pixPtr];
if (Y < 0)
Y += 255;
if ((i & 0x1) != 1) {
final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
Cb = fg[cOff];
if (Cb < 0)
Cb += 127;
else
Cb -= 128;
Cr = fg[cOff + 1];
if (Cr < 0)
Cr += 127;
else
Cr -= 128;
}
int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if (R < 0)
R = 0;
else if (R > 255)
R = 255;
int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1)
+ (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if (G < 0)
G = 0;
else if (G > 255)
G = 255;
int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if (B < 0)
B = 0;
else if (B > 255)
B = 255;
out[pixPtr] = 0xff000000 + (B << 16) + (G << 8) + R;
outGrey[pixPtr] = (int)(B*0.114f + R*0.299f + G*0.587f);
pixPtr++;
}
}
}
有没有更快的方法来达到这一点?
开放式简历?实际的相机图片捕获让操作系统完成工作?
我的下一步是线程化 YUV 解码。