Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV Android Programming By Example

You're reading from   OpenCV Android Programming By Example Leverage OpenCV to develop vision-aware and intelligent Android applications.

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781783550593
Length 202 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Installing the OpenCV and Android development environment manually

To choose to manually install OpenCV and the Android development environment, you probably have the following installed components on your machine:

  • Java SE Development Kit 6
  • Android Studio
  • Android SDK
  • Eclipse IDE
  • ADT and CDT plugin for Eclipse
  • Android NDK
  • OpenCV4Android SDK

You could go through the manual installation steps to make sure that you have all the needed components in order to start developing Android applications with OpenCV is ready and properly configured.

Java SE Development Kit 6

You can download the JDK installer for your OS from http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Android Studio

Another very good option to work with is the Android Studio. You can download the Android Studio from http://developer.android.com/sdk/index.html. Note that Android Studio comes bundled with Android SDK, so you don't need to install it if you go with this option. Additionally, you can skip the Eclipse and ADT installation and note that starting from Android Studio 1.3; you will find built-in support for NDK as well.

Android SDK

To download and install Android SDK, follow these steps:

  1. Go to http://developer.android.com/sdk/index.html.
  2. Scroll down to the SDK Tools Only section and click on the .exe file of Windows installer link.
  3. After you have read and accepted the terms and conditions, click the download button.
  4. Save the installer on your disk and click on the .exe file to start the installer and then follow the onscreen instructions.
  5. Keep a note of the SDK directory to refer to it later from the command line.
  6. Once the installation is done, Android SDK Manager will start.
  7. Select to install Android SDK Tools, revision 20 or newer.
  8. For the SDK platform, Android, select Android 3.0 (API 11) or higher. In my case, I used API 15 and you are recommended to do so.
  9. Read and accept the license agreement, then click Install.

Eclipse IDE

For OpenCV 2.4.x, it is recommended to have Eclipse 3.7 (Indigo) or Eclipse 4.2 (Juno); you can download your selected version from Eclipse's official website at http://www.eclipse.org/downloads/.

ADT and CDT plugins for Eclipse

Assuming that you have already downloaded Eclipse, you can follow these steps to download the Android Developer Tools (ADT) and C/C++ Development Tool (CDT) plugins:

  1. Launch Eclipse and then navigate to Help | Install New Software.
  2. Click the Add button in the top corner to the right.
  3. In the Add Repository dialog, write ADT Plug-in in the Name field and copy and paste this URL, https://dl-ssl.google.com/android/eclipse/, in the Location field.
  4. Click OK.
  5. Check the Developer Tools checkbox.
  6. Click Next.
  7. A list of the tools to be downloaded will be shown in the next window. Just make sure that it includes the native support tools (CDT) and click Next.
  8. Read and accept the license agreement and click Finish.
  9. Once the installation is complete, you will need to restart Eclipse.

Android NDK

In order to develop for Android in C++, you will need to install Android NDK.

Note

Android NDK is not meant to be used in all situations. As a developer, you need to balance between the performance gains that come with using a native API and the introduced complexity.

In our case, as the OpenCV libraries are written in C/C++, we might have to use NDK. However, using NDK shouldn't be just because the programmer prefers to write in C/C++.

Downloading Android NDK

You can download Android NDK by following these steps:

  1. Go to the Android NDK home page, http://developer.android.com/tools/sdk/ndk/index.html.
  2. In the Downloads section, select the version corresponding to your operating system. In my case, it is Windows 64-bit.
  3. Read and agree with the terms and conditions.
  4. Click the Download button.

Installing and configuring Android NDK

Once the download has finished, you will need to follow these steps to configure NDK:

  1. Navigate to the NDK download folder.
  2. Double-click on the downloaded file to extract it.
  3. Rename and move the extracted folder; I'll refer to the ndk folder as <ndk_home>. Now you are ready to use NDK to build your projects.
  4. If you prefer to build from the command line, you will need to add the <ndk_home> folder (in my case, C:/android/android-ndk-r10d) to your PATH environment variable. For Windows, open CMD. Enter the following command and replace the ndk directory with yours:
    set PATH=%PATH%;c:/android/android-ndk-r10d
    
  5. To check that NDK is configured properly, go to the directory that contains your project. For simplicity, you can test on the hello-jni sample project. You can find it under <ndk_home>/samples/.
  6. Change the directory by executing the command cd <your_project_directory>/. Run the following command:
    ndk-build
    
  7. As depicted in the console output, the files with the .so extension are the compiled version of the C/C++ source code used in this project:
    Installing and configuring Android NDK

Building native code using Eclipse

If you prefer to build from Eclipse, which is more convenient, you will need to tell Eclipse where to find NDK so that you can build your apps:

  1. Launch Eclipse and open Window | Preferences.
  2. In the left-hand side pane, open the Android tree.
  3. Select the NDK tree node and in the right-hand side pane, click Browse and select the <ndk_home> directory.
  4. Click OK.
  5. Import the hello-jni sample project from <ndk_home>/samples/ as an Android project.
  6. Open the Project Explorer and right-click on the hello-jni project.
  7. In the context menu, navigate to Android Tools | Add Native Support to convert this project to a C++ project.
  8. Accept the default library name and click Finish.
  9. Build the application.

In the console, you will see a list of .so files, which are the compiled C++ part of this project. Still, if you open any C/C++ file from the imported project, you will see many highlighted errors. You just need to do some extra steps related to the CDT plugin:

  1. Navigate to Project | Properties. In the left-hand side pane, expand the C/C++ General node.
  2. Select Paths and Symbols.
  3. In the right-hand side pane, select the Includes tab.
  4. Click Add and then File system to add the following paths:
    • If you installed NDK r8 or prior:

      <ndk_home>/platforms/android-9/arch-arm/usr/include<ndk_home>/sources/cxx-stl/gnu-libstdc++/include<ndk_home>/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/include

    • If you installed NDK r8b or later:

      <ndk_home> /platforms/android-9/arch-arm/usr/include

      <ndk_home>/sources/cxx-stl/gnu-libstdc++/4.6/include

      <ndk_home> /sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include

  5. Click OK. Eclipse will rebuild the project and all the syntax errors should be cleared from Eclipse.
  6. Now, you can build the project to package both the Java and native code in one APK. To install the application on the emulator of your choice, use the menu item, Run | Run As | Android Application.

OpenCV4Android SDK

To be able to use the OpenCV collection of native (C/C++) libraries on your Android device, you need to install OpenCV4Android SDK, which is a part of OpenCV to run on the Android operating system.

  1. First, go to the OpenCV download page, http://sourceforge.net/projects/opencvlibrary/files/opencv-android/.
  2. Download the latest available version, which, at the time this book is being written, was 2.4.10.
  3. Extract the compressed file to a convenient path, for example, C:\opencv\.

    Note

    It is highly recommended to use paths with no spaces to avoid any problems with ndk-build.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image