1 OpenCV4Android SDK {#tutorial_O4A_SDK}
4 This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK.
6 This guide was written with MS Windows 7 in mind, though it should work with GNU Linux and Apple Mac
9 This tutorial assumes you have the following software installed and configured:
14 - ADT and CDT plugins for Eclipse
16 If you need help with anything of the above, you may refer to our @ref tutorial_android_dev_intro guide.
18 If you encounter any error after thoroughly following these steps, feel free to contact us via
19 [OpenCV4Android](https://groups.google.com/group/android-opencv/) discussion group or OpenCV [Q&A
20 forum](http://answers.opencv.org). We'll do our best to help you out.
25 OpenCV4Android SDK package enables development of Android applications with use of OpenCV library.
27 The structure of package contents looks as follows:
29 OpenCV-2.4.9-android-sdk
31 | |_ OpenCV_2.4.9_binary_pack_armv7a.apk
32 | |_ OpenCV_2.4.9_Manager_2.18_XXX.apk
50 - `sdk` folder contains OpenCV API and libraries for Android:
51 - `sdk/java` folder contains an Android library Eclipse project providing OpenCV Java API that can
52 be imported into developer's workspace;
53 - `sdk/native` folder contains OpenCV C++ headers (for JNI code) and native Android libraries
54 (\*.so and \*.a) for ARM-v5, ARM-v7a and x86 architectures;
55 - `sdk/etc` folder contains Haar and LBP cascades distributed with OpenCV.
56 - `apk` folder contains Android packages that should be installed on the target Android device to
57 enable OpenCV library access via OpenCV Manager API (see details below).
59 On production devices that have access to Google Play Market (and Internet) these packages will
60 be installed from Market on the first start of an application using OpenCV Manager API. But
61 devkits without Market or Internet connection require this packages to be installed manually.
62 Install the Manager.apk and optional binary_pack.apk if it needed. See `Manager Selection`
65 @note Installation from Internet is the preferable way since OpenCV team may publish updated
66 versions of this packages on the Market.
68 - `samples` folder contains sample applications projects
69 and their prebuilt packages (APK). Import them into Eclipse workspace (like described below) and
70 browse the code to learn possible ways of OpenCV use on Android.
72 - `doc` folder contains various OpenCV documentation in PDF format. It's also available online at
73 <http://docs.opencv.org>.
74 @note The most recent docs (nightly build) are at <http://docs.opencv.org/2.4>. Generally, it's more
75 up-to-date, but can refer to not-yet-released functionality.
76 @todo I'm not sure that this is the best place to talk about OpenCV Manager
78 Starting from version 2.4.3 OpenCV4Android SDK uses OpenCV Manager API for library initialization.
79 OpenCV Manager is an Android service based solution providing the following benefits for OpenCV
80 applications developers:
82 - Compact apk-size, since all applications use the same binaries from Manager and do not store
83 native libs within themselves;
84 - Hardware specific optimizations are automatically enabled on all supported platforms;
85 - Automatic updates and bug fixes;
86 - Trusted OpenCV library source. All packages with OpenCV are published on Google Play;
88 For additional information on OpenCV Manager see the:
90 - [Slides](https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p)
91 - [Reference Manual](http://docs.opencv.org/android/refman.html)
93 Manual OpenCV4Android SDK setup
94 -------------------------------
96 ### Get the OpenCV4Android SDK
98 -# Go to the [OpenCV download page on
99 SourceForge](http://sourceforge.net/projects/opencvlibrary/files/opencv-android/) and download
100 the latest available version. Currently it's [OpenCV-2.4.9-android-sdk.zip](http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.9/OpenCV-2.4.9-android-sdk.zip/download).
101 -# Create a new folder for Android with OpenCV development. For this tutorial we have unpacked
102 OpenCV SDK to the `C:\Work\OpenCV4Android\` directory.
104 @note Better to use a path without spaces in it. Otherwise you may have problems with ndk-build.
106 -# Unpack the SDK archive into the chosen directory.
108 You can unpack it using any popular archiver (e.g with 7-Zip):
110 ![](images/android_package_7zip.png)
112 On Unix you can use the following command:
114 unzip ~/Downloads/OpenCV-2.4.9-android-sdk.zip
117 ### Import OpenCV library and samples to the Eclipse
119 -# Start Eclipse and choose your workspace location.
121 We recommend to start working with OpenCV for Android from a new clean workspace. A new Eclipse
122 workspace can for example be created in the folder where you have unpacked OpenCV4Android SDK
125 ![](images/eclipse_1_choose_workspace.png)
127 -# Import OpenCV library and samples into workspace.
129 OpenCV library is packed as a ready-for-use [Android Library
130 Project](http://developer.android.com/guide/developing/projects/index.html#LibraryProjects). You
131 can simply reference it in your projects.
133 Each sample included into the `OpenCV-2.4.9-android-sdk.zip` is a regular Android project that
134 already references OpenCV library. Follow the steps below to import OpenCV and samples into the
137 - Right click on the Package Explorer window and choose Import... option from the context
140 ![](images/eclipse_5_import_command.png)
142 - In the main panel select General --\> Existing Projects into Workspace and press Next
145 ![](images/eclipse_6_import_existing_projects.png)
147 - In the Select root directory field locate your OpenCV package folder. Eclipse should
148 automatically locate OpenCV library and samples:
150 ![](images/eclipse_7_select_projects.png)
152 - Click Finish button to complete the import operation.
154 @note OpenCV samples are indeed **dependent** on OpenCV library project so don't forget to import it to your workspace as well.
156 After clicking Finish button Eclipse will load all selected projects into workspace, and you
157 have to wait some time while it is building OpenCV samples. Just give a minute to Eclipse to
158 complete initialization.
160 ![](images/eclipse_cdt_cfg4.png)
162 Once Eclipse completes build you will have the clean workspace without any build errors:
164 ![](images/eclipse_10_crystal_clean.png)
166 @anchor tutorial_O4A_SDK_samples
167 ### Running OpenCV Samples
169 At this point you should be able to build and run the samples. Keep in mind, that face-detection and
170 Tutorial 2 - Mixed Processing include some native code and require Android NDK and NDK/CDT plugin
171 for Eclipse to build working applications. If you haven't installed these tools, see the
172 corresponding section of @ref tutorial_android_dev_intro.
176 Please consider that some samples use Android Java Camera API, which is accessible with an AVD.
178 @note Recent *Android SDK tools, revision 19+* can run ARM v7a OS images but they available not for
179 all Android versions.
181 Well, running samples from Eclipse is very simple:
183 - Connect your device with adb tool from Android SDK or create an emulator with camera support.
184 - See [Managing Virtual Devices](http://developer.android.com/guide/developing/devices/index.html) document for help
185 with Android Emulator.
186 - See [Using Hardware Devices](http://developer.android.com/guide/developing/device.html) for
187 help with real devices (not emulators).
189 - Select project you want to start in Package Explorer and just press Ctrl + F11 or select option
190 Run --\> Run from the main menu, or click Run button on the toolbar.
192 @note Android Emulator can take several minutes to start. So, please, be patient. \* On the first
193 run Eclipse will ask you about the running mode for your application:
195 ![](images/eclipse_11_run_as.png)
197 - Select the Android Application option and click OK button. Eclipse will install and run the
200 Chances are that on the first launch you will not have the [OpenCV
201 Manager](https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p)
202 package installed. In this case you will see the following message:
204 ![](images/android_emulator_opencv_manager_fail.png)
206 To get rid of the message you will need to install OpenCV Manager and the appropriate
207 OpenCV binary pack. Simply tap Yes if you have *Google Play Market* installed on your
208 device/emulator. It will redirect you to the corresponding page on *Google Play Market*.
210 If you have no access to the *Market*, which is often the case with emulators - you will need to
211 install the packages from OpenCV4Android SDK folder manually. See `Manager Selection` for
214 <Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk
217 @note armeabi, armv7a-neon, arm7a-neon-android8, mips and x86 stand for platform targets:
218 - armeabi is for ARM v5 and ARM v6 architectures with Android API 8+,
219 - armv7a-neon is for NEON-optimized ARM v7 with Android API 9+,
220 - arm7a-neon-android8 is for NEON-optimized ARM v7 with Android API 8,
221 - mips is for MIPS architecture with Android API 9+,
222 - x86 is for Intel x86 CPUs with Android API 9+.
225 If using hardware device for testing/debugging, run the following command to learn its CPU
228 adb shell getprop ro.product.cpu.abi
230 If you're using an AVD emulator, go Window \> AVD Manager to see the list of available devices.
231 Click Edit in the context menu of the selected device. In the window, which then pop-ups, find
235 You may also see section `Manager Selection` for details.
237 When done, you will be able to run OpenCV samples on your device/emulator seamlessly.
239 - Here is Sample - image-manipulations sample, running on top of stock camera-preview of the
242 ![](images/emulator_canny.png)
247 Now, when you have your instance of OpenCV4Adroid SDK set up and configured, you may want to proceed
248 to using OpenCV in your own application. You can learn how to do that in a separate @ref tutorial_dev_with_OCV_on_Android tutorial.