3

i am doing a project using opencv on android. i am coding with c++ but want to implement the program on an android smartphone. i don't want to convert c++ codes to java just because android is in java, so im gonna use JNI. in my research, i have come across codes that really use the JNI coding style but i cannot understand how to transform my c++ codes to JNI codes. i found out that in tutorial 4 of android opencv samples, it uses just the c++ codes unlike in tutorial 3.. so what is really the difference between these two? im wondering if the JNI coding style will give me faster performance. as of now, i was able to use just the c++ and inegrate it with java but the app crashes.

From android opencv tutorial 3:

JNIEXPORT void JNICALL
Java_org_opencv_samples_tutorial3_Sample3View_FindFeatures(JNIEnv* env, jobject,
jint width, jint height, jbyteArray yuv, jintArray bgra)
{
    jbyte* _yuv  = env->GetByteArrayElements(yuv, 0);
    jint*  _bgra = env->GetIntArrayElements(bgra, 0);

    Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
    Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
    Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);

    cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);

    vector<KeyPoint> v;

    FastFeatureDetector detector(50);
    detector.detect(mgray, v);
    for( size_t i = 0; i < v.size(); i++ )
        circle(mbgra, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));

    env->ReleaseIntArrayElements(bgra, _bgra, 0);
    env->ReleaseByteArrayElements(yuv, _yuv, 0);
}

From android opencv tutorial 4:

JNIEXPORT void JNICALL
Java_org_opencv_samples_tutorial4_Sample4View_FindFeatures(JNIEnv*, jobject, jlong
addrGray, jlong addrRgba)
{
    Mat* pMatGr=(Mat*)addrGray;
    Mat* pMatRgb=(Mat*)addrRgba;
    vector<KeyPoint> v;

    FastFeatureDetector detector(50);
    detector.detect(*pMatGr, v);
    for( size_t i = 0; i < v.size(); i++ )
        circle(*pMatRgb, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(255,0,0,255));
}
4
  • I'm afraid you are totally confused. You do not write "JNI codes". JNI is the interface between Java and native code. You compile your c++ as normal into libraries then reference those libraries using JNI. The Android NDK provides the tool chain, samples and documentation used to accomplish this. Commented Nov 1, 2012 at 16:07
  • Also, keep in mind that if you decide to use the NDK, you will have to recompile your library for different devices. NTK/JNI may, or may not, depending on the context, give you better performance. C++ is an excellent language, but in this case especially, make sure you weight the benefits against the drawbacks. If you want more specific help, you have to add in more specific code :D Commented Nov 1, 2012 at 19:24
  • This code I got from tutorial 3 of android opencv: jint* _bgra = env->GetIntArrayElements(bgra, 0); This code I got from tutorial 4 of android opencv: Mat* pMatRgb=(Mat*)addrRgba; These codes are different but do more or less the same process right? Commented Nov 2, 2012 at 8:15
  • @Simon i edited above question and added code to clarify what i mean. Commented Nov 2, 2012 at 8:35

2 Answers 2

1

You can build an android application using c/c++ using the Android NDK

Take a look here Can I do Android Programming in C++, C?

Sign up to request clarification or add additional context in comments.

Comments

1

JNI is the layer between Java code and native code (C, C++). It rely on the native keyword and static methods. In your app, you will use only Java methods and classes. You will need to compile the C/C++ sources to a dynamic library, a .so file.

JNI enables one to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e.g. when the standard Java class library does not support the platform-specific features or program library

You should not always code in C and C++. it will increase the complexity of your application.
see this

1 Comment

Addendum: I think that from the software design standpoint, a rewrite will result in a more 'native java' application, than wrapping the c++ code in JNI and access that. Furthermore, is your code only doing numbercrunching? If yes, building a .so might still be a good way. But if your code does more, like GUI stuff or system calls etc. Simply moving the .so file to android could result in issues to track.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.