1 | initial version |
Hey,
don't give up that quick, might be a simple error. In your CPP-File:
JNIEXPORT void JNICALL Java_com_slani_tracker_OpenCamera_findObject((JNIEnv *env, jlong addRgba, jlong addHsv)
There is a ( too much at the beginning of arguments. Furthermore I'd recommend to configure the OpenCV-Type a bit more (see Android.mk later). I did a quick demo application that calculates the HSV-Value of an RGBA-Value and prints it to logcat as a fatal error. Your project settings seem to be correct (as soon as the ndk-build is invoked, everything is fine).
Build looks something like:
11:56:03 **** Incremental Build of configuration Default for project testApp ****
/home/tbergmueller/bin/avEclipse/android-ndk-r8d/ndk-build
Compile++ thumb : testLibrary <= detect_jni.cpp
Prebuilt : libopencv_contrib.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_legacy.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_ml.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_stitching.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_objdetect.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_ts.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_videostab.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_calib3d.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_photo.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_video.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_features2d.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_highgui.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_androidcamera.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_flann.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_imgproc.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : libopencv_core.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../libs/armeabi/
Prebuilt : liblibjpeg.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../3rdparty/libs/armeabi/
Prebuilt : liblibpng.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../3rdparty/libs/armeabi/
Prebuilt : liblibtiff.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../3rdparty/libs/armeabi/
Prebuilt : liblibjasper.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../3rdparty/libs/armeabi/
Prebuilt : libIlmImf.a <= /home/tbergmueller/bin/avEclipse/opencv-2-4-5-android-sdk/sdk/native/jni/../3rdparty/libs/armeabi/
Prebuilt : libgnustl_static.a <= <NDK>/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/
SharedLibrary : libtestLibrary.so
Install : libtestLibrary.so => libs/armeabi/libtestLibrary.so
11:56:05 Build Finished (took 1s.817ms)
As I stated, you might want to configure OpenCV in the makefile a bit (no camera modules). Furthermore I'd recommend to use libtype static for the opencv AND most important set the OPENCV_INSTALL_MODULES, otherwise they might not be exported to your device when you install the app.
To build the application, I used the following Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off
OPENCV_LIB_TYPE:=STATIC
$(info $(NDK_MODULE_PATH))
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_LDLIBS += -llog -landroid -lEGL -lGLESv1_CM
#name of the Library
LOCAL_MODULE := testLibrary
######################################################
############ Dig for Sourcefiles #####################
######################################################
FILE_LIST_CPP := $(wildcard $(LOCAL_PATH)/*.cpp) # finds all cpp files
### This value has to be set in order to make the next command work
LOCAL_SRC_FILES := $(FILE_LIST_CPP:$(LOCAL_PATH)/%=%)
######################################################
############ Trigger BUILDING ########################
######################################################
include $(BUILD_SHARED_LIBRARY)
Application.mk looks like that:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi
The source-file:
/*
* detect_jni.cpp
*
* Created on: Oct 30, 2013
* Author: tbergmueller
*/
#include <jni.h>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <android/log.h>
using namespace cv;
extern "C"
{
JNIEXPORT void JNICALL Java_com_example_testapp_MainActivity_findObject(JNIEnv *env, jobject obj, jlong addRgba, jlong addHsv)
{
Mat bgr(1,1,CV_8UC3);
bgr.data[0]= addRgba >> 8; // B
bgr.data[1]= addRgba >> 16; // G
bgr.data[2]= addRgba >> 24; // R
Mat hsv;
cvtColor(bgr, hsv,CV_BGR2HSV);
__android_log_print(ANDROID_LOG_FATAL, "Test","Hue-Value is %d", hsv.data[0] );
}
}
And corresponding Java-File for the application:
package com.example.testapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
static
{
System.loadLibrary("avNative");
}
private static native void findObject(long addRgba, long addHsv);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findObject(0xffff0000, 0xff223344); // should print hue value of first number (~30)
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If you don't manage it to get it working on your own I can zip my project file.. But I think you'd should figure it out easily with these hints ;)