I have been wondering if it is possible to use all the functions of opencv in android opencv. I am planning to do my PG project in android Open CV. Because i am familiar with android but not opencv. Is it possible to implement all the functions used in opencv (windows) to Android opencv?
Unfortunately, Java API doesn't provide access to all the functions as in the case of C++ API. First, I will discuss what all the toolkits that are needed for doing so and then how can we use the native(C / C++) in Android.
Toolkits Needed
To develop Android apps using OpenCV Library, we need the following tools (all of these are either Open Source or free software, or both) -
Eclipse with ADT Bundle (You can choose any other IDE like Android Studio)
OpenCV4Android (although, you can compile from source code, it is recommended for novice users to download the binaries.)
CygWin (Only for Windows, not needed on Linux or Mac)
Java JDK (JRE would not be sufficient)
Android Native Development Toolkit (NDK) (needed to run native C/C++ code)
You will need to set environment variables on your OS for the tools to correctly configure.
Alternatively, nVidia also provides a suite of developing tools — TADP. The advantage of TADP over the piece-by-piece method is that you don’t need to follow the often perplexing task of setting up the development environment. The default installation will download a lot of superfluous packages that are not needed (The download size can be greater than 2GB and on slow internet connections, it can turn into a prolonged activity.), rather select the packages manually at the time of download (A dialog box will appear, asking you to select either Complete, Custom or Manually.)
Using C/C++ code in Android
You can get access to native C/ C++ functionality in Android by
Here is the official OpenCV tutorial on setting up .
Also, Check out the Mixed Processing Sample in the Samples folder of Android4OpenCV folder. This example shows how you can pass an image and its grayscale equivalent to a native function and detect features on the image using FAST features detection.
Useful books and tutorials
There is not much study material right now, but I guess these 2 source are the best -
Android Application Programming with OpenCV by Packt
Publishing.
Sample programs in the OpenCV4Android folder.
Yes it is definitely possible to use all funtions in OpneCV on Android. But you have to note that OpenCV4Android includes 2 parts: the Java part and the native part.
In fact the Java part provides most of the functions of the OpenCV library. If you would like to have access to more functionalities and faster speed, then just go for native development. The .so library in the OpenCV SDK pack provides all native functions. In fact if you are familiar with native development, you could even directly use the OpenCV c++ library, just like how people use FFMPEG on android.
Related
I have coded a library that provides some function add, sub, divide, multi by c/c++ programming language. It's built to library.so by using Android NDK. So now, I want to call these function of library by using Android. I want to as How I can do ?
Thank.
For a working example project, you can find one from here: https://github.com/russell-shizhen/JniExample
NDK Package
Android Studio only includes the default tools and SDK, so download and install the NDK package separately. There are two ways to do this. The first and easiest is the automatic installation option in the SDK Tools tab. This is a large download (~1GB) so be sure to have a good internet connection and/or patience.
The second way is to download the NDK manually from the NDK Downloads page. This saves some time because of the smaller download size (<.5 GB) but will need some setting up. Download the appropriate NDK package for your platform and follow the installation instructions. You can put the extracted package anywhere you want, but remember this location as you will need it later.
Hello World
Let’s run a test project to see if the NDK installation works. The NDK package folder contains samples but they don’t seem to work out of the box in Android Studio without extra configuration. Instead we’ll import samples that work from the welcome screen. These NDK samples from GitHub can also be downloaded or cloned directly. This collection of samples has better compatibility with the latest version of Android Studio.
Select Import an Android code sample and type hello in the search box to filter the list. Choose Hello JNI from the filtered list under the Ndk category. Click Next to edit the app name and project location, then click Finish.
JNI
After the code has loaded and Gradle has synced, let’s take a look at the resulting project structure. You might have noticed what appears to be a discrepancy in the project’s name. Why is it called HelloJNI and not HelloNDK? What is ‘JNI’ and how does it differ from the ‘NDK’? The ‘Java Native Interface’ is a framework that enables Java applications to interact with native code. The JNI and the NDK work together to bring native support to Android apps. The NDK is part of the Android framework while the JNI is available to any Java application, not just Android apps.
Inside the project is a folder named jni which will hold all the native C or C++ source code of the app. The JNI provides a two-way interface. The C/C++ code is able to call Java code, including the standard Android libraries, and the Java code is able to call native functions defined in C/C++ code. Native code is governed by the same sandbox and security rules as Java code so the app will not have full unrestricted access to hardware. For a more detailed discussion on the JNI, read the JNI articles from the Android Developer website.
Since 2 Years I am working as Android Application Developer. I generally use android SDK for all the Android Apps Development. Now I have a project which is an Android App in which I have to use SDK as well as NDK for App development (As per Client requirement).
But as I don't have any experience with NDK I don't know what is it. In some Blogs I have read that NDK development is based on c++.
Is it true that to work with NDK one must have the complete knowledge of c++ ?
Please Help !!
Use of NDK means you have to write portion of code in C/C++ just to achieve the speed. If it is client requirement then you have no option. But keep in mind that you should use NDK only when you feel you need better performance. And of course you must have some understanding of c/c++ to use NDK.
NDK
NDK is a set of tools to compile C code to shared lib,
which you could use in your app - and that's all.
Enables legacy code re-use between iOS and Android platforms
Good for implementing CPU intensive operations that don't
allocate much memory like signal processing, physics simulations.
SDK
SDK is the main development kit for Android apps - it contains tools for Java and resources (png, xml) compiling, packaging to apk file, installing, running and debugging them on a device, an emulator, documentation, etc.
Java has superior memory management model Superior threading model Better exception handling model Rich set of libraries Superior support for unicode characters.
How to configure OpenCL environment for android platform either in windows or linux using any IDE? I want to make use of GPU using OpenCL for smartphone.
Android does not have official support for OpenCL and Google is openly hostile towards it, going to far as to remove unofficial drivers http://www.anandtech.com/show/7191/android-43-update-for-nexus-10-and-4-removes-unofficial-opencl-drivers . Even if your hardware still has OpenCL drivers you cannot distribute the app in Google Play, so it's practically useless.
For Android you are better off using Google's Renderscript (the reason why they hate OpenCL, they want their solution to be used) http://developer.android.com/guide/topics/renderscript/compute.html
You can write certain algorithms easily using Renderscript. It's basically like OpenCL but it has no concept of local memory and accessing thread id is not supported. But for things like simple image processing or particle simulation it's perfect.
If you must use OpenCL for some reason and your target hardware supports it just use adb pull to get the libOpenCL.so out of the phone, copy it to the ndk lib directory, and also place OpenCL include headers to the include path and you're good to go.
Basically, to develop OpenCL program/applications for Android, the followings are what you need:
Android SDK, Android NDK
Cygwin (if under windows)
OpenCL header file (could be downloaded from Khronos website)
OpenCL lib file. For different chip vendor, this library is located under different path. See here for details.
Develop OpenCL program in C/C++. compile with ndk-build to a shared library.
Develop your application using Android Studio. Use JNI to call your OpenCL-related functions from the native shared library.
I found a related answer here: How do cross-platform mobile app development frameworks work?
but I was thinking more about c++ cross-platform SDKs work (e.g. Corona, Marmalade, EdgeLib, etc.). They provide the ability to export binaries for iOS and Android while allowing the developer to use C++ code. My assumption is listed below, but please correct it if I am wrong anywhere:
User writes code in C++.
SDK has an interface layer with C++ functions called in user code requesting mobile OS specific functionality. This interface layer is built from code required to implement that SDK function call in the specific mobile OS(written in Java for Android and Obj-C for iOS).
Part I am most confused about because I don't have much mobile dev experience points: Do iOS and Android both have C++ cross compilers that can compile the general logic code written in C++ in the user's app?
MoSync is an example of a C++ based cross platform mobile toolkit - this one starts by using the open source GCC compiler to compile your app's C++ code into an assembly-like format. A custom tool by MoSync (called the 'PipeTool') then combines this assembly format with their pre-compiled libraries into various target formats, including java bytecode (for Android) or Objective-C source (for iOS). More detail about this process here.
The final compilation on the target platform (Android or iOS) is left to you, using the native IDE (Xcode for iOS and Eclipse IDE with Android SDK for Android). So to create an iOS application you'll still need to be a member of the Apple iOS developer program (US$99 per year) for example, whereas the Eclipse IDE and Android SDK are free.
Your example of Corona SDK is not fully relevant since Corona builds into the native binary format using their custom build servers in the cloud - what goes on there is not fully documented as its a closed source toolkit. You pay a subscription fee per year to Corona to be able to build apps. I'm not sure about the other ones you mentioned (Marmalade, EdgeLib, etc.) but would assume they are similar to MoSync.
Check out codenameone.com - they use Java but eventually compile into C++ for iOS and Java for Android.
The difference is their environment includes all the graphics and they create the controls themselves so you get an actual native application with just one codebase.
What is the Android NDK (native development kit) ? How can one use it? Why should one use it?
The NDK (Native Development Kit) is a tool that allows you to program in C/C++ for Android devices. It's intended to integrate with the SDK (it's described as a "companion tool") and used only for performance-critical portions of a project. See here for more information.
NDK may improve application performance. This is usually true for many
processor-bound applications. Many multimedia applications and video games use
native code for processor-intensive tasks.
The performance improvements can come from three sources. Firstly, the native code is compiled to a binary code and run directly on OS, while Java code is translated into Java
byte-code and interpreted by Dalvik Virtual Machine (VM). At Android 2.2 or higher,
a Just-In-Time (JIT) compiler is added to Dalvik VM to analyze and optimize the Java
byte-code while the program is running (for example, JIT can compile a part of the
byte-code to binary code before its execution). But in many cases, native code still
runs faster than Java code.
Java code is run by Dalvik VM on Android. Dalvik VM is specially designed
for systems with constrained hardware resources (memory space, processor
speed, and so on).
The second source for performance improvements at NDK is that native code allows
developers to make use of some processor features that are not accessible at Android SDK,
such as NEON, a Single Instruction Multiple Data (SIMD) technology, allowing multiple
data elements to be processed in parallel. One particular coding task example is the color
conversion for a video frame or a photo. Suppose we are to convert a photo of 1920x1280
pixels from the RGB color space to the YCbCr color space. The naive approach is to apply a
conversion formula to every pixel (that is, over two million pixels). With NEON, we can process multiple pixels at one time to reduce the processing time.
The third aspect is that we can optimize the critical code at an assembly level, which is a
common practice in desktop software development.
Disadvantage
NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity
into your application.
The Android NDK is a companion tool used only in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.
This provide benefits in form of reuse of existing code and increased speed.
Please go through below links.
Link-1
Link-2
Link-3
The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
The following links also answers your question:
What is NDK?
When to Develop in Native Code
NDK Download
How to build NDK app
how to work with NDK
10 tips for Android NDK
The Android NDK is a toolset that lets you embed components that make
use of native code in your Android applications.
Android applications run in the Dalvik virtual machine. The NDK allows
you to implement parts of your applications using native-code
languages such as C and C++. This can provide benefits to certain
classes of applications, in the form of reuse of existing code and in
some cases increased speed.
Source: http://developer.android.com/sdk/ndk/overview.html
The Android NDK is a companion tool to the Android SDK that lets you
build performance-critical portions of your apps in native code. It
provides headers and libraries that allow you to build activities,
handle user input, use hardware sensors, access application resources,
and more, when programming in C or C++. If you write native code, your
applications are still packaged into an .apk file and they still run
inside of a virtual machine on the device. The fundamental Android
application model does not change.
Source: http://developer.android.com/sdk/ndk/index.html
NDK is just a set of tools which lets you to write C/C++ codes for your application.For example suppose you want to add a critical function/performance to your app and you want to write it in C/C++ then eclipse or any other IDE will not allow you to write your C/C++ and in that case you have to use NDK and integrate it in your app.
NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++....Checkout this https://developer.android.com/tools/sdk/ndk/index.html
Android NDK (native development kit)
Android Native Development Kit (NDK) is developers to write code in C/C++ that compiles to native code
Why should one use it?
The source code is compiled directly into machine code for the CPU (and not into an intermediate language, as with Java) then developers are able to get the best performance
How can one use it?
Here best tutorials
https://developer.android.com/ndk/index.html
https://www.androidauthority.com/android-ndk-everything-need-know-677642/
https://www.ntu.edu.sg/home/ehchua/programming/android/Android_NDK.html