我最近实施了类似的事情。为了实现这一目标,我使用 OpenCV 设置了一个 Android JNI 项目,并使用 OpenCV 相机读取功能来存储帧。然后,可以将指向帧数据的指针传递给 CCV 图像包装器,以便与 CCV 库函数一起使用。 CCV 具有最小的依赖性,最简单的启动和运行方法是将所需模块的源代码包含在项目的 JNI 目录中。
要使用 OpenCV 设置项目,您可以按照本教程。 http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.htmlOpenCV SDK 有一个用于简单相机读取器的示例项目。 Android GitHub 页面包含一个示例 HelloJNI 项目here https://github.com/googlesamples/android-ndk/tree/master/hello-jni其中展示了如何使用 JNI 通过 Java 和 C/C++ 设置 Android 项目。然后可以将 CCV 源添加到 C/C++ 源目录,以便您的 C/C++ 函数可以访问该库。
一旦您使用 OpenCV 库和 JNI 功能设置了项目,就需要使用 OpenCV 保存帧数据并将其指针传递给 C 代码。将每一帧存储为Mat
对象,即Mat
然后可以将对象传递给您的 C/C++ 代码,如下所示:(请注意,这只是显示所需关键代码段的摘录)
package your.namespace.here;
import org.opencv.core.Core;
import org.opencv.core.Mat;
public class MainActivity{
// Store frames in this object for later processing
Mat frame;
static {
// Load the c file name with JNI bindings, e.g. here we load test.c
System.loadLibrary("test");
}
// Declare the JNI function wrapper
public native int ccvTest( long input, long output);
// OpenCV methods here to store the frame, see
// OpenCV4Android - tutorial-1-camerapreview for full
// code description.
//...
// This function to be called after each frame is stored.
// output can then be converted to Bitmap and displayed in ImageView
// or used for further processing with OpenCV.
public Mat processFrame(){
Mat output = new Mat();
ccvTest(frame.getNativeObjAddr(), output.getNativeObjAddr());
return output;
}
}
使用 HelloJNI 模板,调用 CCV 库函数之一的示例 C 文件(在本示例中我们将其称为 test.c)将如下所示:
#include <string.h>
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
// ccv files to include should be compiled using c compiler
#include "ccv/lib/ccv.h"
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL
Java_your_namespace_here_MainActivity_ccvTest( JNIEnv* env,
jobject thiz,
jlong input, jlong output)
{
Mat* in_p = (Mat*)input;
Mat* out_p = (Mat*)output;
Mat &rgb = *in_p;
ccv_dense_matrix_t* image = 0;
// Pass the Mat data to the CCV Image wrapper
ccv_read(rgb.data, &image, CCV_IO_BGR_RAW | CCV_IO_ANY_RAW | CCV_IO_GRAY, rgb.rows, rgb.cols, rgb.step[0]);
// Now the Mat is in ccv image format, you can pass
// the image pointer to any ccv function you like.
//
// Put your calls to CCV library here..
//
}
#ifdef __cplusplus
}
#endif
项目的树结构可能与此类似,所有 ccv 源都位于 jni/ccv 文件夹中:
此设置非常有用,因为它允许您连接 OpenCV 和 CCV 的功能。希望这可以帮助。