Sunday, July 21, 2013

OpenCV in Android - Quick install guide

I found a good explanation of OpenCV in Android and the inner workings of calling this library... I feel it is a must read before you start. Will save you work/time.

Then we go into the tutorial. Top level, check this first. Then you need to download/add the OpenCV for Android library. Then create a new workspace, this library should be one of the projects on the workspace, and any OpenCV Android project will refer to that project. When you run it, your apk gets loaded and a Manager (that gets downloaded the first time, if you don't have it in your phone), loads the library apk.

Still trying to wrap my brain around this, but here are the details of how I did till I got to run one of the sample demos (puzzle15). Follow the tutorial and if that doesn't work, read below to see my experience...

We have stuff installed from the past (like Eclipse, Basic4Android, java...). So, we need to figure out how to make it work without reinstalling or creating a parallel copy of all this (maybe only alternative).

 Sun JDK 6 or higher:
  1. I seem to have 7 (jdk1.7.0_03). JDK stands for Java Development Kit. Check in Program Files/Java...
  2. I also have JRE6 and JRE7. JRE stands for Java Runtime Environment, which contains  everything required to run Java applications on your system. Do I really need both?

Android SDK:

This is nicely documented by the Android folks. Just look around, as I guess pages may change, but I found this and this.
  1. Got an old installation (about a year ago) that I have been leveraging with Basic4Android. So, I launched my Android SDK Manager and selected the updates. The screen shows Android 4.2.2 (API17) empty when I had Android 4.1 (API16), indicating possible updates. I clicked the updates (not a new install) and the Manager suggested other dependencies I needed to get...
  2. Nevertheless, I think I still should install the new API17 (the updates just update the old API16). This is what the SDK Manager looks like:
  3. To develop in Eclipse, I need the ADT plug-in. Follow the instructions there, but remember that if you want to develop in Android with C++ (which is likely for any kind of intensive processing), you should get also the NDK. This is how my Eclipse - Help - About Eclipse looks like...
  4. Get the NDK. Follow links... Installation is just to unzip anywhere...
  5. At this moment, things seem to look very complicated (maybe because of the NDK), so, we go build a simple first app in Android, nothing to do with OpenCV. Just to make sure all the above installation is working... If you do not need this, jump to #8.
  6. While doing this, Eclipse didn't detect the HTC One at launch. Also, if you run the adb (Android Debug Bridge, go to command prompt and type adb devices) nothing shows up. In the Windows Device Manager the driver shows nicely, but it is NOT the one we need. Found a solution here. You should see at the TOP of the device manager "Android USB devices". If not, keep uninstalling driver and installing the right one till it does show up like that. What is the right one it is tough to say. Initially it worked when I got the USB Composite Device. But lately works with "My HTC". Key seems to be to have an "Android USB devices" at the top of the list. Now the adb  will see it... Restart Eclipse and now the launch should show the phone! Note, during this whole thing, I uninstalled HTC sync (but I don't think it had nothing to do with it). Other things that I am not sure they can work are here and here. A good summary of things to try is actually on the original tutorial we are following.
  7. Even after knowing the app launches, you may want to finish the tutorial. It is very useful and a nicely explained to quickly get a background on Android app development, if you didn't have one... Just as a quick note, it uses a intent to create the screen with the result of an app, so, that when you click the "back" key (which I think that is what he calls "up") it can go back to the app.
  8. Then we take this further by trying to use the NDK. We will go for the Hello-jni example which works right away. Note: JNI stands for Java Native Interface and it is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly. To understand what is all this NDK about, look  in the NDK folder for a "docs" folder, and open the file OVERVIEW.html. Nice explanation.
  9. We do also the 2nd example, a fully native app. I.e., no java involved, only the .C with the NDK. Notice how the src file is empty.
     Notice that the original sample source only had the C stuff in jni folder and the res folder.
    Most of the final stuff (in "gen", etc...) is generated by Eclipse when creating the project from an existing source. Before running NDK... The ndk-build will create the binary of our native libraries and place them in the right places for Eclipse to use them...
  10. Anyhow, back to our OpenCV installation tutorial!! The above NDK tutorial shows how to include native code in your app. But you can build the native part of your application from within Eclipse. That is where this tutorial continues...
  11. It is a bit confusing, though, in some way... Maybe I missed it, but so far, we have not downloaded the OpenCV for Android SDK! Follow these instructions... Note: there are Android OpenCV samples on the original OpenCV package (not the specific one for Android but the one I was using in my previous examples), under "samples\android", but the tutorial can't assume that...
  12. The tutorial then goes into importing those samples. Either of the two tutorials (Introduction to Android Development or OpenCV Android SDK) does this, but it is not clear if we need to add the C Nature to the samples that don't even have a jni. I am not 100% it will even allow you to do it... but seems to ask for that.
  13. So, simply do the import to the point we see the projects in the workspace but nothing else. And we try to load puzzle15. Lots of errors on the package from OpenCV. Left click on the error and one of the solutions (all the way to the bottom) sounds like "fix project setup" or something like that. It will suggest to add OpenCV library - 2.4.6 to the build path. Notice that it is on the same workspace. These errors go away:
  14. Nevertheless, I was still having lots of errors, some of them on the OpenCV library project. As the other projects link to this one, they didn't run. So, after running the SDK manager and updating everything (which was just some minor stuff as I recently installed everything), I deleted the projects (not from the drive) and run again the import. Now everything looks clean... Don't ask me why...
  15. Still, when I launch the project, I get an error: "Could not find OpenCV-2.3.1.apk!". This was solved following this.
  16. So, now, when you launch it, the installer realizes that is missing a package and gives you the following notice in the phone "Package not found. Do you want to install it?" Answer yes and that launches Google play and looks for OpenCV Manager. Install it as any other app... The Manager will get the right openCV apk for your hardware.
  17. Now your app should run!!
In the next post we will create our own app...

PS.: Click here to see the index of these series of posts on OpenCV

1 comment:

  1. This comment has been removed by a blog administrator.