我已经实现了一个启动相机并使用 JNI 接口(interface)将所有预览缓冲区向下发送到 native 组件的 android 应用程序。由于预览数据是 NV21 图像格式,我需要从中创建一个 cv::Mat 实例。我搜索了它并找到了以下解决方案:
cv::Mat _yuv(height, width, CV_8UC1, (uchar *) imagebuffer);
where imagebuffer is jbyte*
但是,不要在输出图像中得到预期的图像。里面全是一些随机的线条等等。有谁知道我到底该怎么做?
最佳答案
您需要将 YUV 图像转换为 RGBA 图像。
cv::Mat _yuv(height+height/2, width, CV_8UC1, (uchar *)imagebuffer);
cv::cvtColor(_yuv, _yuv, CV_YUV2RGBA_NV21);
通常,YUV 图像是具有 1.5*height
的 1 channel 图像(如果它是 RGB 或灰度图像)。
或者您可以创建一个新的 Mat 并将 jint 数组传递给 native 函数并使用该数组设置位图的像素。
jint *_out = env->GetIntArrayElements(out, 0);
cv::Mat _yuv(height + height/2, width, CV_8UC1, (uchar*)imagebuffer);
cv::Mat _rgba(height, width, CV_8UC4, (uchar *)_out);
cv::cvtColor(_yuv, _rgba, CV_YUV2RGBA_NV21);
env->ReleaseIntArrayElements(out, _out, 0);
在 Java 中,
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
pixels = new int[width * height];
native_function(height, width, bytedata, pixels);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
关于android - opencv C++ 从 android NV21 图像数据缓冲区创建 Mat 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122957/