GithubHelp home page GithubHelp logo

mavelyne / ndk_opencv_androidstudio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from quanhua92/ndk_opencv_androidstudio

0.0 2.0 0.0 6.32 MB

NDK and OpenCV with AndroidStudio

Java 99.95% Makefile 0.03% C++ 0.03%

ndk_opencv_androidstudio's Introduction

Step to use NDK in Android Studio:

  • Create class NativeClass with function getStringFromNative:
public class NativeClass {
    public native static String getStringFromNative();
}
  • Build project to generate classes

  • Open terminal. Change to app/src/main. Then run

javah -d jni -classpath ../../build/intermediates/classes/debug/ com.example.ndk_opencv_androidstudio.NativeClass
  • Create "com_example_ndk_opencv_androidstudio_NativeClass.cpp" in c folder (jni folder).
#include 

JNIEXPORT jstring JNICALL Java_com_example_ndk_1opencv_1androidstudio_NativeClass_getStringFromNative
  (JNIEnv * env, jobject obj){
    return env->NewStringUTF("Hello from JNI");
}
  • Edit build.gradle. Add ndk moduleName in defaultConfig
defaultConfig {
        applicationId "com.example.ndk_opencv_androidstudio"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "-0"

        ndk {
            moduleName "MyLib"
        }
    }
  • Load MyLib in MainActivity
    static {
        System.loadLibrary("MyLib");
    }
  • Add id to TextView
android:id="@+id/testTextView"
  • Use getStringFromNative() to change testTextView text
    TextView tv = (TextView) findViewById(R.id.testTextView);
    tv.setText(NativeClass.getStringFromNative());

Step to use OpenCV:

  • Open build.gradle. Add this before buildTypes
    // begin
    sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
    }
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine "/home/robotbase/Android/NDK64/android-ndk-r10d/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
    }
    //end
  • Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := com_example_ndk_opencv_androidstudio_NativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLib

include $(BUILD_SHARED_LIBRARY)
  • Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#opencv
OPENCVROOT:= /home/robotbase/Android/OpenCV-2.4.10-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk


LOCAL_SRC_FILES := com_example_ndk_opencv_androidstudio_NativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLib

include $(BUILD_SHARED_LIBRARY)
  • Edit MainActivity.java
static {
    System.loadLibrary("MyLib");
    System.loadLibrary("opencv_java");
}

Step to use OpenCV in Java:

  • Open Android Studio, choose import
  • Select OpenCV-2.4.10-android-sdk/sdk/java/ -> choose new location ** opencv-java **
  • Build > Rebuild Project
  • Close Project
  • Open NDK_OpenCV_AndroidStudio
  • File > Import Module
  • Select ** opencv-java **
  • Edit build.gradle
dependencies {
	...
    compile project(':opencv-java')
}
- Import org.opencv.... and code OpenCv in Java

Reference:

ndk_opencv_androidstudio's People

Contributors

quanhua92 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.