ndk和JNI的使用初探

參考資料:

https://blog.csdn.net/qq_25884515/article/details/104250920

創建C++項目

å¨è¿éæå¥å¾çæè¿°

 

理解 CMakeList.txt文件,

設置頭文件 和 添加源文件

#設置頭文件爲當前路徑
INCLUDE_DIRECTORIES("./")

# 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
             v4l2Cocos.cpp  )

還有 在庫裏面 調用庫巴 應該是

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
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

 

在 JAVA 文件中 定義 調用的 函數 參數 和返回數據

public native String stringFromJNI();
public native int getFrameFromJNI(int[] data);
// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

 

在C++文件中 寫入這些函數的實現

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_seziocr_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
char pbuf[128];

pCap=new CapCocos();
int d=pCap->OpenVideo("/dev/video0");
sprintf(pbuf,"video0:%d",d);

LOGD("Hello World %d",d);
//return env->NewStringUTF(hello.c_str());
return env->NewStringUTF(pbuf);

}

注意 env 是鏈接 c++ 和 java 的數據格式轉換的 重要參數

 

還要研究怎麼 直接調用 so 的庫 不需要些 源碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章