Skip to content

Instantly share code, notes, and snippets.

@dkurt
Last active August 29, 2019 14:08
Show Gist options
  • Save dkurt/d8dee79eaa8a0275f7cf037d7bc9b5ac to your computer and use it in GitHub Desktop.
Save dkurt/d8dee79eaa8a0275f7cf037d7bc9b5ac to your computer and use it in GitHub Desktop.
  1. Download Android Studio

  2. Download Android NDK and SDK (as far as I remember SDK comes with Android Studio or might be installed from it's manager).

  3. Clone master branch of OpenCV. Create a /path/to/opencv/build folder and run cmake with the following flags. You may emit BUILD_LIST flag to build all the modules.

export ANDROID_NDK=/home/dkurtaev/Downloads/android-ndk-r14b
export ANDROID_SDK=/home/dkurtaev/Android/Sdk/
export ANDROID_HOME=/home/dkurtaev/Android/Sdk/

cmake \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_ANDROID_EXAMPLES=OFF \
  -DBUILD_TESTS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DBUILD_LIST=dnn,java,imgcodecs .. && make -j4
  1. Create a new Android Studio project. File -> New -> New Project

https://drive.google.com/open?id=1fbhrHuJ3YdfOuBbVL-hKQDQcitHZ3ZJg

  1. Add OpenCV module: File -> New -> Import module. Choose a path to /path/to/opencv/build/opencv_android.
  2. Go to File -> Project Structure. Add OpenCV module depedency.

https://drive.google.com/open?id=1f9xgsDWc-6p5i-7fr0pWUlFaP8clWEW-

  1. Try to build a project. If you faced a errror with message such "Suggestion: use a compatible library with a minSdk of at most 15", open "Gradle Scripts" -> "build.gradle (Module:opencv)" and replace "minSdkVersion 21" to "minSdkVersion 15"

  2. Let's create a "Hello world!" application which displays frames from camera:

  3. Modify app/manifests/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="org.opencv.samples.testopencv">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">  <!--Full screen mode-->

        <activity android:name=".MainActivity"
                  android:screenOrientation="landscape">  <!--Screen orientation-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

    <!--Allow to use a camera-->
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

</manifest>

From the default version, here are added camera permissions, landscape screen orientation and full screen mode.

  1. Modify res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.opencv.samples.testopencv.MainActivity">

    <org.opencv.android.JavaCameraView
        android:id="@+id/CameraView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" />

</android.support.constraint.ConstraintLayout>
  1. Modify app/java/org.opencv.samples.testopencv/MainActivity.java:
package org.opencv.samples.testopencv;

import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.opencv.core.Mat;

public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 {

  @Override
  public void onResume() {
    super.onResume();
    System.loadLibrary("opencv_java4");
    mOpenCvCameraView.enableView();
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Set up camera listener.
    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.CameraView);
    mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
  }

  @Override
  public void onCameraViewStarted(int width, int height) {}

  @Override
  public void onCameraViewStopped() {}

  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat frame = inputFrame.rgba();
    return frame;
  }

  private CameraBridgeViewBase mOpenCvCameraView;
}
  1. Create a folder /path/to/AndroidStudioProjects/TestOpenCV/app/src/main/jniLibs. Copy /path/to/opencv/build/jni/* to it (contains armeabi-v7a/libopencv_java4.so).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment