Android NDK JNI + OpenCV

JNI + OperCV 参考网站

1. Android Studio安装NDK
Tools -> SDK Manager
勾选 LLDB、CMake、NDK
Android SDK Location 修改为 /opt/Android/Sdk
2. 新建Project
File -> New -> New Project
勾选 Include C++ support
勾选 Phone and Tablet,选择一个较低的SDK版本
勾选 Exceptions Support (-fexceptions)、Runtime Type Information Support (-frtti)
3. 新建CPP

testMessage.cpp

#include <jni.h>
#include <string>

using namespace std;

extern "C" {
    JNIEXPORT jstring JNICALL
    Java_hgw_facedetect_1dnn_MainActivity_testString(JNIEnv *env, jobject job);
}

JNIEXPORT jstring JNICALL
Java_hgw_facedetect_1dnn_MainActivity_testString(JNIEnv *env, jobject job) {
    string hello = "nice to see you!";
    return env->NewStringUTF(hello.c_str());
}

修改CMakeLists.txt

add_library(
        testMessage
        SHARED
        src/main/cpp/testMessage.cpp)
target_link_libraries( # Specifies the target library.
        native-lib
        testMessage

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

修改MainActivity.java

package hgw.facedetect_dnn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.opencv.android.BaseLoaderCallback;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("testMessage");	// new add------------------------------------
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI() + "\n" +  testString());	// new add------------------------------------
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
    public native String testString();		// new add------------------------------------
}

4. OpenCV
  1. 在app->src->main下新建文件夹jniLibs
    将/OpenCV-android-sdk/sdk/native/libs下的armeabi-v7a复制到该文件夹下
  2. 将OpenCV-android-sdk/sdk/native/jni/include复制到app->src->main->cpp->include->opencv->include下
  3. 在CMakeLists.txt中添加如下语句
# OpenCV

set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
add_library(
        opencv_java3-lib
        SHARED
        IMPORTED)
set_target_properties(
        opencv_java3-lib
        PROPERTIES IMPORTED_LOCATION
        ${lib_src_DIR}/libopencv_java3.so)

注意 opencv_java3-lib一定要放在native-lib后面

target_link_libraries( # Specifies the target library.
        native-lib
        opencv_java3-lib

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

在app下的build.gradle中的android下的defaultConfig下添加如下语句

        ndk {
            abiFilters "armeabi-v7a"
        }
需要
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include/opencv/include)
不能
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include/opencv)
  1. 菜单栏File->New->Import Module添加如下文件
    /home/hgw/ubtrobot/development_tools/OpenCV-android-sdk/sdk/java
    在File->Project Structure->app->Dependencies->"+"号->Module dependency->Choose Modules->openCVLibraryxxx
0. 问题

Error: package android.hardware.camera2 does not exist OpenCV

Ctrl +Alt+Shift+s
openCVLibrary343 -> Properties
 -> Compile Sdk Version : Android 6.0
-> Build Tools Version : 28.03

添加其他工程
在settings.gradle中添加

include ':app','lib_camera','lib_base'

CMakeLists.txt

# 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)

set(NATIVE_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp)
set(LIBS_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)



#OpenCV
include_directories(${NATIVE_DIR}/include/opencv/include)
add_library(opencv_java3-lib SHARED IMPORTED)
set_target_properties(opencv_java3-lib PROPERTIES IMPORTED_LOCATION "${LIBS_DIR}/${ANDROID_ABI}/libopencv_java3.so")

#dlib
#add_library(libdlib SHARED IMPORTED)
#set_target_properties(libdlib PROPERTIES IMPORTED_LOCATION "${LIBS_DIR}/${ANDROID_ABI}/libdlib.so")
include(src/main/cpp/dlib/cmake)

#lipmove
include_directories(${NATIVE_DIR}/include/lipmove/include)
aux_source_directory(${NATIVE_DIR}/src/lipmove SRC_DIR)


# 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).
        ${SRC_DIR}
        src/main/cpp/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
        opencv_java3-lib
        dlib

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

模型文件获得
1.在src/main目录下新建assets文件夹

main->右键->New->Folder->Assets Folder->Change Folder Location->src/main/assets->Finish

2.将模型文件放入该文件夹
3.在MainActivity.java中新建函数

    //String model = getPath("fd_cnn_s3.bin", this);   //获得路径方法
    private static String getPath(String file, Context context) {
        AssetManager assetManager = context.getAssets();
        BufferedInputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(assetManager.open(file));
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            File outFile = new File(context.getFilesDir(), file);
            FileOutputStream os = new FileOutputStream(outFile);
            os.write(data);
            os.close();
            return outFile.getAbsolutePath();
        } catch (IOException ex) {
            Log.i(TAG, "Failed to upload a file");
        }
        return "";
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值