This tutorial also assumes you have OpenCV4Android SDK already installed on your development
machine and OpenCV Manager on your testing device correspondingly. If you need help with any of
these, you may consult our OpenCV4Android SDK tutorial.

If you encounter any error after thoroughly following these steps, feel free to contact us via
OpenCV4Android discussion group or OpenCV
Q&A forum . We’ll do our best to help you out.

In this section we will explain how to make some existing project to use OpenCV.
Starting with 2.4.2 release for Android, OpenCV Manager is used to provide apps with the best
available version of OpenCV.
You can get more information here: Android OpenCV Manager and in these
slides.

In most cases OpenCV Manager may be installed automatically from Google Play. For the case, when
Google Play is not available, i.e. emulator, developer board, etc, you can install it manually
using adb tool. See How to select the proper version of OpenCV Manager for details.

There is a very base code snippet implementing the async initialization. It shows basic principles.
See the “15-puzzle” OpenCV sample for details.

It this case application works with OpenCV Manager in asynchronous fashion. OnManagerConnected
callback will be called in UI thread, when initialization finishes. Please note, that it is not
allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback.
Load your own native libraries that depend on OpenCV after the successful OpenCV initialization.
Default BaseLoaderCallback implementation treat application context as Activity and calls
Activity.finish() method to exit in case of initialization failure. To override this behavior
you need to override finish() method of BaseLoaderCallback class and implement your own
finalization method.

According to this approach all OpenCV binaries are included into your application package. It is
designed mostly for development purposes. This approach is deprecated for the production code,
release package is recommended to communicate with OpenCV Manager via the async initialization
described above.

If your application project doesn’t have a JNI part, just copy the corresponding OpenCV
native libs from <OpenCV-2.4.11-android-sdk>/sdk/native/libs/<target_arch> to your
project directory to folder libs/<target_arch>.

In case of the application project with a JNI part, instead of manual libraries copying you
need to modify your Android.mk file:
add the following two code lines after the "include$(CLEAR_VARS)" and before
"includepath_to_OpenCV-2.4.11-android-sdk/sdk/native/jni/OpenCV.mk"

1
2

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on

The result should look like the following:

1
2
3
4
5
6

include $(CLEAR_VARS)# OpenCVOPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include ../../sdk/native/jni/OpenCV.mk

After that the OpenCV libraries will be copied to your application libs folder during
the JNI build.v

Eclipse will automatically include all the libraries from the libs folder to the
application package (APK).

The last step of enabling OpenCV in your application is Java initialization code before calling
OpenCV API. It can be done, for example, in the static section of the Activity class:

1
2
3
4
5

static{if(!OpenCVLoader.initDebug()){// Handle initialization error}}

If you application includes other OpenCV-dependent native libraries you should load them
after OpenCV initialization:

Perform static linking with OpenCV. By default dynamic link is used and the project JNI lib
depends on libopencv_java.so.

The file Application.mk should exist and should contain lines:

APP_STL:= gnustl_static
APP_CPPFLAGS:= -frtti -fexceptions

Also, the line like this one:

APP_ABI:= armeabi-v7a

Should specify the application target platforms.

In some cases a linkage error (like "Infunction'cv::toUtf16(std::basic_string<...>...undefinedreferenceto'mbstowcs'") happens when building an application JNI library,
depending on OpenCV. The following line in the Application.mk usually fixes it:

Lets discuss some most important steps. Every Android application with UI must implement Activity
and View. By the first steps we create blank activity and default view layout. The simplest
OpenCV-centric application must implement OpenCV initialization, create its own view to show
preview from camera and implements CvCameraViewListener2 interface to get frames from camera and
process it.

First of all we create our application view using xml layout. Our layout consists of the only
one full screen component of class org.opencv.android.JavaCameraView. This class is
implemented inside OpenCV library. It is inherited from CameraBridgeViewBase, that extends
SurfaceView and uses standard Android camera API. Alternatively you can use
org.opencv.android.NativeCameraView class, that implements the same interface, but uses
VideoCapture class as camera access back-end. opencv:show_fps="true" and
opencv:camera_id="any" options enable FPS message and allow to use any camera on device.
Application tries to use back camera first.

After creating layout we need to implement Activity class. OpenCV initialization process has
been already discussed above. In this sample we use asynchronous initialization. Implementation of
CvCameraViewListener interface allows you to add processing steps after frame grabbing from
camera and before its rendering on screen. The most important function is onCameraFrame. It is
callback function and it is called on retrieving frame from camera. The callback input is object
of CvCameraViewFrame class that represents frame from camera.

Note

Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object
does not have its own state and its behavior out of callback is unpredictable!

It has rgba() and gray() methods that allows to get frame as RGBA and one channel gray scale
Mat respectively. It expects that onCameraFrame function returns RGBA frame that will be
drawn on the screen.