【发布时间】:2019-03-03 14:14:58
【问题描述】:
我们从 takePicture() 函数获取我们的图像 - 这里我们使用 jpeg 回调(第三个参数),因为我们无法获得原始图像 - 即使在设置了回调缓冲区到最大大小。所以图像被压缩为 JPEG 格式 - 另一方面,我们需要我们的图像与预览帧的格式相同:YCbCr_420_SP (NV21) (这种格式是我们使用的第三方库所期望的,我们没有重新实现的资源)
我们在使用setPictureFormat()初始化相机时尝试在参数中设置图片格式,可惜没有用。我猜这个函数只适用于原始回调。
我们可以访问JNI端的OpenCV C库,但是不知道如何用IplImage实现转换。
所以目前我们正在使用以下java实现进行转换,它的性能非常差(对于尺寸为3840x2160的图片大约需要2秒):
byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
int [] argb = new int[inputWidth * inputHeight];
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte [] yuv = new byte[inputWidth*inputHeight*3/2];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
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 (j % 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 ++;
}
}
}
有人知道在 OpenCV C 的帮助下转换会是什么样子,或者可以提供更高效的 java 实现吗?
更新:在重新实现相机类以使用 camera2 API 后,我们将收到格式为 YUV_420_888 的 Image 对象。我们正在使用以下函数转换为 NV21:
private static byte[] YUV_420_888toNV21(Image image) {
byte[] nv21;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
nv21 = new byte[ySize + uSize + vSize];
//U and V are swapped
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
return nv21;
}
虽然此函数可与 cameraCaptureSessions.setRepeatingRequest 一起正常工作,但在调用 cameraCaptureSessions.capture 时会出现分段错误。两者都通过 ImageReader 请求 YUV_420_888 格式。
【问题讨论】:
标签: java android opencv camera java-native-interface