Saturday, July 23, 2016

How to Compile CameraPreview OpenCV Tutorial App on Android Studio

First, download the latest OpenCV for Android from here. At the time of writing this post, the latest version is OpenCV 3.1.0. I will assume that you have downloaded the file in ~/Downloads folder.

Unzip the contents by running
$ unzip OpenCV-3.1.0-android-sdk.zip

Next, open up Android Studio, and select Import project (Eclipse ADT, Gradle, etc.) and choose ~/Downloads/OpenCV-android-sdk/samples/tutorial-1-camerapreview

Choose the import destination directory as desired. I will assume it to be ~/AndroidStudioProjects/tutorial-1-camerapreview

Check all three boxes and click on finish.

Now, you should be prompted with an auto-generated Eclipse Android Project Import Summary. In the messages box below, you may probably see
Error:Cause: failed to find target with hash string 'android-14' in: /data/Android/Sdk
Install missing platform(s) and sync project

To fix this, open up build.gradle files for both the openCVLibrary module and openCVTutorial module. Make sure to change compile and target SDK versions appropriately. I will use API 23 and Build Tools 23.0.3 throughout this post, but your versions may differ from me. For example, gradle.build file for the openCVLibrary should read

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

and openCVTutorial module's build.gradle should read

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial1"
        minSdkVersion 8
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':openCVLibrary310')
}

Now, you should be able to successfully compile the project. Try running the app on your device or emulator. You will probably be encountered with Package Not Found message, unless you already have OpenCV Manager installed on your device. You should be able to successfully run the app after installing OpenCV Manager!

In case you do not want to require OpenCV Manager on the device, it is possible to link the OpenCV library as a static library. To do this, you will first need to copy the native library folder to the project directory and name it as jniLibs:
$ cp -r ~/Downloads/OpenCV-android-sdk/sdk/native/libs ~/AndroidStudioProjects/tutorial-1-camerapreview/openCVTutorial1CameraPreview/src/main/jniLibs

Next, you will need to insert static initialization code into the main activity java file at ~/AndroidStudioProjects/tutorial-1-camerapreview/openCVTutorial1CameraPreview/src/main/java/org/opencv/samples/tutorial1/Tutorial1Activity.java:

...
public class Tutorial1Activity extends Activity implements CvCameraViewListener2 {
    static {
        if (!OpenCVLoader.initDebug()) {
            Log.v("OpenCV", "ERROR: OpenCV Library Load Failed");
        } else {
            Log.v("OpenCV", "OpenCV Library Load Successful");
        }
    }
...

where 7 lines starting from static have been inserted.

You should now be able to compile and run the app without OpenCV Manager app on the device. Sometimes, you need to clean build and rebuild the project, or select File -> Invalidate Caches / Restart option to get it take effect. If it still says it requires OpenCV Manager, make sure to delete the original app from the device, rebuild the app, and re-run the app.

NOTE - if you are compiling for Android API 23 or above, you will need to set permission for the camera. Follow the instructions below:

1. Add a dependency in openCVTutorial module's build.gradle file to read
...
dependencies {
    compile project(':openCVLibrary310')
    compile 'com.android.support:appcompat-v7:23.0.0'
}
...

Make sure to insert the appropriate repository version in place of v7:23.0.0; Else, you could always install one.

2. Let the main activity extend AppCompatActivity and add permission request code in the main java file:
...
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
...
public class Tutorial1Activity extends AppCompatActivity implements CvCameraViewListener2 {
...
    public void onCreate(Bundle savedInstanceState) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA},
                0);
...

This is the bare minimum code to access camera for API 23 and above. For more details and practical coding, please refer to Android's official document.

3. In openCVTutorial module's AndroidManifest.xml file, replace android:theme="@android:style/Theme.NoTitleBar.Fullscreen" with android:theme="@style/Theme.AppCompat.Light".

Now, you should be able to run the app without OpenCV Manager installed! If you are running on the emulator, make sure to go to the advanced option to emulate back camera.

No comments:

Post a Comment