Obtain/convert 到 YCbCr_420_SP (NV21) 来自相机的图像格式 API 函数 takePicture()
Obtain/convert to YCbCr_420_SP (NV21) image format from Camera API function takePicture()
我们正在从 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 实现吗?
更新: 在重新实现相机 class 以使用 camera2 API 之后,我们收到格式为 YUV_420_888 的图像对象。然后我们使用以下函数转换为 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 格式。
有两种方法可以改善您的结果。
可以使用jpeg-turbo库直接从Jpeg中获取YUV,函数为tjDecompressToYUV。
您可以使用 renderscript 将位图转换为 YUV。
哪一个更适合您,取决于设备。有些会为 Java 提供硬件加速的 Jpeg 解码器,有些会在软件中使用 libjpeg。在后一种情况下,tjDecompressToYUV 将带来显着的改进。
如果您的设备运行 Android-5 或更高版本,请考虑切换到 camera2 API,ImageReader 可能能够传送 YUV 或 RAW 图像所需的分辨率和质量。
我们正在从 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 实现吗?
更新: 在重新实现相机 class 以使用 camera2 API 之后,我们收到格式为 YUV_420_888 的图像对象。然后我们使用以下函数转换为 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 格式。
有两种方法可以改善您的结果。
可以使用jpeg-turbo库直接从Jpeg中获取YUV,函数为tjDecompressToYUV。
您可以使用 renderscript 将位图转换为 YUV。
哪一个更适合您,取决于设备。有些会为 Java 提供硬件加速的 Jpeg 解码器,有些会在软件中使用 libjpeg。在后一种情况下,tjDecompressToYUV 将带来显着的改进。
如果您的设备运行 Android-5 或更高版本,请考虑切换到 camera2 API,ImageReader 可能能够传送 YUV 或 RAW 图像所需的分辨率和质量。