Create an application that can use OpenCV Javacameraview to understand the development process for OPENCV Java API-based applications. With the Android base, there are several places in the program that need to be modified 1.activity_main.xml 2.androidmanifest.xml 3.mainactivity.java
First, create the project
Install create an Android program to create a blank activity, the project name is Hellosamples, the other uses the default Activity_main.xml,mainactivity.java. Once created, the OpenCV Library is included in the project (important steps) in the manner described earlier in the installation.
Second, set the layout file
Open Activity_main.xml
Copy the following code
<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:id= "@+id/helloopencvview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:visibility= "Gone"opencv:camera_id= "any"Opencv:show_fps= "true"/></linearlayout>
The layout file contains only one full-screen part class Org.opencv.android.JavaCameraView, this class is implemented in the OpenCV library, and is implemented using Android Cameraapi. opencv:show_fps= "true" and opencv:camera_id= "any" represent the number of frames per second that can be displayed, as well as any one by one cameras available. Of course, the first use is the rear.
third, set up the Androidmanifest.xml file
Open Androidmanifest.xml Copy the following code
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.hellosamples "Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK android:minsdkversion= "+"android:targetsdkversion= "/>" <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"/> <application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@android: Style/theme.notitlebar.fullscreen" > <activity android:name= ". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.actio N.main "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-fil ter> </activity> </application></manifest>
This is primarily a setup license and theme
Iv. Preparation of Mainactivity.java
PackageCom.example.hellosamples;importOrg.opencv.android.baseloadercallback;importOrg.opencv.android.camerabridgeviewbase;importOrg.opencv.android.camerabridgeviewbase.cvcameraviewframe;importOrg.opencv.android.camerabridgeviewbase.cvcameraviewlistener2;importOrg.opencv.android.loadercallbackinterface;importOrg.opencv.android.opencvloader;importOrg.opencv.core.mat;importAndroid.app.activity;importAndroid.os.bundle;importAndroid.util.log;importAndroid.view.surfaceview;importAndroid.view.windowmanager;public class Mainactivity extends Activity implementscvcameraviewlistener2{private static final String TAG = "Ocvsample::activity"; Private Baseloadercallback Mloadercallback = new Baseloadercallback ( this) {@Override public void onmanagerconnected (intstatus) {Switch(status) { caseloadercallbackinterface.success: {log.i (TAG, "OpenCV loaded successfully") /c5>); Mopencvcameraview.enableview (); } Break; Default: {Super. onmanagerconnected (status); } Break; } } }; @Override public voidOnresume () {Super. Onresume (); Opencvloader.initasync (Opencvloader.opencv_version_2_4_6, this, Mloadercallback); } Privatecamerabridgeviewbase Mopencvcameraview; @Override public voidonCreate (Bundle savedinstancestate) {log.i (TAG, "called OnCreate"); Super. OnCreate (savedinstancestate); GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Setcontentview (R.layout.activity_main); Mopencvcameraview =(camerabridgeviewbase) Findviewbyid (R.id.helloopencvview); Mopencvcameraview.setvisibility (surfaceview.visible); Mopencvcameraview.setcvcameraviewlistener ( this); } @Override public voidOnPause () {Super. OnPause (); if (Mopencvcameraview! = null) Mopencvcameraview.disableview (); } public voidOnDestroy () {Super. OnDestroy (); if (Mopencvcameraview! = null) Mopencvcameraview.disableview (); } public void oncameraviewstarted (int width, intheight) {} public voidoncameraviewstopped () {} PublicMat oncameraframe (cvcameraviewframe inputframe) {returnInputframe.rgba (); }}
The first is the OPENCV initialization process, onmanagerconnected callback is called in the UI thread, and must be included before using the OPENCV call or loading the local library of the OPENCV support. When the OPENCV is successfully initialized and loaded into the local library, the default Baseloadercallback treats the application context as Activity, so Activity.finish () is called when the initialization fails . To override this behavior, you must rewrite the finish () method.
This is done in a non-synchronous manner (i.e. to install the OpenCV Manager on the target machine), and then some processing of the activity lifecycle process. The main modification is to implement the important function oncameraframein cvcameraviewlistener2, which allows you to process the image after the camera captures the image frame and then display it on the screen, where The Cvcameraviewframe class represents an image frame captured from the camera. This class can be used in two ways Rgba (), Gray () to Rgba and single-channel images.
Android Learning Seven---Hello OpenCV samples