Skip to content

Instantly share code, notes, and snippets.

@dkurt
Last active April 9, 2023 00:02
Show Gist options
  • Save dkurt/f4a84cb954e8da4d8de1201d56e226f5 to your computer and use it in GitHub Desktop.
Save dkurt/f4a84cb954e8da4d8de1201d56e226f5 to your computer and use it in GitHub Desktop.
OpenCV for Android Studio

This page contains a guide of how to add OpenCV into Android Studio Project

Tested with

  • OpenCV 4.3.0
  • Android Studio 3.6.3

Steps

image

  • Try to run default application

device-2020-05-06-214108

Add OpenCV dependency

  • File -> New -> ImportModule

Specify a path to unpacked SDK: C:\Users\dkurtaev\Downloads\opencv-4.3.0-android-sdk\OpenCV-android-sdk\sdk

image

  • File -> Project Structure

image

image

  • Replace "minSdkVersion 21" to "minSdkVersion 15" in Gradle Scripts -> build.gradle (Module: opencv)

image

  • Try to build a project. There should not be any error.

Simple application

  • manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <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="fullSensor">  <!--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>
  • res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/activity_frame_layout"
        tools:context="com.example.myapplication.MainActivity">
    <org.opencv.android.JavaCameraView
        android:id="@+id/CameraView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" />
</FrameLayout>
  • java/com/example/myapplication/MainActivity.java
package com.example.myapplication;

import android.os.Bundle;
import android.view.SurfaceView;

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

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import java.util.Collections;
import java.util.List;

public class MainActivity extends CameraActivity 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(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    @Override
    protected List<? extends CameraBridgeViewBase> getCameraViewList() {
        return Collections.singletonList(mOpenCvCameraView);
    }

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

    @Override
    public void onCameraViewStopped() {}

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat frame = inputFrame.rgba();
        Mat gray = new Mat();
        Imgproc.cvtColor(frame, gray, Imgproc.COLOR_RGBA2GRAY);
        return gray;
    }

    private CameraBridgeViewBase mOpenCvCameraView;
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment