Android(安卓)ndk 入门5 - opencv实现
16lz
2021-12-04
参考:http://segmentfault.com/a/1190000000735743
http://ph0b.com/android-studio-gradle-and-ndk-integration/
############################################################
新建工程NDKTest4,新建活动MainActivity
新建MyNDK.java:
package com.zj.ndktest4;/** * Created by root on 15-11-26. */public class MyNDK { static { System.loadLibrary("hello-jni-opencv"); } public static native String hello();}
修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.zj.ndktest4.MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
修改MainActivity.java:
package com.zj.ndktest4;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.hello()); }}
修改local.properties,加入:
ndk.dir=$ //加入ndk地址
修改gradle.properties,加入:
android.useDeprecatedNdk=truendkDir=$ //加入ndk地址
修改app/build.gradle,加入:
//add begin sourceSets.main.jni.srcDirs = [] task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') { commandLine "/opt/android-ndk-r10e/ndk-build", 'NDK_PROJECT_PATH=build/intermediates/ndk', 'NDK_LIBS_OUT=src/main/jniLibs', 'APP_BUILD_SCRIPT=src/main/jni/Android.mk', 'NDK_APPLICATION_MK=src/main/jni/Application.mk' } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } //add end
点击MyNDK,右键->External Tools->javah,生成app/src/main/jni/com_zj_ndktest4_MyNDK.h:
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_zj_ndktest4_MyNDK */#ifndef _Included_com_zj_ndktest4_MyNDK#define _Included_com_zj_ndktest4_MyNDK#ifdef __cplusplusextern "C" {#endif/* * Class: com_zj_ndktest4_MyNDK * Method: hello * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_zj_ndktest4_MyNDK_hello (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif
新建jni/main.cpp:
#include <jni.h>#include <opencv2/opencv.hpp>#include "com_zj_ndktest4_MyNDK.h"JNIEXPORT jstring JNICALL Java_com_zj_ndktest4_MyNDK_hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("hello jni");}
新建jni/Android.mk:
LOCAL_PATH := ${call my-dir}include $(CLEAR_VARS)#opencvOPENCVROOT := /opt/opencv-sdkOPENCV_CAMERA_MODULE := onOPENCV_INSTALL_MODULES := onOPENCV_LIB_TYPE := SHAREADinclude ${OPENCVROOT}/sdk/native/jni/OpenCV.mkLOCAL_SRC_FILES := main.cppLOCAL_LDLIBS := -llogLOCAL_MODULE := hello-jni-opencvinclude $(BUILD_SHARED_LIBRARY)
新建jni/Application.mk:
APP_ABI := armeabiAPP_PLATFORM := android-21APP_STL := stlport_shared
运行:
###########################################################
从文章Android Studio,gradle and NDK integration中知道
在Elipse下,当你想要在Android工程中使用C/C++源码时,你必须配置Android.mk文件,而现在,在Android Studio下,默认不使用Android.mk文件,你可以在app/build.gradle中配置如下:
android { ... defaultConfig { ndk { moduleName "hello-jni" } } ...}上述语句表示使用一个ndk模块"hello-jni"
而且你还可以在ndk下设置其他属性:
1.cFlags
2.ldLibs
3.stl (ie. stlport_shared, gnustl_shared)
4.abiFilters (ie. "x86", "armeabi-v7a")
android { ... defaultConfig { ndk { moduleName "hello-jni" stl := stlport_shared //配置C++ ldLibs := "log" //NDK可以进行log输出 } } ...}
这对于简单的NDK使用来说,非常方便。
不过对于比较复杂的NDK使用来说,就有一些不变,比如加入opencv功能。可以通过加入下述语句来禁止默认的ndk实现,从而转向之前的Android.mk方式:
在app/build.gradle下加入:
android { ... sourceSets.main.jni.srcDirs = [] //disable automatic ndk-build call task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') { commandLine "/opt/android-ndk-r10e/ndk-build", 'NDK_PROJECT_PATH=build/intermediates/ndk', 'NDK_LIBS_OUT=src/main/jniLibs', 'APP_BUILD_SCRIPT=src/main/jni/Android.mk', 'NDK_APPLICATION_MK=src/main/jni/Application.mk' } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild }}
更多相关文章
- Android(安卓)ndk 入门4 - C++实现
- 调整 FMX Android(安卓)文字显示「锯齿」效果
- android permission Suggestion: add 'tools:replace="android:v
- android,进入页面textview默认获得焦点问题,如何取消焦点
- Android(安卓)全屏设置
- Android(安卓)源码热门改动速查(持续更新.....)
- Android(安卓)软键盘遮挡Dialog
- Android(安卓)Studio 小提示,新建Activity
- android studio 新建项目 界面一直停在 【“building ‘ 项目名