OpenCV Study Notes (vi)--OPENCV for Android Open camera

Source: Internet
Author: User

In the previous chapter, we completed the configuration of the Android Platform development environment, and also found a way to remove the OpenCV Manager API, then we start from scratch, the completion of a personal program, the implementation of the following functions:

1. Identify the specified picture and draw the edge of the picture with a colored box

2. Display a 3D model on the identified image

It is not difficult to see that this is actually the initial function of AR, of course, to complete this function but need to complete a lot of things, steps:

Open Camera Get image Flow -- pattern Recognition -- object tracking -- drawing model

=============================== Split Line =====================================

Here, we mainly invoke the API in the OpenCV Android SDK to realize the function of turning on the camera.

First, knowledge Preparation:

1. Learn how Android works on your device

2. Understand the C + + compilation process and be able to read makefile


Second, the realization process:

1. Open Eclipse and create a new blank Android project:

The project name is opencv_test:


2. Introduction of the OpenCV Library-3.1.0 Library project for the new project:

Select Project, right click Properties, then in Android tab, add a library project reference with the Add function.


3. Open the SRC directory below the mainactivity, because our goal is to open the camera full screen in the application through the OpenCV Java API implementation, and get the preview box, so mainactivity need to implement CvCameraViewListener2 interface , you can implement three methods, namely:oncameraviewstarted,oncameraviewstopped and Oncameraframe, the key image processing is written in the oncameraframe function:


4. Modify the Androidmanifest.xml file:

To add a camera's permissions:

<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 "/>
to set the app's interface theme to a full-screen display without the top title bar, add it in the Application tab:

<application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @android: Style/theme.notitlebar.fullscreen ">

5. Add the components that display the camera content for the interface layout file:

Open the Activity_main.xml layout file below Res/layout and add a OpenCV visual component Javacameraviewto the layout:

<relativelayout     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:id= "@+id/camera_view"        opencv:show_fps= "true"         opencv:camera_id= "any"/></relativelayout>


6. Go back to Mainactivity and complete the API call:

Declares a camerabridgeviewbase object that holds the Javacameraview component in the Activity_main.xml and implements bindings and adds event snooping in OnCreate:

Mcvcamera = (camerabridgeviewbase) Findviewbyid (R.id.camera_view); Mcvcamera.setcvcameraviewlistener (this);


7. Add Nativesupport, which is the addition of C + +, in order to not rely on OpenCV Manager, directly into the library file for compiling:


Remember the name here is the name of the imported library file that was compiled to generate the. so file:


Open the android.mk file under the generated JNI directory, which is the makefile configuration file that was used to compile and link the C/s + +, slightly modify the content, and add the following after "include $ (clear_vars)":

Opencv_camera_modules:=onopencv_install_modules:=onopencv_lib_type:=sharedifdef OPENCV_ANDROID_SDK  ifneq ("" , "$ (wildcard $ (OPENCV_ANDROID_SDK)/opencv.mk)")    include ${opencv_android_sdk}/opencv.mk  else    include ${opencv_android_sdk}/sdk/native/jni/opencv.mk  endifelse  include: /.. /sdk/native/jni/opencv.mkendif
Thus, we have completed the introduction of the library file, then the next step is to let the camera input frame on the preview component Javacameraview.


8. Modify the contents of the public Mat oncameraframe (cvcameraviewframe inputframe) callback function, which is called once for each frame of the camera refresh . And each time the input parameter is the current camera view information, we directly get the RGBA information in it as the mat data returned to the display component:

/** * Image processing is written here */@Overridepublic Mat oncameraframe (cvcameraviewframe inputframe) {// Directly returns the RGBA data for the input video preview and exists in the mat data Return Inputframe.rgba ();}


9. In the above operation, we have obtained the Mcvcamera object in the OnCreate function, only after calling Mcvcamera.enableview () , the preview component will display the mat image for each frame. But before we show it, we have to make sure that the OpenCV library file is loaded, so calling this method requires asynchronous processing:

/** * Manage Android services via OPENCV, asynchronously initialize OPENCV */baseloadercallback mloadercallback = new Baseloadercallback (this) {@ overridepublic void onmanagerconnected (int status) {switch (status) {case loadercallbackinterface.success:log.i (TAG, " OpenCV loaded successfully "); Mcvcamera.enableview (); break;default:break;}};

so only when Mloadercallback receive Loadercallbackinterface.success message, will open the preview display, then this message is sent from, this need we Rewrite the activity's Onrusume method, because this method is called every time activity activation is active, so you can detect whether the OpenCV library file is loaded at this point:

@Overridepublic void Onresume () {super.onresume (); Opencvloader.initdebug ()) {LOG.D (TAG, "OpenCV Library not found!");} else {log.d (TAG, "OpenCV Library found inside package. Using it! "); Mloadercallback.onmanagerconnected (loadercallbackinterface.success);}};


Third, the results show:

At this point, we have finished the equipmentCamera OpenAndentering preview Data acquisition, running on the device you can see:

OpenCV Study Notes (vi)--OPENCV for Android Open camera

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.