我目前正在使用Javacv,它使用public void onPreviewFrame(byte[] data, Camera camera)
相机功能。
由于相机已被弃用,我一直在研究camera2和MediaProjection。这两个库都使用了ImageReader class。
目前我使用以下代码实例化这样的ImageReader
:
ImageReader.newInstance(DISPLAY_WIDTH, DISPLAY_HEIGHT, PixelFormat.RGBA_8888, 2);
并附上这样的OnImageAvailableListener
:
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
mBackgroundHandler.post(new processImage(reader.acquireNextImage()));
}
我尝试使用RGBA_8888
格式与Javacv按照这个线程:https://github.com/bytedeco/javacv/issues/298,但这对我不起作用。
因此,我正在考虑使用Render script将这些Images转换为NV21(YUV_420_SP)
格式(这是onPreviewFrame
函数中相机的默认输出),因为这对camera
库有用。
我也读过this one和this website这样的帖子进行转换,但这些对我来说不起作用,我担心它们会太慢。此外,我对C的了解非常有限。基本上看起来我想要https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB.html的反向操作
那你怎么能从Image
到一个匹配onPreviewFrame
函数输出的字节数组,即NV21(YUV_420_SP)
格式?最好使用Renderscript,因为它更快。
我尝试过使用ImageFormat.YUV_420_888
,但无济于事。我一直得到像The producer output buffer format 0x1 doesn't match the ImageReader's configured buffer format
这样的错误。我换回PixelFormat.RGBA_8888
,发现ImageObject
只有一架飞机。该平面的字节缓冲区大小为宽*高* 4(分别为R,G,B,A的一个字节)。所以我试着将其转换为NV21
格式。
我修改了this answer中的代码以生成以下函数:
void RGBtoNV21(byte[] yuv420sp, byte[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int A, R, G, B, Y, U, V;
int index = 0;
int rgbIndex = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
R = argb[rgbIndex++];
G = argb[rgbIndex++];
B = argb[rgbIndex++];
A = argb[rgbIndex++];
// RGB to YUV conversion according to
// https://en.wikipedia.org/wiki/YUV#Y.E2.80.B2UV444_to_RGB888_conversion
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor
// of 2 meaning for every 4 Y pixels there are 1 V and 1 U.
// Note the sampling is every other pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (i % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}
并使用以下方法调用它:
int mWidth = mImage.getWidth();
int mHeight = mImage.getHeight();
byte[] rgbaBytes = new byte[mWidth * mHeight * 4];
mImage.getPlanes()[0].getBuffer().get(rgbaBytes);
mImage.close();
byte[] yuv = new byte[mWidth * mHeight * 3 / 2];
RGBtoNV21(yuv, rgbaBytes, mWidth, mHeight);
在这里mImage
是我的Image
生产的ImageReader
物体。
然而,这会产生类似于此图像的结果:
这显然是畸形的。好像我的转换已关闭,但我无法确定究竟是什么。
@TargetApi(19)
public static byte[] yuvImageToByteArray(Image image) {
assert(image.getFormat() == ImageFormat.YUV_420_888);
int width = image.getWidth();
int height = image.getHeight();
Image.Plane[] planes = image.getPlanes();
byte[] result = new byte[width * height * 3 / 2];
int stride = planes[0].getRowStride();
assert (1 == planes[0].getPixelStride());
if (stride == width) {
planes[0].getBuffer().get(result, 0, width*height);
}
else {
for (int row = 0; row < height; row++) {
planes[0].getBuffer().position(row*stride);
planes[0].getBuffer().get(result, row*width, width);
}
}
stride = planes[1].getRowStride();
assert (stride == planes[2].getRowStride());
int pixelStride = planes[1].getPixelStride();
assert (pixelStride == planes[2].getPixelStride());
byte[] rowBytesCb = new byte[stride];
byte[] rowBytesCr = new byte[stride];
for (int row = 0; row < height/2; row++) {
int rowOffset = width*height + width/2 * row;
planes[1].getBuffer().position(row*stride);
planes[1].getBuffer().get(rowBytesCb);
planes[2].getBuffer().position(row*stride);
planes[2].getBuffer().get(rowBytesCr);
for (int col = 0; col < width/2; col++) {
result[rowOffset + col*2] = rowBytesCr[col*pixelStride];
result[rowOffset + col*2 + 1] = rowBytesCb[col*pixelStride];
}
}
return result;
}
我发布了类似要求的another function。这个新实现试图利用这样一个事实,即YUV_420_888通常只是伪装的NV21。