- Use Android Studio instead of Ecliplse. - Use new Gradle-based OpenCV for Android SDK.pull/24653/head
@ -1,255 +0,0 @@ |
||||
OpenCV4Android SDK {#tutorial_O4A_SDK} |
||||
================== |
||||
|
||||
@prev_tutorial{tutorial_android_dev_intro} |
||||
@next_tutorial{tutorial_dev_with_OCV_on_Android} |
||||
|
||||
| | | |
||||
| -: | :- | |
||||
| Original author | Vsevolod Glumov | |
||||
| Compatibility | OpenCV >= 3.0 | |
||||
|
||||
@warning |
||||
This tutorial is deprecated. |
||||
|
||||
This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK. |
||||
|
||||
This guide was written with MS Windows 7 in mind, though it should work with GNU Linux and Apple Mac |
||||
OS as well. |
||||
|
||||
This tutorial assumes you have the following software installed and configured: |
||||
|
||||
- JDK |
||||
- Android SDK and NDK |
||||
- Eclipse IDE |
||||
- ADT and CDT plugins for Eclipse |
||||
|
||||
If you need help with anything of the above, you may refer to our @ref tutorial_android_dev_intro guide. |
||||
|
||||
If you encounter any error after thoroughly following these steps, feel free to contact us via |
||||
[OpenCV4Android](https://groups.google.com/group/android-opencv/) discussion group or OpenCV [Q&A |
||||
forum](https://forum.opencv.org). We'll do our best to help you out. |
||||
|
||||
General info |
||||
------------ |
||||
|
||||
OpenCV4Android SDK package enables development of Android applications with use of OpenCV library. |
||||
|
||||
The structure of package contents looks as follows: |
||||
|
||||
OpenCV-2.4.9-android-sdk |
||||
|_ apk |
||||
| |_ OpenCV_2.4.9_binary_pack_armv7a.apk |
||||
| |_ OpenCV_2.4.9_Manager_2.18_XXX.apk |
||||
| |
||||
|_ doc |
||||
|_ samples |
||||
|_ sdk |
||||
| |_ etc |
||||
| |_ java |
||||
| |_ native |
||||
| |_ 3rdparty |
||||
| |_ jni |
||||
| |_ libs |
||||
| |_ armeabi |
||||
| |_ armeabi-v7a |
||||
| |_ x86 |
||||
| |
||||
|_ LICENSE |
||||
|_ README.android |
||||
|
||||
- `sdk` folder contains OpenCV API and libraries for Android: |
||||
- `sdk/java` folder contains an Android library Eclipse project providing OpenCV Java API that can |
||||
be imported into developer's workspace; |
||||
- `sdk/native` folder contains OpenCV C++ headers (for JNI code) and native Android libraries |
||||
(\*.so and \*.a) for ARM-v5, ARM-v7a and x86 architectures; |
||||
- `sdk/etc` folder contains Haar and LBP cascades distributed with OpenCV. |
||||
- `apk` folder contains Android packages that should be installed on the target Android device to |
||||
enable OpenCV library access via OpenCV Manager API (see details below). |
||||
|
||||
On production devices that have access to Google Play Market (and Internet) these packages will |
||||
be installed from Market on the first start of an application using OpenCV Manager API. But |
||||
devkits without Market or Internet connection require this packages to be installed manually. |
||||
Install the Manager.apk and optional binary_pack.apk if it needed. See `Manager Selection` |
||||
for details. |
||||
|
||||
@note Installation from Internet is the preferable way since OpenCV team may publish updated |
||||
versions of this packages on the Market. |
||||
|
||||
- `samples` folder contains sample applications projects |
||||
and their prebuilt packages (APK). Import them into Eclipse workspace (like described below) and |
||||
browse the code to learn possible ways of OpenCV use on Android. |
||||
|
||||
- `doc` folder contains various OpenCV documentation in PDF format. It's also available online at |
||||
<http://docs.opencv.org>. |
||||
@note The most recent docs (nightly build) are at <http://docs.opencv.org/4.x>. Generally, it's more |
||||
up-to-date, but can refer to not-yet-released functionality. |
||||
@todo I'm not sure that this is the best place to talk about OpenCV Manager |
||||
|
||||
Starting from version 2.4.3 OpenCV4Android SDK uses OpenCV Manager API for library initialization. |
||||
OpenCV Manager is an Android service based solution providing the following benefits for OpenCV |
||||
applications developers: |
||||
|
||||
- Compact apk-size, since all applications use the same binaries from Manager and do not store |
||||
native libs within themselves; |
||||
- Hardware specific optimizations are automatically enabled on all supported platforms; |
||||
- Automatic updates and bug fixes; |
||||
- Trusted OpenCV library source. All packages with OpenCV are published on Google Play; |
||||
|
||||
|
||||
Manual OpenCV4Android SDK setup |
||||
------------------------------- |
||||
|
||||
### Get the OpenCV4Android SDK |
||||
|
||||
-# Go to the [OpenCV download page on |
||||
SourceForge](http://sourceforge.net/projects/opencvlibrary/files/) and download |
||||
the latest available version. This tutorial is based on this package: [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). |
||||
-# Create a new folder for Android with OpenCV development. For this tutorial we have unpacked |
||||
OpenCV SDK to the `C:\Work\OpenCV4Android\` directory. |
||||
|
||||
@note Better to use a path without spaces in it. Otherwise you may have problems with ndk-build. |
||||
|
||||
-# Unpack the SDK archive into the chosen directory. |
||||
|
||||
You can unpack it using any popular archiver (e.g with 7-Zip): |
||||
|
||||
 |
||||
|
||||
On Unix you can use the following command: |
||||
@code{.bash} |
||||
unzip ~/Downloads/OpenCV-2.4.9-android-sdk.zip |
||||
@endcode |
||||
|
||||
### Import OpenCV library and samples to the Eclipse |
||||
|
||||
-# Start Eclipse and choose your workspace location. |
||||
|
||||
We recommend to start working with OpenCV for Android from a new clean workspace. A new Eclipse |
||||
workspace can for example be created in the folder where you have unpacked OpenCV4Android SDK |
||||
package: |
||||
|
||||
 |
||||
|
||||
-# Import OpenCV library and samples into workspace. |
||||
|
||||
OpenCV library is packed as a ready-for-use [Android Library |
||||
Project](http://developer.android.com/guide/developing/projects/index.html#LibraryProjects). You |
||||
can simply reference it in your projects. |
||||
|
||||
Each sample included into the `OpenCV-2.4.9-android-sdk.zip` is a regular Android project that |
||||
already references OpenCV library. Follow the steps below to import OpenCV and samples into the |
||||
workspace: |
||||
|
||||
- Right click on the Package Explorer window and choose Import... option from the context |
||||
menu: |
||||
|
||||
 |
||||
|
||||
- In the main panel select General --\> Existing Projects into Workspace and press Next |
||||
button: |
||||
|
||||
 |
||||
|
||||
- In the Select root directory field locate your OpenCV package folder. Eclipse should |
||||
automatically locate OpenCV library and samples: |
||||
|
||||
 |
||||
|
||||
- Click Finish button to complete the import operation. |
||||
|
||||
@note OpenCV samples are indeed **dependent** on OpenCV library project so don't forget to import it to your workspace as well. |
||||
|
||||
After clicking Finish button Eclipse will load all selected projects into workspace, and you |
||||
have to wait some time while it is building OpenCV samples. Just give a minute to Eclipse to |
||||
complete initialization. |
||||
|
||||
 |
||||
|
||||
Once Eclipse completes build you will have the clean workspace without any build errors: |
||||
|
||||
 |
||||
|
||||
@anchor tutorial_O4A_SDK_samples |
||||
### Running OpenCV Samples |
||||
|
||||
At this point you should be able to build and run the samples. Keep in mind, that face-detection and |
||||
Tutorial 2 - Mixed Processing include some native code and require Android NDK and NDK/CDT plugin |
||||
for Eclipse to build working applications. If you haven't installed these tools, see the |
||||
corresponding section of @ref tutorial_android_dev_intro. |
||||
|
||||
**warning** |
||||
|
||||
Please consider that some samples use Android Java Camera API, which is accessible with an AVD. |
||||
|
||||
@note Recent *Android SDK tools, revision 19+* can run ARM v7a OS images but they available not for |
||||
all Android versions. |
||||
|
||||
Well, running samples from Eclipse is very simple: |
||||
|
||||
- Connect your device with adb tool from Android SDK or create an emulator with camera support. |
||||
- See [Managing Virtual Devices](http://developer.android.com/guide/developing/devices/index.html) document for help |
||||
with Android Emulator. |
||||
- See [Using Hardware Devices](http://developer.android.com/guide/developing/device.html) for |
||||
help with real devices (not emulators). |
||||
|
||||
- Select project you want to start in Package Explorer and just press Ctrl + F11 or select option |
||||
Run --\> Run from the main menu, or click Run button on the toolbar. |
||||
|
||||
@note Android Emulator can take several minutes to start. So, please, be patient. \* On the first |
||||
run Eclipse will ask you about the running mode for your application: |
||||
|
||||
 |
||||
|
||||
- Select the Android Application option and click OK button. Eclipse will install and run the |
||||
sample. |
||||
|
||||
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. In this case you will see the following message: |
||||
|
||||
 |
||||
|
||||
To get rid of the message you will need to install OpenCV Manager and the appropriate |
||||
OpenCV binary pack. Simply tap Yes if you have *Google Play Market* installed on your |
||||
device/emulator. It will redirect you to the corresponding page on *Google Play Market*. |
||||
|
||||
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 `Manager Selection` for |
||||
details. |
||||
@code{.sh} |
||||
<Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk |
||||
@endcode |
||||
|
||||
@note armeabi, armv7a-neon, arm7a-neon-android8, mips and x86 stand for platform targets: |
||||
- armeabi is for ARM v5 and ARM v6 architectures with Android API 8+, |
||||
- armv7a-neon is for NEON-optimized ARM v7 with Android API 9+, |
||||
- arm7a-neon-android8 is for NEON-optimized ARM v7 with Android API 8, |
||||
- mips is for MIPS architecture with Android API 9+, |
||||
- x86 is for Intel x86 CPUs with Android API 9+. |
||||
|
||||
@note |
||||
If using hardware device for testing/debugging, run the following command to learn its CPU |
||||
architecture: |
||||
@code{.sh} |
||||
adb shell getprop ro.product.cpu.abi |
||||
@endcode |
||||
If you're using an AVD emulator, go Window \> AVD Manager to see the list of available devices. |
||||
Click Edit in the context menu of the selected device. In the window, which then pop-ups, find |
||||
the CPU field. |
||||
|
||||
@note |
||||
You may also see section `Manager Selection` for details. |
||||
|
||||
When done, you will be able to run OpenCV samples on your device/emulator seamlessly. |
||||
|
||||
- Here is Sample - image-manipulations sample, running on top of stock camera-preview of the |
||||
emulator. |
||||
|
||||
 |
||||
|
||||
What's next |
||||
----------- |
||||
|
||||
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 tutorial_dev_with_OCV_on_Android tutorial. |
@ -1,384 +1,113 @@ |
||||
Android Development with OpenCV {#tutorial_dev_with_OCV_on_Android} |
||||
=============================== |
||||
|
||||
@prev_tutorial{tutorial_O4A_SDK} |
||||
@prev_tutorial{tutorial_android_dev_intro} |
||||
@next_tutorial{tutorial_android_dnn_intro} |
||||
|
||||
| | | |
||||
| -: | :- | |
||||
| Original author | Vsevolod Glumov | |
||||
| Compatibility | OpenCV >= 3.0 | |
||||
|
||||
@warning |
||||
This tutorial is deprecated. |
||||
| Original authors | Alexander Panov, Rostislav Vasilikhin | |
||||
| Compatibility | OpenCV >= 4.9.0 | |
||||
|
||||
This tutorial has been created to help you use OpenCV library within your Android project. |
||||
|
||||
This guide was written with Windows 7 in mind, though it should work with any other OS supported by |
||||
OpenCV4Android SDK. |
||||
This guide was checked on Ubuntu but contains no platform-dependent parts, therefore should be compatible with any OS supported by Android Studio and OpenCV4Android SDK. |
||||
|
||||
This tutorial assumes you have the following installed and configured: |
||||
|
||||
- Android Studio |
||||
- JDK |
||||
- Android SDK and NDK |
||||
- Eclipse IDE |
||||
- ADT and CDT plugins for Eclipse |
||||
- OpenCV for Android SDK from official [release page on Github](https://github.com/opencv/opencv/releases) |
||||
or [SourceForge](https://sourceforge.net/projects/opencvlibrary/). Advanced: as alternative the SDK may be |
||||
built from source code by [instruction on wiki](https://github.com/opencv/opencv/wiki/Custom-OpenCV-Android-SDK-and-AAR-package-build). |
||||
|
||||
If you need help with anything of the above, you may refer to our @ref tutorial_android_dev_intro guide. |
||||
|
||||
This tutorial also assumes you have OpenCV4Android SDK already installed on your development machine |
||||
and OpenCV Manager on your testing device correspondingly. If you need help with any of these, you |
||||
may consult our @ref tutorial_O4A_SDK tutorial. |
||||
|
||||
If you encounter any error after thoroughly following these steps, feel free to contact us via |
||||
[OpenCV4Android](https://groups.google.com/group/android-opencv/) discussion group or OpenCV [Q&A |
||||
forum](https://forum.opencv.org) . We'll do our best to help you out. |
||||
|
||||
Using OpenCV Library Within Your Android Project |
||||
------------------------------------------------ |
||||
|
||||
In this section we will explain how to make some existing project to use OpenCV. Starting with 2.4.2 |
||||
release for Android, *OpenCV Manager* is used to provide apps with the best available version of |
||||
OpenCV. You can get more information here: `Android OpenCV Manager` and in these |
||||
[slides](https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p). |
||||
If you encounter any error after thoroughly following these steps, feel free to contact us via OpenCV [forum](https://forum.opencv.org). We'll do our best to help you out. |
||||
|
||||
### Java |
||||
Hello OpenCV sample |
||||
------------------- |
||||
|
||||
#### Application Development with Async Initialization |
||||
In addition to this instruction you can use some video guide, for example [this one](https://www.youtube.com/watch?v=bR7lL886-uc&ab_channel=ProgrammingHut) |
||||
|
||||
Using async initialization is a **recommended** way for application development. It uses the OpenCV |
||||
Manager to access OpenCV libraries externally installed in the target system. |
||||
1. Open Android Studio and create empty project by choosing ***Empty Views Activity*** |
||||
|
||||
-# Add OpenCV library project to your workspace. Use menu |
||||
File -\> Import -\> Existing project in your workspace. |
||||
 |
||||
|
||||
Press Browse button and locate OpenCV4Android SDK (`OpenCV-2.4.9-android-sdk/sdk`). |
||||
2. Setup the project: |
||||
- Choose ***Java*** language |
||||
- Choose ***Groovy DSL*** build configuration language |
||||
- Choose ***Minumum SDK*** to the same version number as was used during OpenCV 4 Android build |
||||
- If you don't know it, you can find it in file `OpenCV-android-sdk/sdk/build.gradle` at `android -> defaultConfig -> minSdkVersion` |
||||
|
||||
 |
||||
 |
||||
|
||||
-# In application project add a reference to the OpenCV Java SDK in |
||||
Project -\> Properties -\> Android -\> Library -\> Add select OpenCV Library - 2.4.9. |
||||
|
||||
 |
||||
3. Click ***File -> New -> Import module...*** and select OpenCV SDK path |
||||
|
||||
In most cases OpenCV Manager may be installed automatically from Google Play. For the case, when |
||||
Google Play is not available, i.e. emulator, developer board, etc, you can install it manually using |
||||
adb tool. See `Manager Selection` for details. |
||||
 |
||||
|
||||
There is a very base code snippet implementing the async initialization. It shows basic principles. |
||||
See the "15-puzzle" OpenCV sample for details. |
||||
@code{.java} |
||||
public class Sample1Java extends Activity implements CvCameraViewListener { |
||||
4. Set module name as `OpenCV` and press `Finish` |
||||
|
||||
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { |
||||
@Override |
||||
public void onManagerConnected(int status) { |
||||
switch (status) { |
||||
case LoaderCallbackInterface.SUCCESS: |
||||
{ |
||||
Log.i(TAG, "OpenCV loaded successfully"); |
||||
mOpenCvCameraView.enableView(); |
||||
} break; |
||||
default: |
||||
{ |
||||
super.onManagerConnected(status); |
||||
} break; |
||||
} |
||||
} |
||||
}; |
||||
 |
||||
|
||||
@Override |
||||
public void onResume() |
||||
{ |
||||
super.onResume(); |
||||
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback); |
||||
5. OpenCV also provides experiemental Kotlin support. Please add Android Kotlin plugin to `MyApplication/OpenCV/build.gradle` file: |
||||
@code{.gradle} |
||||
plugins { |
||||
id 'org.jetbrains.kotlin.android' version '1.7.10' #version may differ for your setup |
||||
} |
||||
|
||||
... |
||||
} |
||||
@endcode |
||||
It this case application works with OpenCV Manager in asynchronous fashion. OnManagerConnected |
||||
callback will be called in UI thread, when initialization finishes. Please note, that it is not |
||||
allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback. Load |
||||
your own native libraries that depend on OpenCV after the successful OpenCV initialization. Default |
||||
BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() |
||||
method to exit in case of initialization failure. To override this behavior you need to override |
||||
finish() method of BaseLoaderCallback class and implement your own finalization method. |
||||
|
||||
#### Application Development with Static Initialization |
||||
|
||||
According to this approach all OpenCV binaries are included into your application package. It is |
||||
designed mostly for development purposes. This approach is deprecated for the production code, |
||||
release package is recommended to communicate with OpenCV Manager via the async initialization |
||||
described above. |
||||
|
||||
-# Add the OpenCV library project to your workspace the same way as for the async initialization |
||||
above. Use menu File -\> Import -\> Existing project in your workspace, press Browse button and |
||||
select OpenCV SDK path (`OpenCV-2.4.9-android-sdk/sdk`). |
||||
|
||||
 |
||||
|
||||
-# In the application project add a reference to the OpenCV4Android SDK in |
||||
Project -\> Properties -\> Android -\> Library -\> Add select OpenCV Library - 2.4.9; |
||||
|
||||
 |
||||
|
||||
-# If your application project **doesn't have a JNI part**, just copy the corresponding OpenCV |
||||
native libs from `<OpenCV-2.4.9-android-sdk>/sdk/native/libs/<target_arch>` to your project |
||||
directory to folder `libs/<target_arch>`. |
||||
|
||||
In case of the application project **with a JNI part**, instead of manual libraries copying you |
||||
need to modify your Android.mk file: add the following two code lines after the |
||||
"include $(CLEAR_VARS)" and before |
||||
"include path_to_OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk" |
||||
@code{.make} |
||||
OPENCV_CAMERA_MODULES:=on |
||||
OPENCV_INSTALL_MODULES:=on |
||||
@endcode |
||||
The result should look like the following: |
||||
@code{.make} |
||||
include $(CLEAR_VARS) |
||||
Like this: |
||||
 |
||||
If you don't do this, you may get an error: |
||||
@code |
||||
Task failed with an exception. |
||||
----------- |
||||
* Where: |
||||
Build file '/home/alexander/AndroidStudioProjects/MyApplication/opencv/build.gradle' line: 4 |
||||
|
||||
# OpenCV |
||||
OPENCV_CAMERA_MODULES:=on |
||||
OPENCV_INSTALL_MODULES:=on |
||||
include ../../sdk/native/jni/OpenCV.mk |
||||
* What went wrong: |
||||
A problem occurred evaluating project ':opencv'. |
||||
> Plugin with id 'kotlin-android' not found. |
||||
@endcode |
||||
After that the OpenCV libraries will be copied to your application `libs` folder during the JNI |
||||
build.v |
||||
The fix was found [here](https://stackoverflow.com/questions/73225714/import-opencv-sdk-to-android-studio-chipmunk) |
||||
|
||||
Eclipse will automatically include all the libraries from the `libs` folder to the application |
||||
package (APK). |
||||
6. OpenCV project uses `aidl` and `buildConfig` features. Please enable them in |
||||
`MyApplication/OpenCV/build.gradle` file to `android` block: |
||||
|
||||
-# The last step of enabling OpenCV in your application is Java initialization code before calling |
||||
OpenCV API. It can be done, for example, in the static section of the Activity class: |
||||
@code{.java} |
||||
static { |
||||
if (!OpenCVLoader.initDebug()) { |
||||
// Handle initialization error |
||||
} |
||||
} |
||||
@endcode |
||||
If you application includes other OpenCV-dependent native libraries you should load them |
||||
**after** OpenCV initialization: |
||||
@code{.java} |
||||
static { |
||||
if (!OpenCVLoader.initDebug()) { |
||||
// Handle initialization error |
||||
} else { |
||||
System.loadLibrary("my_jni_lib1"); |
||||
System.loadLibrary("my_jni_lib2"); |
||||
} |
||||
@code{.gradle} |
||||
buildFeatures{ |
||||
aidl true |
||||
buildConfig true |
||||
} |
||||
@endcode |
||||
|
||||
### Native/C++ |
||||
|
||||
To build your own Android application, using OpenCV as native part, the following steps should be |
||||
taken: |
||||
|
||||
-# You can use an environment variable to specify the location of OpenCV package or just hardcode |
||||
absolute or relative path in the `jni/Android.mk` of your projects. |
||||
-# The file `jni/Android.mk` should be written for the current application using the common rules |
||||
for this file. |
||||
|
||||
For detailed information see the Android NDK documentation from the Android NDK archive, in the |
||||
file `<path_where_NDK_is_placed>/docs/ANDROID-MK.html`. |
||||
|
||||
-# The following line: |
||||
@code{.make} |
||||
include C:\Work\OpenCV4Android\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk |
||||
@endcode |
||||
Should be inserted into the `jni/Android.mk` file **after** this line: |
||||
@code{.make} |
||||
include $(CLEAR_VARS) |
||||
@endcode |
||||
-# Several variables can be used to customize OpenCV stuff, but you **don't need** to use them when |
||||
your application uses the async initialization via the OpenCV Manager API. |
||||
|
||||
@note These variables should be set **before** the "include .../OpenCV.mk" line: |
||||
@code{.make} |
||||
OPENCV_INSTALL_MODULES:=on |
||||
Like this: |
||||
 |
||||
If you don't do this, you may get an error: |
||||
@code |
||||
JavaCameraView.java:15: error: cannot find symbol import org.opencv.BuildConfig; ^ symbol: class BuildConfig location: package org.opencv |
||||
@endcode |
||||
The fix was found [here](https://stackoverflow.com/questions/76374886/error-cannot-find-symbol-import-org-opencv-buildconfig-android-studio) and [here](https://forum.opencv.org/t/task-compiledebugjavawithjavac-failed/13667/4) |
||||
|
||||
Copies necessary OpenCV dynamic libs to the project libs folder in order to include them |
||||
into the APK. |
||||
@code{.make} |
||||
OPENCV_CAMERA_MODULES:=off |
||||
@endcode |
||||
Skip native OpenCV camera related libs copying to the project libs folder. |
||||
@code{.make} |
||||
OPENCV_LIB_TYPE:=STATIC |
||||
@endcode |
||||
Perform static linking with OpenCV. By default dynamic link is used and the project JNI lib |
||||
depends on libopencv_java.so. |
||||
|
||||
-# The file `Application.mk` should exist and should contain lines: |
||||
@code{.make} |
||||
APP_STL := gnustl_static |
||||
APP_CPPFLAGS := -frtti -fexceptions |
||||
@endcode |
||||
Also, the line like this one: |
||||
@code{.make} |
||||
APP_ABI := armeabi-v7a |
||||
@endcode |
||||
Should specify the application target platforms. |
||||
|
||||
In some cases a linkage error (like |
||||
`"In function 'cv::toUtf16(std::basic_string<...>... undefined reference to 'mbstowcs'"`) |
||||
happens when building an application JNI library, depending on OpenCV. The following line in the |
||||
`Application.mk` usually fixes it: |
||||
@code{.make} |
||||
APP_PLATFORM := android-9 |
||||
@endcode |
||||
7. Add the module to the project: |
||||
- Click ***File -> Project structure... -> Dependencies -> All modules -> + (Add Dependency button) -> Module dependency*** |
||||
- Choose `app` |
||||
- Select `OpenCV` |
||||
|
||||
-# Either use @ref tutorial_android_dev_intro_ndk "manual" ndk-build invocation or |
||||
@ref tutorial_android_dev_intro_eclipse "setup Eclipse CDT Builder" to build native JNI lib |
||||
before (re)building the Java part and creating |
||||
an APK. |
||||
|
||||
Hello OpenCV Sample |
||||
------------------- |
||||
|
||||
Here are basic steps to guide you through the process of creating a simple OpenCV-centric |
||||
application. It will be capable of accessing camera output, processing it and displaying the result. |
||||
|
||||
-# Open Eclipse IDE, create a new clean workspace, create a new Android project |
||||
File --\> New --\> Android Project |
||||
-# Set name, target, package and minSDKVersion accordingly. The minimal SDK version for build with |
||||
OpenCV4Android SDK is 11. Minimal device API Level (for application manifest) is 8. |
||||
-# Allow Eclipse to create default activity. Lets name the activity HelloOpenCvActivity. |
||||
-# Choose Blank Activity with full screen layout. Lets name the layout HelloOpenCvLayout. |
||||
-# Import OpenCV library project to your workspace. |
||||
-# Reference OpenCV library within your project properties. |
||||
|
||||
 |
||||
|
||||
-# Edit your layout file as xml file and pass the following layout there: |
||||
@code{.xml} |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
xmlns:opencv="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" > |
||||
|
||||
<org.opencv.android.JavaCameraView |
||||
android:layout_width="fill_parent" |
||||
android:layout_height="fill_parent" |
||||
android:visibility="gone" |
||||
android:id="@+id/HelloOpenCvView" |
||||
opencv:show_fps="true" |
||||
opencv:camera_id="any" /> |
||||
|
||||
</LinearLayout> |
||||
@endcode |
||||
-# Add the following permissions to the `AndroidManifest.xml` file: |
||||
@code{.xml} |
||||
</application> |
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/> |
||||
|
||||
<uses-feature android:name="android.hardware.camera" android:required="false"/> |
||||
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> |
||||
<uses-feature android:name="android.hardware.camera.front" android:required="false"/> |
||||
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/> |
||||
@endcode |
||||
-# Set application theme in AndroidManifest.xml to hide title and system buttons. |
||||
@code{.xml} |
||||
<application |
||||
android:icon="@drawable/icon" |
||||
android:label="@string/app_name" |
||||
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > |
||||
@endcode |
||||
-# Add OpenCV library initialization to your activity. Fix errors by adding required imports. |
||||
@code{.java} |
||||
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { |
||||
@Override |
||||
public void onManagerConnected(int status) { |
||||
switch (status) { |
||||
case LoaderCallbackInterface.SUCCESS: |
||||
{ |
||||
Log.i(TAG, "OpenCV loaded successfully"); |
||||
mOpenCvCameraView.enableView(); |
||||
} break; |
||||
default: |
||||
{ |
||||
super.onManagerConnected(status); |
||||
} break; |
||||
} |
||||
} |
||||
}; |
||||
|
||||
@Override |
||||
public void onResume() |
||||
{ |
||||
super.onResume(); |
||||
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback); |
||||
} |
||||
@endcode |
||||
-# Defines that your activity implements CvCameraViewListener2 interface and fix activity related |
||||
errors by defining missed methods. For this activity define onCreate, onDestroy and onPause and |
||||
implement them according to the code snippet below. Fix errors by adding required imports. |
||||
@code{.java} |
||||
private CameraBridgeViewBase mOpenCvCameraView; |
||||
|
||||
@Override |
||||
public void onCreate(Bundle savedInstanceState) { |
||||
Log.i(TAG, "called onCreate"); |
||||
super.onCreate(savedInstanceState); |
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
||||
setContentView(R.layout.HelloOpenCvLayout); |
||||
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView); |
||||
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); |
||||
mOpenCvCameraView.setCvCameraViewListener(this); |
||||
} |
||||
|
||||
@Override |
||||
public void onPause() |
||||
{ |
||||
super.onPause(); |
||||
if (mOpenCvCameraView != null) |
||||
mOpenCvCameraView.disableView(); |
||||
} |
||||
|
||||
public void onDestroy() { |
||||
super.onDestroy(); |
||||
if (mOpenCvCameraView != null) |
||||
mOpenCvCameraView.disableView(); |
||||
} |
||||
|
||||
public void onCameraViewStarted(int width, int height) { |
||||
} |
||||
|
||||
public void onCameraViewStopped() { |
||||
} |
||||
|
||||
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { |
||||
return inputFrame.rgba(); |
||||
} |
||||
@endcode |
||||
-# Run your application on device or emulator. |
||||
 |
||||
|
||||
Lets discuss some most important steps. Every Android application with UI must implement Activity |
||||
and View. By the first steps we create blank activity and default view layout. The simplest |
||||
OpenCV-centric application must implement OpenCV initialization, create its own view to show preview |
||||
from camera and implements CvCameraViewListener2 interface to get frames from camera and process it. |
||||
 |
||||
|
||||
First of all we create our application view using xml layout. Our layout consists of the only one |
||||
full screen component of class org.opencv.android.JavaCameraView. This class is implemented inside |
||||
OpenCV library. It is inherited from CameraBridgeViewBase, that extends SurfaceView and uses |
||||
standard Android camera API. |
||||
 |
||||
|
||||
After creating layout we need to implement Activity class. OpenCV initialization process has been |
||||
already discussed above. In this sample we use asynchronous initialization. Implementation of |
||||
CvCameraViewListener interface allows you to add processing steps after frame grabbing from camera |
||||
and before its rendering on screen. The most important function is onCameraFrame. It is callback |
||||
function and it is called on retrieving frame from camera. The callback input is object of |
||||
CvCameraViewFrame class that represents frame from camera. |
||||
8. Before using any OpenCV function you have to load the library first. If you application includes other OpenCV-dependent native libraries you should load them ***after*** OpenCV initialization. |
||||
Library is loaded at app start: |
||||
@snippet samples/android/tutorial-1-camerapreview/src/org/opencv/samples/tutorial1/Tutorial1Activity.java ocv_loader_init |
||||
Like this: |
||||
 |
||||
|
||||
@note Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object does |
||||
not have its own state and its behavior out of callback is unpredictable! |
||||
9. Choose a device to check the sample on and run the code by pressing `run` button |
||||
|
||||
It has rgba() and gray() |
||||
methods that allows to get frame as RGBA and one channel gray scale Mat respectively. It expects |
||||
that onCameraFrame function returns RGBA frame that will be drawn on the screen. |
||||
 |
||||
|
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 243 KiB |
After Width: | Height: | Size: 181 KiB |
After Width: | Height: | Size: 235 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 49 KiB |
After Width: | Height: | Size: 106 KiB |
Before Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 205 KiB |
Before Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 33 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 9.2 KiB |
Before Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 77 KiB |
Before Width: | Height: | Size: 94 KiB |
Before Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 7.0 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 33 KiB |
Before Width: | Height: | Size: 51 KiB |
Before Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 95 KiB |
Before Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 16 KiB |
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 32 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 23 KiB |
Before Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 232 KiB |
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 103 KiB |
Before Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 141 KiB |
After Width: | Height: | Size: 131 KiB |
After Width: | Height: | Size: 217 KiB |
After Width: | Height: | Size: 294 KiB |
Before Width: | Height: | Size: 28 KiB |