Android + OpenCV = Confusion…
This is another post that I found sitting in my drafts folder… It was written by my wife while she was doing her Computer Science MSc about 18 months ago. I expect that most of what she says is still correct, but things may have changed since then. Also, please don’t comment asking questions about Android and OpenCV – I have no experience with it, and my wife isn’t writing for Android these days.
Hello, I’m Olivia, Robin’s wife, and I thought I’d write a guest post for this blog about how to get Android and OpenCV playing nicely together.
Having just started writing for Android, I was astonished at the amount of separate tutorials I had to follow just to get started. It didn’t help that:
- I had never programmed for Android
- I had never programmed in Java (yes, I know)
- I had never used a complex code building IDE
- I needed to get OpenCV working with this
Needless to say I was a little flummoxed by the sheer amount of things that I had to learn. The fact that methods can only have one return type still bewilders me (coming from a Python background, where methods can easily return loads of different objects, of all sorts of different types).
So for those of you who are in a similar situation I thought I’d provide a quick start guide which joins together the tutorials I found the best to create your first Android app using OpenCV.
First things first, you will need to have the following downloaded:
- Android Studio (when installing pretty much tick everything, just to make sure!)
- OpenCV for Android
First start by following Google’s first Android tutorial Building Your First App through to Starting Another Activity (if you get to Supporting different devices then you’ve gone too far). This takes you through setting up a project, the files you will need to edit, setting up an emulator (or using a actual Android device), running the app, creating a user interface, and possibly most importantly Android ‘Activities’. It also gives a brilliant introduction in the sidebars into the concepts involved. If you just want to get things working with OpenCV the only parts you need to follow is up to Running Your Application.
To get OpenCV working within Android Studio you need to have unzipped the download. Then follow the instructions from this blog post, using whichever OpenCV version you have. This should get everything set up properly, but now we need to check everything has worked and that our app compiles.
Add these import statements at the top of your activity file:
import org.opencv.core.CvType; import org.opencv.core.Mat;
You should then add this code within the Activity class before any other methods.
static { // If you use opencv 2.4, System.loadLibrary("opencv_java") System.loadLibrary("opencv_java3"); }
Then add the following into the onCreate
method at the bottom:
Mat test = new Mat(200, 200, CvType.CV_8UC1); Imgproc.equalizeHist(test, test);
(the example code above was taken from here).
If you now try and run this, it should work, if it doesn’t it may suggest that OpenCV isn’t properly loaded. If this doesn’t work, then the comments on this blog post are fairly comprehensive – your problem has probably already been solved!
If you understand all of this and just want to have a ready made ‘project template’ to use all of this then I have put one together at https://github.com/oew1v07/AndroidOpenCVTemplate where all the libraries are already in the required folders. To get this working follow these steps:
- Clone the repository using
git clone https://github.com/oew1v07/AndroidOpenCVTemplate.git
- Open Android Studio and choose Open an existing Android Studio project
- Navigate to the cloned folder and click Open.
- Using the Project button on the left side navigate to
Gradle Scripts/build.gradle
(Module: app)
. The settings here are not necessarily the ones that you will want to use. The following example from here shows in blue the areas you might want to change.compileSdkVersion
is the Android version you wish to compile forbuildToolsVersion
are the build tools version you want to useminSdkVersion
is the minimum Android version you want the app to run undertargetSdkVersion
is the version that will be the mostly commonly used.applicationId
should reflect the name of your app. However this is very difficult to change after the initial setting up. The one in the cloned repository iscom.example.name.myapplication
. Should you wish to change this there is a list of places you would need to edit, in a list at the bottom of this article.
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.haidermushtaq.bullincarnadine.piclone" minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:22.1.1' }
- In the file
local.properties
you need to changesdk.dir
to the directory where the Android SDK is installed on your computer. - Once this is finished you should be able to run it with no problems, and it should display a text message saying “Elmer is the best Elephant of them all!” (for an explanation of this message, see here)
Hopefully this was helpful to other people in a similar situation to me – and brought all of the various tutorials together.
List of places to change app name
These files and folders can be navigated from the cloned repository.
Where | What to change | What to change it to |
app/build.gradle |
com.example.name.myapplication | Whatever you want this to be called. Generally something along the lines of com.example.maker.appname |
app/src/androidTest/java/com/example/ |
The folder ‘name’ | maker |
app/src/androidTest/java/com/example/name |
The folder ‘myapplication’ | appname |
app/src/androidTest/java/com/example/name/myapplication/ApplicationTest.java |
package com.example.name.myapplication | package com.example.maker.appname |
app/src/main/AndroidManifest.xml |
package com.example.name.myapplication | package com.example.maker.appname |
app/src/main/java/com/example/ |
The folder ‘name’ | maker |
app/src/main/java/com/example/name |
The folder ‘myapplication’ | appname |
app/src/main/java/com/example/name/myapplication/MyActivity.java |
package com.example.name.myapplication; | package com.example.maker.appname |
app/src/main/res/layout/activity_display_message.xml |
tools:context=”com.example.name.myapplication.DisplayMessageActivity” | tools:context=”com.example.maker.appname.DisplayMessageActivity” |
app/src/main/res/layout/activity_my.xml |
tools:context=”com.example.name.myapplication.MyActivity” | tools:context=”com.example.maker.appname.MyActivity” |
app/src/main/res/layout/content_display_message.xml |
tools:context=”com.example.name.myapplication.DisplayMessageActivity” | tools:context=”com.example.maker.appname.DisplayMessageActivity” |
app/src/main/res/menu/menu_my.xml |
tools:context=”com.example.name.myapplication.MyActivity” | tools:context=”com.example.maker.appname.MyActivity” |
app/src/main/res/values/strings.xml |
If you want to change the name of the app (when on the phone). Change <string name=”app_name”>My App</string> | Whatever you want to call it |
app/src/test/java/com/example/ |
The folder ‘name’ | maker |
app/src/test/java/com/example/name |
The folder ‘myapplication’ | appname |
app/src/test/java/com/example/name/myapplication/ExampleUnitTest.java |
package com.example.name.myapplication; | package com.example.maker.appname |
If you found this post useful, please consider buying me a coffee.
This post originally appeared on Robin's Blog.
Categorised as: Programming
Leave a Reply