Opencv sample code android

Should specify the application target platforms. In some cases a linkage error (like `"In function 'cv::toUtf16(std::basic_string<. >. undefined reference to 'mbstowcs'"`) happens when building an application JNI library, depending on OpenCV. The following line in the Application.mk usually fixes it:

APP_PLATFORM := android-9

Hello OpenCV Sample

Here are basic steps to guide you trough the process of creating a simple OpenCV-centric application. It will be capable of accessing camera output, processing it and displaying the result.

  1. Open Eclipse IDE, create a new clean workspace, create a new Android project File –> New –> Android Project
  2. Set name, target, package and minSDKVersion accordingly. The minimal SDK version for build with OpenCV4Android SDK is 11. Minimal device API Level (for application manifest) is 8.
  3. Allow Eclipse to create default activity. Lets name the activity HelloOpenCvActivity.
  4. Choose Blank Activity with full screen layout. Lets name the layout HelloOpenCvLayout.
  5. Import OpenCV library project to your workspace.
  6. Reference OpenCV library within your project properties.

dev_OCV_reference.png

< 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" > < org.opencv.android.JavaCameraView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:visibility = "gone" android:id = "@+id/HelloOpenCvView" opencv:show_fps = "true" opencv:camera_id = "any" /> < application android:icon = "@drawable/icon" android:label = "@string/app_name" android:theme = "@android:style/Theme.NoTitleBar.Fullscreen" > private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback( this ) < public void onManagerConnected( int status) < switch (status) < case LoaderCallbackInterface.SUCCESS: Log.i(TAG, "OpenCV loaded successfully" ); mOpenCvCameraView.enableView(); super.onManagerConnected(status); public void onResume() super.onResume(); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this , mLoaderCallback); private CameraBridgeViewBase mOpenCvCameraView; 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 ); 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();

Lets discuss some most important steps. Every Android application with UI must implement Activity and View. By the first steps we create blank activity and default view layout. The simplest OpenCV-centric application must implement OpenCV initialization, create its own view to show preview from camera and implements CvCameraViewListener2 interface to get frames from camera and process it.

First of all we create our application view using xml layout. Our layout consists of the only one full screen component of class org.opencv.android.JavaCameraView. This class is implemented inside OpenCV library. It is inherited from CameraBridgeViewBase, that extends SurfaceView and uses standard Android camera API.

After creating layout we need to implement Activity class. OpenCV initialization process has been already discussed above. In this sample we use asynchronous initialization. Implementation of CvCameraViewListener interface allows you to add processing steps after frame grabbing from camera and before its rendering on screen. The most important function is onCameraFrame. It is callback function and it is called on retrieving frame from camera. The callback input is object of CvCameraViewFrame class that represents frame from camera.

Note Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object does not have its own state and its behavior out of callback is unpredictable!

It has rgba() and gray() methods that allows to get frame as RGBA and one channel gray scale Mat respectively. It expects that onCameraFrame function returns RGBA frame that will be drawn on the screen.

doxygen

Generated on Tue Sep 10 2024 23:18:03 for OpenCV by 1.8.13