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.xm
l 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.
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.
|
NVIDIA® GameWorks™ Documentation Rev. 1.0.220830 ©2014-2022. NVIDIA Corporation and affiliates. All Rights Reserved.