Android tutorial updated
[profile/ivi/opencv.git] / doc / tutorials / introduction / android_binary_package / O4A_SDK.rst
1
2 .. _O4A_SDK:
3
4
5 OpenCV4Android SDK
6 ******************
7
8 This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK.
9
10 This guide was written with MS Windows 7 in mind, though it should work with GNU Linux and Apple
11 Mac OS as well.
12
13 This tutorial assumes you have the following software installed and configured:
14
15 * JDK
16
17 * Android SDK and NDK
18
19 * Eclipse IDE
20
21 * ADT and CDT plugins for Eclipse
22
23      ..
24
25 If you need help with anything of the above, you may refer to our :ref:`android_dev_intro` guide.
26
27 If you encounter any error after thoroughly following these steps, feel free to contact us via
28 `OpenCV4Android <https://groups.google.com/group/android-opencv/>`_ discussion group or
29 OpenCV `Q&A forum <http://answers.opencv.org>`_. We'll do our best to help you out.
30
31 Tegra Android Development Pack users
32 ====================================
33
34 You may have used `Tegra Android Development Pack <http://developer.nvidia.com/tegra-android-development-pack>`_
35 (**TADP**) released by **NVIDIA** for Android development environment setup.
36
37 Beside Android development tools the TADP 2.0 includes OpenCV4Android SDK, so it can be already
38 installed in your system and you can skip to :ref:`Running_OpenCV_Samples` section of this tutorial.
39
40 More details regarding TADP can be found in the :ref:`android_dev_intro` guide.
41
42 General info
43 ============
44
45 OpenCV4Android SDK package enables development of Android applications with use of OpenCV library.
46
47 The structure of package contents looks as follows:
48
49 ::
50
51     OpenCV-2.4.4-android-sdk
52     |_ apk
53     |   |_ OpenCV_2.4.4_binary_pack_armv7a.apk
54     |   |_ OpenCV_2.4.4_Manager_2.6_XXX.apk
55     |
56     |_ doc
57     |_ samples
58     |_ sdk
59     |    |_ etc
60     |    |_ java
61     |    |_ native
62     |          |_ 3rdparty
63     |          |_ jni
64     |          |_ libs
65     |               |_ armeabi
66     |               |_ armeabi-v7a
67     |               |_ x86
68     |
69     |_ license.txt
70     |_ README.android
71
72 * :file:`sdk` folder contains OpenCV API and libraries for Android:
73
74 * :file:`sdk/java` folder contains an Android library Eclipse project providing OpenCV Java API that
75   can be imported into developer's workspace;
76
77 * :file:`sdk/native` folder contains OpenCV C++ headers (for JNI code) and native Android libraries
78   (\*\.so and \*\.a) for ARM-v5, ARM-v7a and x86 architectures;
79
80 * :file:`sdk/etc` folder contains Haar and LBP cascades distributed with OpenCV.
81
82 * :file:`apk` folder contains Android packages that should be installed on the target Android device
83   to enable OpenCV library access via OpenCV Manager API (see details below).
84
85   On production devices that have access to Google Play Market (and Internet) these packages will be
86   installed from Market on the first start of an application using OpenCV Manager API.
87   But devkits without Market or Internet connection require this packages to be installed manually.
88   Install the `Manager.apk` and optional `binary_pack.apk` if it needed.
89   See :ref:`manager_selection` for details.
90
91   .. note:: Installation from Internet is the preferable way since OpenCV team may publish updated
92             versions of this packages on the Market.
93
94 * :file:`samples` folder contains sample applications projects and their prebuilt packages (APK).
95   Import them into Eclipse workspace (like described below) and browse the code to learn possible
96   ways of OpenCV use on Android.
97
98 * :file:`doc` folder contains various OpenCV documentation in PDF format.
99   It's also available online at http://docs.opencv.org.
100
101   .. note:: The most recent docs (nightly build) are at http://docs.opencv.org/trunk/.
102             Generally, it's more up-to-date, but can refer to not-yet-released functionality.
103
104 .. TODO: I'm not sure that this is the best place to talk about OpenCV Manager
105
106 Starting from version 2.4.3 `OpenCV4Android SDK` uses `OpenCV Manager` API for library
107 initialization. `OpenCV Manager` is an Android service based solution providing the following
108 benefits for OpenCV applications developers:
109
110 * Compact apk-size, since all applications use the same binaries from Manager and do not store
111   native libs within themselves;
112
113 * Hardware specific optimizations are automatically enabled on all supported platforms;
114
115 * Automatic updates and bug fixes;
116
117 * Trusted OpenCV library source. All packages with OpenCV are published on Google Play;
118
119      ..
120
121 For additional information on OpenCV Manager see the:
122
123 * |OpenCV4Android_Slides|_
124
125 * |OpenCV4Android_Reference|_
126
127      ..
128
129 .. |OpenCV4Android_Slides| replace:: Slides
130 .. _OpenCV4Android_Slides: https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p
131 .. |OpenCV4Android_Reference| replace:: Reference Manual
132 .. _OpenCV4Android_Reference: http://docs.opencv.org/android/refman.html
133
134 Manual OpenCV4Android SDK setup
135 ===============================
136
137 Get the OpenCV4Android SDK
138 --------------------------
139
140 #. Go to the `OpenCV download page on SourceForge <http://sourceforge.net/projects/opencvlibrary/files/opencv-android/>`_
141    and download the latest available version. Currently it's |opencv_android_bin_pack_url|_.
142
143 #. Create a new folder for Android with OpenCV development. For this tutorial we have unpacked
144    OpenCV SDK to the :file:`C:\\Work\\OpenCV4Android\\` directory.
145
146    .. note:: Better to use a path without spaces in it. Otherwise you may have problems with :command:`ndk-build`.
147
148 #. Unpack the SDK archive into the chosen directory.
149
150    You can unpack it using any popular archiver (e.g with |seven_zip|_):
151
152    .. image:: images/android_package_7zip.png
153       :alt: Exploring OpenCV package with 7-Zip
154       :align: center
155
156    On Unix you can use the following command:
157
158    .. code-block:: bash
159
160       unzip ~/Downloads/OpenCV-2.4.4-android-sdk.zip
161
162 .. |opencv_android_bin_pack| replace:: OpenCV-2.4.4-android-sdk.zip
163 .. _opencv_android_bin_pack_url: http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.4/OpenCV-2.4.4-android-sdk.zip/download
164 .. |opencv_android_bin_pack_url| replace:: |opencv_android_bin_pack|
165 .. |seven_zip| replace:: 7-Zip
166 .. _seven_zip: http://www.7-zip.org/
167
168 Import OpenCV library and samples to the Eclipse
169 ------------------------------------------------
170
171 #. Start Eclipse and choose your workspace location.
172
173    We recommend to start working with OpenCV for Android from a new clean workspace. A new Eclipse
174    workspace can for example be created in the folder where you have unpacked OpenCV4Android SDK package:
175
176       .. image:: images/eclipse_1_choose_workspace.png
177          :alt: Choosing C:\Work\android-opencv\ as workspace location
178          :align: center
179
180 #. Import OpenCV library and samples into workspace.
181
182    OpenCV library is packed as a ready-for-use `Android Library Project
183    <http://developer.android.com/guide/developing/projects/index.html#LibraryProjects>`_.
184    You can simply reference it in your projects.
185
186    Each sample included into the |opencv_android_bin_pack| is a regular Android project that already
187    references OpenCV library.Follow the steps below to import OpenCV and samples into the workspace:
188
189    .. note:: OpenCV samples are indeed **dependent** on OpenCV library project so don't forget to import it to your workspace as well.
190
191    * Right click on the :guilabel:`Package Explorer` window and choose :guilabel:`Import...` option
192      from the context menu:
193
194       .. image:: images/eclipse_5_import_command.png
195          :alt: Select Import... from context menu
196          :align: center
197
198    * In the main panel select :menuselection:`General --> Existing Projects into Workspace` and
199      press :guilabel:`Next` button:
200
201       .. image:: images/eclipse_6_import_existing_projects.png
202          :alt: General > Existing Projects into Workspace
203          :align: center
204
205    * In the :guilabel:`Select root directory` field locate your OpenCV package folder. Eclipse
206      should automatically locate OpenCV library and samples:
207
208       .. image:: images/eclipse_7_select_projects.png
209          :alt: Locate OpenCV library and samples
210          :align: center
211
212    * Click :guilabel:`Finish` button to complete the import operation.
213
214    After clicking :guilabel:`Finish` button Eclipse will load all selected projects into workspace,
215    and you have to wait some time while it is building OpenCV samples. Just give a minute to
216    Eclipse to complete initialization.
217
218    .. warning :: After the initial import, on a non-Windows (Linux and Mac OS) operating system Eclipse
219               will still show build errors for applications with native C++ code. To resolve the
220               issues, please do the following:
221
222               Open :guilabel:`Project Properties -> C/C++ Build`, and replace "Build command" text
223               to ``"${NDKROOT}/ndk-build"`` (remove .cmd at the end).
224
225    .. note :: In some cases the build errors don't disappear, then try the following actions:
226
227               * right click on ``OpenCV Library`` project -> :guilabel:`Android Tools -> Fix Project Properties`,
228                 then menu :guilabel:`Project -> Clean... -> Clean all`
229               * right click on the project with errors -> :guilabel:`Properties -> Android`, make sure the
230                 ``Target`` is selected and is ``Android 3.0`` or higher
231               * check the build errors in the :guilabel:`Problems` view window and try to resolve them by yourselves
232
233    .. image:: images/eclipse_cdt_cfg4.png
234       :alt: Configure CDT
235       :align: center
236
237    Once Eclipse completes build you will have the clean workspace without any build errors:
238
239       .. image:: images/eclipse_10_crystal_clean.png
240          :alt: OpenCV package imported into Eclipse
241          :align: center
242
243 .. _Running_OpenCV_Samples:
244
245 Running OpenCV Samples
246 ----------------------
247
248 At this point you should be able to build and run the samples. Keep in mind, that
249 ``face-detection``, ``Tutorial 3` and ``Tutorial 4`` include some native code and
250 require Android NDK and CDT plugin for Eclipse to build working applications. If you haven't
251 installed these tools, see the corresponding section of :ref:`Android_Dev_Intro`.
252
253 .. warning:: Please consider that some samples use Android Java Camera API, which is accessible
254              with an AVD. But most of samples use OpenCV Native Camera which **may not work** with
255              an emulator.
256
257 .. note:: Recent *Android SDK tools, revision 19+* can run ARM v7a OS images but they available not
258           for all Android versions.
259
260 Well, running samples from Eclipse is very simple:
261
262 * Connect your device with :command:`adb` tool from Android SDK or create an emulator with camera support.
263
264   * See `Managing Virtual Devices
265     <http://developer.android.com/guide/developing/devices/index.html>`_ document for help with Android Emulator.
266   * See `Using Hardware Devices
267     <http://developer.android.com/guide/developing/device.html>`_ for help with real devices (not emulators).
268
269
270 * Select project you want to start in :guilabel:`Package Explorer` and just press :kbd:`Ctrl + F11`
271   or select option :menuselection:`Run --> Run` from the main menu, or click :guilabel:`Run` button on the toolbar.
272
273   .. note:: Android Emulator can take several minutes to start. So, please, be patient.
274
275 * On the first run Eclipse will ask you about the running mode for your application:
276
277   .. image:: images/eclipse_11_run_as.png
278      :alt: Run sample as Android Application
279      :align: center
280
281 * Select the :guilabel:`Android Application` option and click :guilabel:`OK` button. Eclipse will install and run the sample.
282
283   Chances are that on the first launch you will not have the `OpenCV Manager <https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p>`_ package installed.
284   In this case you will see the following message:
285
286   .. image:: images/android_emulator_opencv_manager_fail.png
287      :alt: You will see this message if you have no OpenCV Manager installed
288      :align: center
289
290   To get rid of the message you will need to install `OpenCV Manager` and the appropriate `OpenCV binary pack`.
291   Simply tap :menuselection:`Yes` if you have *Google Play Market* installed on your device/emulator. It will redirect you to the corresponding page on *Google Play Market*.
292
293   If you have no access to the *Market*, which is often the case with emulators - you will need to install the packages from OpenCV4Android SDK folder manually. See :ref:`manager_selection` for details.
294
295   .. code-block:: sh
296     :linenos:
297
298     <Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.4_Manager_armv7a-neon.apk
299
300   .. note:: ``armeabi``, ``armv7a-neon``, ``arm7a-neon-android8``, ``mips`` and ``x86`` stand for
301             platform targets:
302
303             * ``armeabi`` is for ARM v5 and ARM v6 architectures with Android API 8+,
304
305             * ``armv7a-neon`` is for NEON-optimized ARM v7 with Android API 9+,
306
307             * ``arm7a-neon-android8`` is for NEON-optimized ARM v7 with Android API 8,
308
309             * ``mips`` is for MIPS architecture with Android API 9+,
310
311             * ``x86`` is for Intel x86 CPUs with Android API 9+.
312
313             If using hardware device for testing/debugging, run the following command to learn
314             its CPU architecture:
315
316             .. code-block:: sh
317
318                adb shell getprop ro.product.cpu.abi
319
320             If you're using an AVD emulator, go :menuselection:`Window > AVD Manager` to see the
321             list of availible devices. Click :menuselection:`Edit` in the context menu of the
322             selected device. In the window, which then pop-ups, find the CPU field.
323
324             You may also see section :ref:`manager_selection` for details.
325
326
327   When done, you will be able to run OpenCV samples on your device/emulator seamlessly.
328
329 * Here is ``Tutorial 2 - Use OpenCV Camera`` sample, running on top of stock camera-preview of the emulator.
330
331   .. image:: images/emulator_canny.png
332      :height: 600px
333      :alt: Tutorial 1 Basic - 1. Add OpenCV - running Canny
334      :align: center
335
336
337 What's next
338 ===========
339
340 Now, when you have your instance of OpenCV4Adroid SDK set up and configured, you may want to proceed to using OpenCV in your own application. You can learn how to do that in a separate :ref:`dev_with_OCV_on_Android` tutorial.