Beginning Android OpenCV
Many people who are trying to search for Android Opencv stumble upon my blog due to the name Paranoid Android. I had decided that I would never make an android appliaction as I hate Java, but desperate times require desparate measures, and so I installed Android OpenCV and created a small application.
How To Setup Android OpenCV Development Kit
Follow this link and do exactly as it says. It might be bit confusing, but that’s the right way to do it and it works.
Once you have set up everything and tested one of the OpenCV example applications, you should start scrolling down.
Writing Basic Application
Once you create a new android project, you need to add OpenCV 2.4.4 library in its properties and set NDK path and stuff which is usual. Now, I’ll demonstarte how to make one applicaiton in which OpenCV uses the phone camera, captures the image and shows a gray image on the screen.
In your MainActivity.java
, you need to add some imports, which I guess, you’ll figure it out using Eclipse.
Since we are using OpenCV’s camera functionality, we need to use CvCameraViewListener2. So before calling any methods or callback, we would like to define our variables that we are going to use.
Now, we’d like to add callback method which would start the Android Application and show display on the screen.
Log.i basically helps in debugging as it shows messages in the emulator Log Cat.
Instantiate Main Activity.
Now, we need to configure how the application will behave when it’s started, paused and resumed.
You might be wondering why there is R.id.tutorial1_activity_java_surface_view
. The short answer is I am lazy and just copied it.
Now, we need to implement camera methods in order to use it.
onCameraViewStarted()
, onCameraViewStopped()
and onCameraFrame()
must be implemented in order to create a working application. (Anyway, it will give errors if you don’t add all three methods).
So your MainActivity.java
is done. Now, you need to add tutorial1_surface_view
layout. You can add it in Main_surface_view
also which is by default, but remember to change tutorial1_surface_view
to Main_surface_view
in the code. Create a file named tutorial1_surface_view.xml in your project’s /res/layout
folder.
Copy the following xml in it.
You also need to add some permissions in AndroidManifest.xml Copy and change necessary information. Get the file here.
Even after doing this, you might face small problems, so don’t worry, you will be able to debug them. One of them would be regarding a logo in /res/drwable
folder. You need to add an image there with filename icon.png
or whatever you prefer, just make sure that you do necessary changes in corresponding files.
Take out your Android phone and plug it in USB debugging mode. Load the application and have fun.
P.S. Made an android application that generates disparity map using two images. Here’s a video you can check out. I have improved the disparity map quality a lot.
Playing around with Android UI
Articles focusing on Android UI - playing around with ViewPagers, CoordinatorLayout, meaningful motions and animations, implementing difficult customized views, etc.