Building OpenCV4Android from trunk

Version 5 (Alexander Smorkalov, 2014-10-02 11:43 am)

1 1
h1. Building the trunk OpenCV for Android
2 1
3 1
{{>toc}}
4 1
5 1
The new android build is based on android-cmake project originally developed by Ethan Rublee.
6 1
You can find the latest version of his project at http://code.google.com/p/android-cmake/
7 1
8 1
h2. Prerequisites
9 1
10 1
* svn client
11 1
* cmake tool http://www.cmake.org/
12 1
* Android NDK (revision !r5 or newer) http://developer.android.com/sdk/ndk/index.html
13 1
* OpenCV trunk http://code.opencv.org/svn/opencv/trunk/opencv. You can checkout sources with the following command:
14 1
<pre>svn co http://code.opencv.org/svn/opencv/trunk/opencv</pre>
15 1
16 1
h2. Additional requirements for Java wrapper and Android samples
17 1
18 1
* JDK 6 http://www.oracle.com/technetwork/java/javase/downloads/index.html
19 1
* Android SDK http://developer.android.com/sdk/index.html
20 1
** "Android SDK Tools, revision 14" or newer is recommended
21 1
** "SDK platform Android 2.2, API 8" or newer package installed (for the Java API and tests)
22 1
** "SDK platform Android 3.0, API 11" or newer for the samples (they can run on Android 2.2 but require API 11 at compile time)
23 1
* Apache Ant 1.8.0 or newer http://ant.apache.org
24 1
* Python 2.6 or newer (but not 3.x) http://www.python.org/
25 1
26 1
h2. Windows additional prerequisites
27 1
28 1
At the moment OpenCV cross compilation under @cygwin@ is *not supported*.
29 1
30 1
However you can cross-compile OpenCV on Windows using '''native port of make'''.
31 1
Android NDK !r7 and newer already includes the make executable.
32 1
If you need to build OpenCV with older NDK, then you can get it from NDK !r7 or use one of alternative downloads. These two are tested to work for OpenCV:
33 1
* http://gnuwin32.sourceforge.net/packages/make.htm
34 1
* http://code.google.com/p/mingw-and-ndk/ (see "make-3.7z" under the Downloads section)
35 1
36 1
h1. OpenCV build
37 1
38 1
By default OpenCV and the most of Android samples are configured for modern @ARM-v7a@ architecture.
39 5 Alexander Smorkalov
If you want to compile OpenCV for emulator or older device, or want to build with NEON support please read the [[Trunk OpenCV for Android#Android-hardware-targets|Android targets]] section for instructions.
40 1
41 1
h2. Linux (Ubuntu) and Mac OS (10.6.7)
42 1
43 1
Export full path to your copy of NDK
44 1
<pre>
45 1
export ANDROID_NDK=~/android-ndk-r7
46 1
</pre>
47 1
48 1
Alternatively to build with standalone toolchain extracted from NDK you can export toolchain location
49 1
<pre>
50 1
export ANDROID_STANDALONE_TOOLCHAIN=~/android-toolchain
51 1
</pre>
52 1
53 1
If you have not declared any of these variables then build scripts will try to find NDK under the @/opt@ folder
54 1
(so if you have placed your NDK to @/opt/android-ndk-r7b/@ then it will be detected automatically).
55 1
56 1
There is a @scripts@ folder in the @android@ folder for running cmake with the proper cache
57 1
variables set.  It is recommended that you use this to setup a cmake build directory.
58 1
<pre>
59 1
cd opencv/android
60 1
sh ./scripts/cmake_android.sh
61 1
</pre>
62 1
63 1
You should now see a build directory, that is ready to be made
64 1
<pre>
65 1
cd build
66 1
make -j8
67 1
</pre>
68 1
69 1
That will build most of the OpenCV modules, except for those that don't make sense on android - @gpu@, etc..
70 1
71 1
h3. Troubleshooting
72 1
73 1
If you get an error saying `install_name_tool not found`, then:
74 1
# search for the file @install_name_tool@ on your file system (usually it is in @/usr/bin/@);
75 1
# open the file @/opt/local/share/cmake-2.8/Modules/CMakeFindBinUtils.cmake@;
76 1
# after the line @FIND_PROGRAM(CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool HINTS ${_CMAKE_TOOLCHAIN_LOCATION})@ add the line
77 1
<pre>
78 1
set( CMAKE_INSTALL_NAME_TOOL <path to install_name_tool>)
79 1
</pre>
80 1
e.g. something like
81 1
<pre>
82 1
set( CMAKE_INSTALL_NAME_TOOL /usr/bin/install_name_tool)
83 1
</pre>
84 1
Next remove your @build@ folder and restart from the @./scripts/cmake_android.sh@ script.
85 1
86 1
h2. Windows (native)
87 1
88 1
Define the following environment variables:
89 1
* @ANDROID_NDK@ - full path to the Android NDK. This path should not have tailing slash.
90 1
* @CMAKE_EXE@ - full path to cmake executable (including executable name).
91 1
* @MAKE_EXE@ - full path to native port of make (including executable name). This path should not contain spaces.
92 1
93 1
Instead of modifying your environment you can create file @opencv\android\scripts\wincfg.cmd@ and put all settings into this file. OpenCV provides a template for this file named @opencv\android\scripts\wincmd.cfg.tmpl@. Please note that settings from @wincfg.cmd@ takes precedence over global environment variables.
94 1
95 1
Then open Windows console (@cmd.exe@) and go to @opencv\android@ folder. Run script @cmake_android.cmd@ from the @scripts@ folder:
96 1
<pre>
97 1
cd opencv\android\
98 1
scripts\cmake_android.cmd
99 1
</pre>
100 1
When script finishes you will have OpenCV compiled.
101 1
102 1
h3. Troubleshooting
103 1
104 1
First, make sure that paths to OpenCV sources, Android NDK and @make.exe@ do not contain spaces and other suspicious symbols.
105 1
106 1
If @make@ fails with message similar to
107 1
<pre>
108 1
*** target pattern contains no `%'.  Stop.
109 1
</pre>
110 1
Then you are probably using @cygwin@ tools instead of their native ports. Remove @cygwin@ from your @PATH@, delete @build@ directory and rebuild.
111 1
112 1
Or if @make@ fails with Java related messages, then make sure your version of python is a Windows app instead of the @cygwin@ one, such as "ActiveState Python v2.7":http://www.activestate.com/activepython/downloads.
113 1
114 1
115 1
h1. Building console "Hello World" with OpenCV
116 1
117 1
This application is not usual Android application. This is a Unix console which may be used as a template for development. Console application is much more convenient for development, debugging and performance optimization stages. Then, if you are finished with your CV algorithm, you can switch to normal Android development with Java + NDK.
118 1
119 1
h2. Prerequisities
120 1
121 1
First of all you need an Android device with root privileges. The goal is to have ability to run console applications on Android device using @adb shell@. Even @chmod@ is not available by default (for example on Nexus One). You have the following options:
122 1
* Use emulator
123 1
* Enable root access to your device. Use ''Google'':http://google.com to find instructions for your device.
124 1
* Use developer device, for example NVidia Tegra devkits have root access by default.
125 1
126 1
If the device allows you to do @chmod@ and run executables, then follow the instructions below.
127 1
128 5 Alexander Smorkalov
Second, you need OpenCV compiled for your device. *This sample is configured for emulator. It is different from default settings for OpenCV.* Read [[Trunk OpenCV for Android#Android-hardware-targets|Android ABIs]] to know how to build OpenCV for emulator.
129 1
Also you need some free space on sdcard to run the @HelloAndroid@ application - it will write small image to @/mnt/sdcard@. *Please note*, that even with emulator you need a sdcard. You can create it with @AVD Manager@ from Android SDK.
130 2 Alexander Smorkalov
131 2 Alexander Smorkalov
h2. Linux (Ubuntu) and Mac OS (10.6.7)
132 2 Alexander Smorkalov
133 2 Alexander Smorkalov
Export full path to your copy of NDK
134 2 Alexander Smorkalov
<pre>
135 2 Alexander Smorkalov
export ANDROID_NDK=~/android-ndk-r7
136 2 Alexander Smorkalov
</pre>
137 2 Alexander Smorkalov
or path to standalone toolchain
138 2 Alexander Smorkalov
<pre>
139 2 Alexander Smorkalov
export ANDROID_STANDALONE_TOOLCHAIN=~/android-toolchain
140 2 Alexander Smorkalov
</pre>
141 2 Alexander Smorkalov
142 2 Alexander Smorkalov
Run the following commands to build project
143 2 Alexander Smorkalov
<pre>
144 2 Alexander Smorkalov
cd opencv/samples/android/hello-android
145 2 Alexander Smorkalov
sh ./cmake_android.sh
146 2 Alexander Smorkalov
cd ./build_armeabi
147 2 Alexander Smorkalov
make
148 2 Alexander Smorkalov
cd ..
149 2 Alexander Smorkalov
</pre>
150 2 Alexander Smorkalov
151 2 Alexander Smorkalov
Connect to your device with @adb@ tool from Android SDK and run the application
152 2 Alexander Smorkalov
<pre>
153 2 Alexander Smorkalov
sh ./run.sh
154 2 Alexander Smorkalov
</pre>
155 2 Alexander Smorkalov
156 2 Alexander Smorkalov
If you can see @HelloAndroid.png@ in your current directory and @Hello Android!@ message in the console - you've arrived.
157 2 Alexander Smorkalov
158 2 Alexander Smorkalov
If you want to build this sample for @ARM-v7a@ device (with or without NEON support) you need to edit @cmake_android.sh@ script. Change @BUILD_DIR@ path and @ANDROID_ABI@ CMake parameter.
159 2 Alexander Smorkalov
160 2 Alexander Smorkalov
h2. Windows
161 2 Alexander Smorkalov
162 2 Alexander Smorkalov
Set the same environment variables as needed for OpenCV build (variables set in @opencv\android\scripts\wincfg.cmd@ work as well).
163 2 Alexander Smorkalov
If you are using @wincfg.cmd@ file then ensure that @ANDROID_ABI@ and @BUILD_DIR@ are set to correct values. Use the following values for emulator:
164 2 Alexander Smorkalov
<pre>
165 2 Alexander Smorkalov
SET BUILD_DIR=build_armeabi
166 2 Alexander Smorkalov
SET ANDROID_ABI=armeabi
167 2 Alexander Smorkalov
</pre>
168 2 Alexander Smorkalov
169 2 Alexander Smorkalov
When all variables are set run the commands
170 2 Alexander Smorkalov
<pre>
171 2 Alexander Smorkalov
cd opencv\samples\android\hello-android
172 2 Alexander Smorkalov
cmake_android.cmd
173 2 Alexander Smorkalov
</pre>
174 2 Alexander Smorkalov
175 2 Alexander Smorkalov
Connect to your device with @adb@ tool from Android SDK and run the script
176 2 Alexander Smorkalov
<pre>
177 2 Alexander Smorkalov
run.cmd
178 2 Alexander Smorkalov
</pre>
179 2 Alexander Smorkalov
180 2 Alexander Smorkalov
If you can see @HelloAndroid.png@ in your current directory and "Hello Android!" message in console - you've arrived.
181 2 Alexander Smorkalov
182 2 Alexander Smorkalov
h3. Troubleshooting
183 2 Alexander Smorkalov
184 2 Alexander Smorkalov
If you see error message similar to
185 2 Alexander Smorkalov
<pre>
186 2 Alexander Smorkalov
ld: cannot find -lopencv_contrib
187 2 Alexander Smorkalov
</pre>
188 2 Alexander Smorkalov
or (on Windows)
189 2 Alexander Smorkalov
<pre>
190 2 Alexander Smorkalov
ld.exe: cannot find -lopencv_contrib240
191 2 Alexander Smorkalov
</pre>
192 2 Alexander Smorkalov
It means that CMake failed to find OpenCV compiled for Android. Check that all paths are set to correct values, remove @build@ directory and try to rebuild the application.
193 2 Alexander Smorkalov
194 2 Alexander Smorkalov
h1. Native camera support in OpenCV
195 2 Alexander Smorkalov
196 2 Alexander Smorkalov
Native interface of Android camera is platform-dependent. So OpenCV isolates platform-dependent part of code into separate shared library and provides several prebuilt libraries for native camera:
197 2 Alexander Smorkalov
* @libnative_camera_r2.2.0.so@
198 2 Alexander Smorkalov
* @libnative_camera_r2.3.3.so@
199 2 Alexander Smorkalov
* @libnative_camera_r3.0.1.so@
200 2 Alexander Smorkalov
* @libnative_camera_r4.0.0.so@
201 2 Alexander Smorkalov
* @libnative_camera_r4.0.3.so@
202 2 Alexander Smorkalov
203 2 Alexander Smorkalov
These libraries are tested to work for Android 2.2.0, 2.2.1, 2.2.2, 2.3.3, 2.3.4, 3.0.1, 3.2.1, 4.0.0, 4.0.3.
204 2 Alexander Smorkalov
You should include these libraries into your .apk to get camera working. OpenCV will automatically load one of these libraries compatible with your Android OS version.
205 2 Alexander Smorkalov
206 2 Alexander Smorkalov
h2. Use native camera in your Android project (Java)
207 2 Alexander Smorkalov
208 2 Alexander Smorkalov
See android samples for example of native camera application.
209 2 Alexander Smorkalov
This one - @opencv/samples/android/tutorial-2-opencvcamera@ is a good point to start.
210 2 Alexander Smorkalov
211 2 Alexander Smorkalov
h2. Use native camera in your NDK project (C++)
212 2 Alexander Smorkalov
213 2 Alexander Smorkalov
Tutorial is coming...
214 2 Alexander Smorkalov
215 2 Alexander Smorkalov
h2. Use native camera in your cmake project (C++)
216 2 Alexander Smorkalov
217 2 Alexander Smorkalov
Include the @highgui.hpp@ header to you source
218 2 Alexander Smorkalov
<pre><code class="cpp">
219 2 Alexander Smorkalov
#include "opencv2/highgui/highgui.hpp"
220 2 Alexander Smorkalov
</code></pre>
221 2 Alexander Smorkalov
222 2 Alexander Smorkalov
Create a new instance of @VideoCapture@ and start grabbing frames
223 2 Alexander Smorkalov
<pre><code class="cpp">
224 2 Alexander Smorkalov
cv::VideoCapture capture(CV_CAP_ANDROID + 0);
225 2 Alexander Smorkalov
//cv::VideoCapture capture(CV_CAP_ANDROID + 1);//front camera for Android 2.3.3 or newer
226 2 Alexander Smorkalov
227 2 Alexander Smorkalov
if( !capture.isOpened() )
228 2 Alexander Smorkalov
    return;
229 2 Alexander Smorkalov
230 2 Alexander Smorkalov
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
231 2 Alexander Smorkalov
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
232 2 Alexander Smorkalov
233 2 Alexander Smorkalov
for(int i=0; i < 100; ++i)
234 2 Alexander Smorkalov
{
235 2 Alexander Smorkalov
    cv::Mat frame;
236 2 Alexander Smorkalov
237 2 Alexander Smorkalov
    capture >> frame;
238 2 Alexander Smorkalov
    if( !frame.empty() )
239 2 Alexander Smorkalov
        yourfunction::processFrame(frame);
240 2 Alexander Smorkalov
}
241 2 Alexander Smorkalov
</code></pre>
242 2 Alexander Smorkalov
243 2 Alexander Smorkalov
Finally, add following lines to your cmake
244 2 Alexander Smorkalov
<pre>
245 2 Alexander Smorkalov
if (COMMAND COPY_NATIVE_CAMERA_LIBS)
246 2 Alexander Smorkalov
  COPY_NATIVE_CAMERA_LIBS(${PROJECT_NAME})
247 2 Alexander Smorkalov
endif()
248 2 Alexander Smorkalov
</pre>
249 2 Alexander Smorkalov
Where @PROJECT_NAME@ variable contains name of module that uses native camera.
250 2 Alexander Smorkalov
251 2 Alexander Smorkalov
*Warning: Native camera from OpenCV does not work inside console applications.*
252 2 Alexander Smorkalov
253 2 Alexander Smorkalov
h2. Advanced: build your camera wrapper
254 2 Alexander Smorkalov
255 2 Alexander Smorkalov
If prebuilt libraries don't work for you for whatever reason then you can build your own camera wrapper lib.
256 2 Alexander Smorkalov
257 2 Alexander Smorkalov
To build your lib you have to download sources for your version of Android and build entire OS first.
258 2 Alexander Smorkalov
See http://source.android.com/source/downloading.html and ''google'' for instructions.
259 2 Alexander Smorkalov
260 2 Alexander Smorkalov
When you finish with Android OS run cmake for OpenCV (@scripts/cmake_android.sh@ will run it for you). You should see following line in its output
261 2 Alexander Smorkalov
<pre>
262 2 Alexander Smorkalov
--     AndroidNativeCamera:        use prebuilt libraries
263 2 Alexander Smorkalov
</pre>
264 2 Alexander Smorkalov
265 2 Alexander Smorkalov
Next, you need to set cmake cache variable @ANDROID_SOURCE_TREE@ to the path to Android OS source tree and rerun cmake. (You can set this variable with @cmake-gui@ application, or editing @build/CMakeCache.txt@ file with text editor, or passing it as cmake argument).
266 2 Alexander Smorkalov
267 2 Alexander Smorkalov
If path is set correctly then you should see line similar to following in cmake output
268 2 Alexander Smorkalov
<pre>
269 2 Alexander Smorkalov
--     AndroidNativeCamera:        build for Android 2.3.4
270 2 Alexander Smorkalov
</pre>
271 2 Alexander Smorkalov
272 2 Alexander Smorkalov
Run @make@ command to compile the sources.
273 2 Alexander Smorkalov
274 2 Alexander Smorkalov
Most probably you will need to make changes in file @opencv/modules/androidcamera/camera_wrapper/camera_wrapper.cpp@ to fix compiler errors.
275 2 Alexander Smorkalov
If you do some changes please post your patch to [[WikiStart|OpenCV issue tracker]] to help with OpenCV development.
276 2 Alexander Smorkalov
277 2 Alexander Smorkalov
h3. Example
278 2 Alexander Smorkalov
279 2 Alexander Smorkalov
Building wrapper library for Android 4.0.3:
280 2 Alexander Smorkalov
281 2 Alexander Smorkalov
<pre>
282 2 Alexander Smorkalov
cd openvc/android
283 2 Alexander Smorkalov
mkdir build_camera
284 2 Alexander Smorkalov
cd build_camera
285 2 Alexander Smorkalov
cmake -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake -DANDROID_SOURCE_TREE=~/Android/4.0.3/src -DANDROID_NATIVE_API_LEVEL=14 -DANDROID_USE_STLPORT=ON ../../
286 2 Alexander Smorkalov
make native_camera_r4.0.3
287 2 Alexander Smorkalov
</pre>
288 2 Alexander Smorkalov
289 2 Alexander Smorkalov
h1. Advanced build options
290 2 Alexander Smorkalov
291 2 Alexander Smorkalov
h2. Using OpenCV in your own CMake projects
292 2 Alexander Smorkalov
293 2 Alexander Smorkalov
Use the CMake find script for OpenCV
294 2 Alexander Smorkalov
<pre>
295 2 Alexander Smorkalov
find_package(OpenCV REQUIRED)
296 2 Alexander Smorkalov
</pre>
297 2 Alexander Smorkalov
Export path to OpenCV source tree
298 2 Alexander Smorkalov
<pre>
299 2 Alexander Smorkalov
export OPENCV_ROOT=~/opencv
300 2 Alexander Smorkalov
</pre>
301 2 Alexander Smorkalov
Then when you run cmake, use:
302 2 Alexander Smorkalov
<pre>
303 2 Alexander Smorkalov
cmake -DOpenCV_DIR=$OPENCV_ROOT/android/build -DCMAKE_TOOLCHAIN_FILE=$OPENCV_ROOT/android/android.toolchain.cmake ..
304 2 Alexander Smorkalov
</pre>
305 2 Alexander Smorkalov
306 2 Alexander Smorkalov
And direct your cmake cache for @OpenCV_DIR@ to the path where you built OpenCV for Android.
307 2 Alexander Smorkalov
something like : @opencv/android/build@
308 2 Alexander Smorkalov
309 2 Alexander Smorkalov
To avoid setting the cmake cache for @OpenCV_DIR@, you can just "install" OpenCV to your
310 2 Alexander Smorkalov
Android toolchain/NDK. Run the following from the @opencv/android/build@ path:
311 2 Alexander Smorkalov
<pre>
312 2 Alexander Smorkalov
make install/strip
313 2 Alexander Smorkalov
</pre>
314 2 Alexander Smorkalov
315 2 Alexander Smorkalov
h2. Android hardware targets
316 2 Alexander Smorkalov
317 2 Alexander Smorkalov
You may wish to build OpenCV and samples for multiple hardware targets.
318 2 Alexander Smorkalov
319 2 Alexander Smorkalov
Just change the cmake cache @ANDROID_ABI@ to either:
320 2 Alexander Smorkalov
* @armeabi@
321 2 Alexander Smorkalov
* @armeab-v7a@
322 2 Alexander Smorkalov
* @armeab-v7a with NEON@
323 2 Alexander Smorkalov
* @armeabi-v7a with VFPV3@
324 2 Alexander Smorkalov
* @armeabi-v6 with VFP@
325 2 Alexander Smorkalov
* @x86@
326 2 Alexander Smorkalov
327 2 Alexander Smorkalov
h2. Android API levels
328 2 Alexander Smorkalov
329 2 Alexander Smorkalov
You may wish to build OpenCV and samples for different level of Android native API.
330 2 Alexander Smorkalov
331 2 Alexander Smorkalov
Just change the cmake cache @ANDROID_NATIVE_API_LEVEL@ to appropriative value.
332 2 Alexander Smorkalov
333 2 Alexander Smorkalov
The following levels are currently supported:
334 3 Alexander Smorkalov
335 3 Alexander Smorkalov
|_. Platform Version |_. SDK API Level |_. NDK API Level|
336 3 Alexander Smorkalov
|Android 4.0.3|=.15|/4=.14|
337 3 Alexander Smorkalov
|Android 4.0.2|/3=.14|
338 3 Alexander Smorkalov
|Android 4.0.1|
339 3 Alexander Smorkalov
|Android 4.0|
340 3 Alexander Smorkalov
|Android 3.2|=.13|/8=.9|
341 3 Alexander Smorkalov
|Android 3.1.x|=.12|
342 3 Alexander Smorkalov
|Android 3.0.x|=.11|
343 3 Alexander Smorkalov
|Android 2.3.4|/2=.10|
344 3 Alexander Smorkalov
|Android 2.3.3|
345 3 Alexander Smorkalov
|Android 2.3.2|/3=.9|
346 3 Alexander Smorkalov
|Android 2.3.1|
347 3 Alexander Smorkalov
|Android 2.3|
348 3 Alexander Smorkalov
|Android 2.2.x|=.8|=.8|
349 3 Alexander Smorkalov
|Android 2.1.x|=.7|/3=.5|
350 3 Alexander Smorkalov
|Android 2.0.1|=.6|
351 3 Alexander Smorkalov
|Android 2.0|=.5|
352 3 Alexander Smorkalov
|Android 1.6|=.4|=.4|
353 3 Alexander Smorkalov
|Android 1.5|=.3|=.3|
354 3 Alexander Smorkalov
|Android 1.1|=.2|/2=.-|
355 3 Alexander Smorkalov
|Android 1.0|=.1|