c++ - 如何将 OpenCV 集成到 gradle 构建的 C++ 项目中

标签 c++ opencv gradle

我设置了一个hello world 项目here这基本上是 gradle 项目中样本的复制/粘贴 here .

我的下一步是 hello world 但针对 OpenCV 框架。它将读取内存中的图片并显示它。准确地说,main.cpp 会更像那个

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

如您所见,我包含了 OpenCV header ,但我正在努力通过 gradle 添加库本身。我可能必须使用 prebuilt方式,但我失败了。

最佳答案

我认为您需要配置 header 和预建库链接:

我通常在android中做,但我不能保证它有效,因为我不知道你的gradle插件是什么。 但是您可以将其用作线索,具体取决于您的 gradle 版本,语法可能会发生变化。

首先定义你的opencv目录:

opencv.dir="the site where opencv headers are"

定义你的 opencv 库后,定义你的 headers.srcDir

repositories {
  libs(PrebuiltLibraries) {
    opencv_core {
    headers.srcDir "your opencv.dir"
      binaries.withType(StaticLibraryBinary) { binary ->
        // This closure will be executed once for every buildType/platform combination
        def variantDir = binary.targetPlatform.architecture.name == 'i386' ? 'Win32' : 'x64'
        staticLibraryFile = "path/to/library/${variantDir}/opencv_core.so"
      }
    }
    opencv_another_lib {
    headers.srcDir "your opencv.dir"
      binaries.withType(StaticLibraryBinary) { binary ->
        // This closure will be executed once for every buildType/platform combination
        def variantDir = binary.targetPlatform.architecture.name == 'i386' ? 'Win32' : 'x64'
        staticLibraryFile = "path/to/library/${variantDir}/opencv_another_lib.so"
      }
    }
  }

可能,例如opencv_core会有更多的3rparty依赖,尝试以同样的方式添加。

希望对您有所帮助。

干杯。

乌奈。

关于c++ - 如何将 OpenCV 集成到 gradle 构建的 C++ 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39477382/

相关文章:

c++ - 交叉编译——Windows 上的 V8 和 Linux

c++ - 优化行为的差异

python - 我尝试从 picamera 获取 2 帧。为什么它的工作速度很慢?

java - 为什么 Gradle 'build' 任务不是最新的,即使源代码没有变化?

c++ - 如何知道启动应用程序的可执行文件名称?

c++ - 如果在运行时添加两个按钮,mfc 按钮将显示不完整

python - 如何正确设置 OpenCV python 中的二进制掩码?

c++ - Windows XP 上的 OpenCV GetTickCount64 错误

java - Eclipse Gradle 插件未安装

android - 将我的应用程序连接到 Firebase 时出现问题