JNI, NDK, and OpenCV - android

I have recently begun using the Android NDK and I have successfully implemented a few simple Android apps. I need to detect objects (squares and rectangles) from an image. My research has shown me that OpenCV is the solution for this. This is the algorithm I use to detect a square from the image.
However, I am unclear as to how should I use the squares.cpp file in my code. The OpenCV samples show how to use the cpp files in JNI format. Do I need to convert the squares.cpp file to JNI or would there be another feasible solution?
Thanks. All suggestions and feedback are welcome.

You don't have to convert the squares.cpp file to JNI.
From your Java code, you will call a JNI function (as I suppose you did in the "few simple Android apps" you have implemented) that will then call the functions in squares.cpp.
In other words, you basically only need one call to a JNI function from Java, and once you are in the C++ code, you can code in C++ as usual.

Related

Are there any Android NDK examples, on how to pass a file from Java to C++ using JNI?

I am not sure, if this is the right place to ask, but I am curious, if there are any Android NDK examples (apks), on how to read a file with Java and pass it over to C/ C++ using JNI.
Currently I am trying to read pdf or office files (e.g. docx) with C/ C++ and I am trying to understand the concept behind it.
Maybe there are some full apk examples or just some snippets, with which I could extend the hello-jni/ hello-jnicallback examples.
I already found the android ndk samples site https://github.com/android/ndk-samples, but there seems to be no example on how to simply read files.
Thank you for your help.

Tencent NCNN detect function on android modification

The application I use (https://github.com/dangbo/ncnn-mobile.git) use a native library that gives out inference result as a tag. I need it to give me a float array from which it knows the tag. The array is already implemented in the C++ files, but changing them does not effect the application itself. I would not mind if the array was written into a string, I just need the numbers in a readable format. However, the method is native and thus I do not know how to modify this behavior.
I use the newest versions of Android Studio and NCNN. Please advise.
Simply use ndk-build on the jni folder for building.

How JNI works on the memory level

I have a java file which loads .so files and prints the result coming from .so file. I don't have a source code for my .so file. Can anyone tell how in the memory structure .so results are loaded and from where this Java class is reading the results generated by .so and printing them out??
If u have any code that is already written in native language and reluctant about changing it but you would want to use native calls in your java codes and not to rewrite entirely everything java. JNI comes in handy. it converts all your platform specific implementations to platform independent. Performance- and platform-sensitive API implementations in the standard library allows all Java applications to access this functionality.
the library files .so are converted in such a manner.
JNIEnv contains all types of conversion from a native data types to the java data types. it also supports suitable implementations for native method calls, signals are also handled by the JVM.
Java being platform independent native processss specific to a hardware and operating system platform are all made platform independent by mapping native data types and method calls to java. JNI performs the above using the library files example .so or .a files
For JNI mapping u can refer the below link
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
Your .so have a JNI glue class that binds all your Java native methods (public native void x();) with compiled native code.
I'm assuming it uses JNI, otherwise I have no idea how its working. If it uses JNI, then it actually creates Java objects on the Java heap in the .so, using the JNI libraries. Look for functions marked "native", these functions map directly to functions in the so that are called when th Java code whats to run something in the library.
The Droidfish Android chess game is written in Java. But its underlying chess engine Stockfish is written in C++. At first, I thought there was a JNI layer that connects these two but there isn't. The C++ stockfish executable is launched, and Java and C++ communicates via stream.
You can launch the C++ stockfish and play with this engine in the command line, no graphical interfaces.
In your question, your native .so library is printing something to stdout/stderr and you want to read these outputs in Java?

Use Opencv on Android

I have found this code: https://github.com/mintuhouse/FinMan/blob/master/src/unix/imp.cpp which is a class for preprocessing an image with a receipt using OpenCV.
I wanted to ask: how can I use it in an android application which takes a photo and saves it as bitmap?
I tried to understand what the class is doing and tried to write this procedure in android. But it's little difficult because it has some functions such as cvZero(image) that I couldn't find in OpenCV for Android. Any ideas?
Also, I have tried the NDK, but I couldn't figure out how my bitmap file and this class would communicate after the NDK-build. I'm confused! Any help?
In old opencv api all functions, structures, etc. started from prefix "cv" to show that they are part of OpenCV library. In version 2.0 api changed, and all functiones, structures, classes etc are in namespace "cv", so in c++ you can access them by using this prefix, for example "cv::Point" or "cv:imread(...)". Old cvZero function now is accessible the same way or eventually is a method(probably static) of Mat class - so you can use it this way: myMat.zero(...) or Mat::zero(...). In Java it should be similar - try myMat.zero(...) or Mat.zero(...).

using opencv haarcascades in android ndk

I would like to use the facedetection in opencv cpp to use it in an android app. I have compiled jni successfully. But I wonder how would i use the haarcascades. I can store in sdcard and read it from there. Is there any other way i can use the xml files directly from the project?
There is a c++ example called facedetect coming with the opencv superpack. I'm running OpenCV-2.3.1 myself and it's located in this folder: ../opencv-2.3.1/samples/c/
The sample uses haarcascades and this might be your best bet for facedetection. If you can use the Android NDK with proper JNI calls from a .cpp file then you shouldn't have any problems to use this sample.
I'm working on a similar thing myself but haven't tried it myself yet. Should be implementing the thing somewhere next week but can't guarantee it. Let me know if this works out for you

Categories

Resources