Android-OpenCV


Source link: https://github.com/ahasbini/Android-OpenCV

Android-OpenCV

An Android Studio project which has a module that contains OpenCV SDK files ported and configured to use CMake and Gradle plugin 2.3.1 or above, making it easy to include OpenCV into Android applications.

Integration

Currently the doc contains details of integrating the opencv module into an exiting git repo of an Android Studio project using git submodule.

Note: if your project is not versioned by git, it will not work. You could enable git by running the following command in the project root directory, but careful not do so if your project is versioned by other VCS tools:

git init 

Sooner or later I'll include steps to include the opencv module without using git submodule and for projects that are not versioned by git. It is better to understand how git submodule works via this link: Git - Submodules. For team managed git repositories, read the section Cloning a Project with Submodules carefully.

Within the project directory, open bash/cmd and run the following command:

git submodule add https://github.com/ahasbini/Android-OpenCV.git android-opencv 

Then within the project directory, edit settings.gradle file and append the following and do Gradle Sync :

include ':android-opencv:opencv' 

Within the Project View in Android Studio, right click and choose "Configure project subset" and check opencv. Then do a Gradle Sync Gradle Sync . You should see opencv as a module as the image below:

Then within the app module directory (or application module that you're developing), create or alter the CMakeLists.txt file and add the following lines (Note that if you you are not developing a native library, you can skip this step):

set(OpenCV_DIR "../android-opencv/opencv/src/sdk/native/jni") find_package(OpenCV REQUIRED) message(STATUS "OpenCV libraries: ${
OpenCV_LIBS
}
") target_link_libraries(YOUR_TARGET_LIB ${
OpenCV_LIBS
}
) 

Then within the app module directory (or application module that you're developing), alter it's build.gradle file as such (Note that if you you are not developing a native library, only the compile line is needed):

apply plugin: 'com.android.application' ... android {

  ...
  defaultConfig {

...

externalNativeBuild {

 cmake {

  cppFlags "-frtti -fexceptions"

  //Choose the abi architectures that you would like to compile to

  abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'

 
}

}

  
}

  buildTypes {

...
  
}

  externalNativeBuild {

cmake {

 path "CMakeLists.txt"

}

  
}
 
}
  dependencies {

  ...
  compile project(':android-opencv:opencv') 
}
 

After final step, OpenCV would be included within the application and their APIs could be accessed from within the Java and Native C/C++ code. To check if OpenCV is working properly, add the following in an Application, Activity or any class to load OpenCV (make sure that the class will be accessed at runtime):

public class MyClass {

static {

if (BuildConfig.DEBUG) {

 OpenCVLoader.initDebug();

}

  
}

... 
}
 

Then build and launch the application. Once the class is loaded, the logcat will display OpenCV messages as below (the first error is normal):

10-21 16:53:28.399 D/OpenCV/StaticHelper: Trying to get library list 10-21 16:53:28.419 E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV 10-21 16:53:28.419 D/OpenCV/StaticHelper: Library list: "" 10-21 16:53:28.419 D/OpenCV/StaticHelper: First attempt to load libs 10-21 16:53:28.419 D/OpenCV/StaticHelper: Trying to init OpenCV libs 10-21 16:53:28.420 D/OpenCV/StaticHelper: Trying to load library opencv_java3 10-21 16:53:28.428 I/OpenCV: calling android_getCpuFeatures() ... 10-21 16:53:28.430 I/OpenCV: calling android_getCpuFeatures() ... Done (1f7ff) 10-21 16:53:28.441 D/OpenCV/StaticHelper: Library opencv_java3 loaded 10-21 16:53:28.441 D/OpenCV/StaticHelper: First attempt to load libs is OK 10-21 16:53:28.444 I/OpenCV/StaticHelper: General configuration for OpenCV 3.3.0 ===================================== 10-21 16:53:28.444 I/OpenCV/StaticHelper:
Version control:

3.3.0 10-21 16:53:28.444 I/OpenCV/StaticHelper:
Platform: 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  Timestamp:

 2017-08-04T00:30:10Z 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  Host:

Linux 4.8.0-58-generic x86_64 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  Target:

 Linux 1 armv7-a 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  CMake:

  2.8.12.2 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  CMake generator:

 Ninja 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  CMake build tool:

/usr/bin/ninja 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  Configuration:

Release 10-21 16:53:28.444 I/OpenCV/StaticHelper:
CPU/HW features: 10-21 16:53:28.444 I/OpenCV/StaticHelper:
  Baseline:

  NEON NEON 10-21 16:53:28.444 I/OpenCV/StaticHelper:

 requested:

  DETECT 10-21 16:53:28.444 I/OpenCV/StaticHelper:

 required:

NEON 10-21 16:53:28.444 I/OpenCV/StaticHelper:

 disabled:

VFPV3 10-21 16:53:28.444 I/OpenCV/StaticHelper:
C/C++: 10-21 16:53:28.445 I/OpenCV/StaticHelper:
  Built as dynamic libs?:

NO 10-21 16:53:28.445 I/OpenCV/StaticHelper:
  C++ Compiler:

 /usr/bin/ccache /opt/android/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ (ver 4.8) 

Also, you could inspect the built apk under app_module_directory/build/outputs to see that the libopenc_java3.so is included within the abi architecture folders (drag the apk onto the text editor tabs of Android Studio):

Updating OpenCV

I'll be monitoring OpenCV's SDK and releases on their website in order to re-port it with their updated files. Please post an issue if I haven't done so within a day or two.

In order to update the OpenCV module to the latest version simply open a bash/cmd in project_root_directory/android-opencv/ and run the command:

git pull 

Once the command finishes, the opencv module will be updated to the latest commit on the repo. Do a Gradle Sync , refresh linked C++ projects ( Build > Refresh Linked C++ Projects) and compile to make sure the update was successful.

Checkout to a certain version of OpenCV

With each release of OpenCV SDK a tag with the OpenCV version will be created on the commit of the release. To checkout to a certain version of the OpenCV library, open a bash/cmd in project_root_directory/android-opencv/ and run the command:

git pull git checkout version-number 

Where version-number is the version of OpenCV you'd like to checkout to (example: 3.2.0). The minimum version of the OpenCV within the repo is 3.2.0.

Don't forget to do a Gradle Sync , refresh linked C++ projects ( Build > Refresh Linked C++ Projects) and compile to make sure the checkout was successful.

Resources

This is a dialog utility library. It provides a easy way to let developers deal with screen rotation issue.

An agnostic Json abstraction to perform data binding operations for Android and Java.

HoldToLoadLayout is a view group that can contain a single child. It draws your child to middle of layout, and performs loading wheel around it with settings you determined.

A perfect pixel button.

CustomFontLib is an Android Library to help adding custom fonts to Android Views.

CircleCheckBox is an Android custom view, that simply animates the check.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes