Ad Code

Android Studio opencv camera example | How to use opencv in Android Studio

Hello developers, in this tutorial I will show you the example of how to use OpenCV in Android Studio. So, follow the article carefully and add the code into your project. Hope this Article will help you.


Android Studio opencv camera example



Add opencv SDK into your project.

Pase below xml design code into your activity layout xml file

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

       <org.opencv.android.JavaCameraView
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:visibility="gone"
           android:id="@+id/HelloOpenCvView"
           opencv:show_fps="false"
           opencv:camera_id="any" />

</LinearLayout>

Now add the required permission into your AndroidManifest.xml file.

AndroidManifest.xml
<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"/>

Now initialise the Callback.

Initialize Callback
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

Paste the below code into your Activity Java file.

MainActivity.java
private CameraBridgeViewBase mOpenCvCameraView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     Log.i(TAG, "called onCreate");
     super.onCreate(savedInstanceState);
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.HelloOpenCvLayout);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener(this);
 }

 @Override
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

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

 public void onCameraViewStopped() {
 }

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

Hope this Artice helps to solve your problems. Thank you for visiting our site and for more info visit our YouTube channel.

Post a Comment

0 Comments

Ad Code