OpenCV 4 Android

From Link wiki
Jump to navigation Jump to search

OpenCV – introduction to Android development

Getting Started

This project assumes that you will be working within a Windows 7 or newer operating system.

Setting up Android development environment

You must download the following:

Java JDK (6 or newer) found here:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Android SDK Tools:

https://developer.android.com/sdk/index.html#Other

Android NDK:

https://developer.android.com/ndk/downloads/index.html

Eclipse Classic (version 4.2.2 Juno)

http://www.eclipse.org/downloads/packages/eclipse-classic-422/junosr2

OpenCV-3.0.0-android-sdk-1.zip (or the package of your choice):

http://sourceforge.net/projects/opencvlibrary/files/opencv-android/

Installation

install Java JDK – install 32 or 64 bit depending on your operating system

  • NOTE: one of the issues that may occur when installing Android SDK Tools on a 64 bit machine is that it may not detect the Java JDK. This anomally had occurred when installing Java JDK 7 (64 bit version). Should this issue arise, you must copy java.exe from
C:\Windows\System32 

and paste it into the directory:

C:\Windows\SysWOW64

Install Android SDK - customize which version of Android you want to develop for (forwards compatible, so Android 2.3 will work for 5.0 but will be lacking features). 5.1 and M are chosen by default, you may uncheck them and choose another if your device is older. Leave the rest as default. (Note: if you are getting access denied errors when attempting to download packages, run Android SDK manager as Adminstrator)

Copy the self-extracting NDK compressed file and place it in the directory you prefer. Then proceed to run it.

Extract Eclipse and OpenCV-android-sdk.zip to your location of preference

Setting up Eclipse

Install the Eclipse ADT plugin. To do this open Eclipse, in the top menu click HELP, then INSTALL NEW SOFTWARE, a new window should pop up, paste the location of the ADT plugin to be downloaded which is:

https://dl-ssl.google.com/android/eclipse/

Hit ADD. Give it the name ADT. SELECT ALL and NEXT. NEXT again, ACCEPT and FINISH. It download automatically and will prompt to restart after done installing, click yes.

Set NDK path under WINDOW → PREFERENCES → ANDROID → NDK and point it to the root file directory where the NDK file is. Use double back slashes or single forward slashes (i.e. "X:\\Apps\\android-ndk-r8" or "/opt/android-ndk-r8")

Import openCV-android-sdk by right clicking inside the package explorer window, click IMPORT, under GENERAL click EXISTING PROJECTS INTO WORKSPACE. In select root directory hit BROWSE and select the file where you extracted the openCV-android-sdk file. Hit SELECT ALL and FINISH.

Right click on the package manager of the eclispe screen and select IMPORT. You want to import an existing Android Project. When prompted, select the OpenCV4Android file containing the samples, sdk, apk etc.

Repeat the following steps for EACH file in the package explorer: Select a file under the package explorer. Then create a new environment variable. Do this by: right clicking PROPERTIES on package explorer → C++ build → Environment → add "NDKROOT" for name and the file path to the NDK root file. Once again, use double forward slashes or single back slashes instead of a single back slashes for the file path. Click REPLACE NATIVE ENVRONMENT WITH SPECIFIED ONE at the bottom. APPLY and OK.

Under C/C++ Build, BUILDER SETTINGS TAB, uncheck USE DEFAULT BUILD COMMAND and type in “${NDKROOT}/ndk-build.cmd”

Next, under the BEHAVIOR tab in the same window, uncheck CLEAN and check STOP ON FIRST BUILD ERROR and BUILD ON RESOURCE SAVE. Delete the “all” value within those fields.

Lastly, on the left plane click ANDROID and for PROJECT BUILD TARGET check the android version you will be developing toward.

After doing this for each file, restart Eclipse. Eclipse should now be properly configured and display no errors.

Setting up Android devices

Make sure the device is not connected to the computer yet.

Assuming your device does not have a developer options tab unlocked already, go to your SETTINGS, ABOUT PHONE or ABOUT TABLET, and tap on the BUILD NUMBER or SW VERSION multiple times until it tells you “You are now a developer!”

Now for device installation, you must set up adb, set up usb debugging by going under the DEVELOPER OPTIONS, check ANDROID DEBUGGING and USB DEBUGGING NOTIFY.

Download adb drivers for the device must be downloaded. Disable mtp or ptp storage and have the device set to charge only mode. This is done differently on each device. For Cyanogenmod, go to SETTINGS, STORAGE, click on the 3 dot button on the right corner. Make sure MTP and PTP storage are unchecked. For the ZTE speed, go to SETTINGS, CONNECT TO PC, and check CHARGE ONLY.

Connect your device to your computer. It should automatically download the adb drivers. If not, first install the manufacture drivers for your device. A quick search on the web of the model and brand of device should be sufficient to find the drivers if they do not come up automatically from your device. Install these drivers, reconnect the device in CHARGE ONLY mode once again and adb drivers should install.

When connecting your device to the computer you should now get a prompt asking, “Allow USB debugging?”. If it is your personal computer, check “Always allow from this computer” for convenience. Then click OK (WARNING: if you do not complete this step you will be unable to proceed further)

Next you will need the OpenCV Manager app for your device. This can be obtained from within your OpenCV-Android-sdk folder. To install it, you will first need to do the following steps:

1) Get cmd to recognize 'adb' as a command To do this, on your windows machine open CONTROL PANEL, click SYSTEM AND SECURITY, then SYSTEM, on the left click ADVANCED SYSTEM SETTINGS hit ENVIRONMENT VARIABLES. Hit NEW and add:

Variable name:

ANDROID_PLATFORM_TOOLS

Variable Value:

C:\Users\USERNAME\Downloads\adt-bundle-windows-x86_64\sdk\platform-tools

click OK. Then NEW again and add:

Variable name:

ANDROID_TOOLS

Variable Value:

C:\Users\USERNAME\Downloads\adt-bundle-windows-x86_64\sdk\tools

click OK. Then NEW again and add:

Variable name:

PATH

Variable Value:

%Path%;%ANDROID_PLATFORM_TOOLS%;%ANDROID_TOOLS%

click OK and OK on the environment variables windows and OK on system properties.


2) Now open up CMD in Windows and run as admin

check your device is listed by typing:

adb devices

get the architecture of your device by typing:

adb shell getprop ro.product.cpu.abi

install the opencv manager from the directory location of the opencv file:

adb install  c:\...\opencv-android-sdk\apk\OpenCV_3.0.0_Manager_3.00_(choose the one corresponding to your device's architecture).apk

or for exmaple:

adb shell getprop ro.product.cpu.abi gave: armeabi-v7a

thus we will do:

adb install C:\...\opencv-android-sdk\apk\armeabi-v7a.apk

You now have installed the OpenCV library required on the android device.

Running the samples

Ensure your device is connected to your computer with the screen on and unlocked.

In Eclispe, in the PACKAGE EXPLORER, right click a package and hit RUN AS → ANDROID APPLICATION. Now wait for it to load on your device.

The package will be compiled as an .apk on the phone automatically and will create a new logo with the openCV icon. Repeat for each desired package.