OpenCV Tutorial 1: Camera Preview


This tutorial shows one of the simplest way in which an Android application can use OpenCV (i.e., via the OpenCV application helper classes). The application grabs preview frames from the camera in real time, and displays them in full screen mode either using OpenCV’s Java or native camera API. It also allows the user to switch between the Java and native modes.

Observe the following special permissions in the AndroidManifest.xml file, which allow access to the device 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"/>

Note also, the following lines in the AndroidManifest.xml file that enable the application to run in full screen mode:

<application 
     android:icon="@drawable/icon"
     android:label="@string/app_name"
     android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

The tutorial only defines the Android Activity class, and uses the helper View classes from OpenCV (JavaCameraView or NativeCameraView). The application’s layout is described in the tutorial1_surface_view.xml file as the following:

<FrameLayout 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" >
     <org.opencv.android.JavaCameraView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:visibility="gone"
          android:id="@+id/tutorial1_activity_java_surface_view"
          opencv:show_fps="true" opencv:camera_id="any" />
     <org.opencv.android.NativeCameraView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:visibility="gone"
          android:id="@+id/tutorial1_activity_native_surface_view"
          opencv:show_fps="true" opencv:camera_id="any" />
</FrameLayout>

The above statements create and make invisible two full screen views of the predefined OpenCV types. Each view can become visible, depending on the application menu choice selected by the user. The application's Activity class is defined in the TutorialActivity.java file. The code to initialize OpenCV asynchronously via the OpenCV Manager Android service is as follows:

BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {

          @Override
          public void onManagerConnected(int status) {
               switch (status) {
                    case LoaderCallbackInterface.SUCCESS:
                    {
                         Log.i(TAG, "OpenCV loaded successfully");
                         mOpenCvCameraView.enableView();
                    } break;
                    default:
                    {
                         super.onManagerConnected(status);
                    } break;
               }
          }
     };
@Override
     public void onResume() {
          super.onResume();
          OpenCVLoader.initAsync( OpenCVLoader.OPENCV_VERSION_2_4_8,
                                  this, mLoaderCallback );
     }

Note that it is not allowed to use OpenCV Java calls or load the native libraries that call OpenCV native functions before invoking this callback.

The Tutorial1Activity class implements the CvCameraViewListener2 interface that allows the app to subscribe to the onCameraFrame callback. This callback delivers a new frame from the camera for processing before displaying it. The subscription is done with the following call:

@Override 
public void onCreate(Bundle savedInstanceState) {
     // …
     mOpenCvCameraView.setCvCameraViewListener(this);
}

On receiving a new frame, the activity class does not process it in any way, and simply returns it, displaying as:

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

Note: If you wish to call OpenCV for Tegra Java functions in your existing Eclipse Android projects, use the steps listed below to correctly setup your Android projects.

  1. Verify that the Eclipse project for the OpenCV Library is imported into your current workspace. If it isn’t, follow steps 3c-3e listed in the Setup section to import it.
  2. Include the OpenCV Library into your Android project by right clicking on the project in Project Explorer > Properties > Android > Library > Add > Select the listed OpenCV Library – 2.4.8.2 > OK.
  3. Modify your application for asynchronous initialization of OpenCV using the OpenCV Manager service.

For more details see: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#java

 

Note: It is also possible to employ so-called "static" initialization of the OpenCV libraries instead of asynchronous initialization with the OpenCV Manager.

The detailed procedure for how to do this is here . However, note the following exceptions to these instructions for development with OpenCV for Tegra.

  1. If your application does not have a JNI part, copy the library files from the <path to TADP directory>/OpenCV-2.4.8.2-Tegra-sdk/sdk/native/libs/tegra3 directory for Tegra 3 or 4 devices, <path to TADP directory>/OpenCV-2.4.8.2-Tegra-sdk/sdk/native/libs/tegra5-static-cuda for Tegra K1, or from the <path to TADP directory>/OpenCV-2.4.8.2-Tegra-sdk/sdk/native/libs/armeabi-v7a directory for Tegra 2 devices, respectively, to the libs/armeabi-7a folder of your project.
  2. If your application has a JNI part, on Tegra 3, 4i, 4 or K1, modify its Android.mk file to include the line:
    include ../../sdk/native/jni/OpenCV-tegra3.mk

    Tegra 2: 
    include ../../sdk/native/jni/OpenCV.mk   

 

 


NVIDIA® GameWorks™ Documentation Rev. 1.0.220830 ©2014-2022. NVIDIA Corporation and affiliates. All Rights Reserved.