前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android利用SurfaceView显示Camera图像爬坑记(五) -- 在现有项目中加入NDK配置

Android利用SurfaceView显示Camera图像爬坑记(五) -- 在现有项目中加入NDK配置

作者头像
Vaccae
发布2019-07-25 11:48:45
8550
发布2019-07-25 11:48:45
举报
文章被收录于专栏:微卡智享微卡智享

前言

前面几章我们已经把SurfaceView加载Camera实现实时帧显示图像完成了,我也说过,我们加载实时图像是为了对接OpenCV进行图像处理所以才生成的Bitmap图像。

OpenCV4Android中NDK开发(一)--- OpenCV4.1.0环境搭建》这篇中我们是新建的项目中直接选择了包含C++,本篇主要是介绍怎么在现在的项目加改为使用JNI的方式。

实现方式

添加CPP的相关文件夹和文件

首先在我们项目的目录app/src/main下建立一个cpp的文件夹

进入cpp目录下我们把别的项目中的CMakeList.txt文件拷贝过来

如果没有配置的可以自己新建一个CMakeList.txt,下面是CMakeList.txt的配置,这个配置是直接关联我们的OpenCV库的,取自《OpenCV4Android中NDK开发(一)--- OpenCV4.1.0环境搭建

代码语言:javascript
复制
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

#该变量为真时会创建完整版本的Makefile
set(CMAKE_VERBOSE_MAKEFILE on)

#定义变量ocvlibs使后面的命令可以使用定位具体的库文件
set(opencvlibs "D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs")

#调用头文件的具体路径
include_directories(D:/PersonalStudio/OpenCV-android-sdk/sdk/native/jni/include)

#增加我们的动态库
add_library(libopencv_java4 SHARED IMPORTED)

#建立链接
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        "${opencvlibs}/${ANDROID_ABI}/libopencv_java4.so")


# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib
        -ljnigraphics
        libopencv_java4

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

然后我们在cpp文件夹下再建一个native-lib.cpp的文件,这个文件名主要是根据CMakeList.txt里面配置相同的,我们也可以改别的名,不过CMakeList.txt也要跟着修改。


修改build.gradle

打开build.gradle文件

在android下的defaultConfig下加入Cmake的配置

代码语言:javascript
复制
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                //我们的APP要支持的CPU架构
                abiFilters 'x86','x86_64','arm64-v8a','armeabi-v7a'
            }
        }

在android下面加入OpenCV的Lib目录

代码语言:javascript
复制
    //加上
    sourceSets{
        main{
            //当前这个目录下的库文件会被调用并且被打包进apk中
            jniLibs.srcDirs = ['D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs']
        }
    }

然后在android下面加入CMakiLists的指定目录

代码语言:javascript
复制
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }

这样的配置完成了,然后左边红框是我们没按右边Sync Now的目录

现在我们点击 一下右上角红框的Sync Now

生成完后可以看到左边红框里面已经出来cpp目录和下面的Cmakelist及native-lib.cpp的文件了,这就说明我们在现有项目中添加JNI成功了。

-END-

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微卡智享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档