Windows system Eclipse+ndk+android + OpenCv

Source: Internet
Author: User

Windows system Eclipse+ndk+android + OpenCv

Reference Documentation Blog

1 NDK Environment Building

Http://jingyan.baidu.com/article/5d6edee22d908799eadeec9f.html

2 official documents

How Android.mk and Application.mk are written, how the OpenCV library calls

http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html# Dev-with-ocv-on-android

3 OPENCV test Code Source

Http://www.verydemo.com/demo_c131_i131095.html

4 NDK OPENCV Test starter program (problematic)

http://blog.csdn.net/pwh0996/article/details/8957764

5 NDK Test Starter program (problematic)

http://blog.csdn.net/redoffice/article/details/6654714

6 Solutions to some problems

http://blog.csdn.net/houshunwei/article/details/17217695

?

There are two ways to call OpenCV library functions under the current Android platform:

1 using the encapsulated OPENCV API to develop Android: This way calls Java's class implementations in a package similar.

2 Use the JNI interface to invoke the OpenCV API function via the NDK platform (this is a more common approach).

This article will implement these two methods of invocation.

The first part of the preparatory work:

1.1 The software version used in this article is as follows:

ADT Version: ADT-22.6.3, development tools ADT (Android development tool), integrates the latest ADT and NDK plugins and Eclipse, and a latest version of the SDK. It can be used after decompression, http://developer.android.com/tools/sdk/ndk/index.html.

Ndk:android-ndk-r10, NDK plugin: For the development of Android NDK plug-ins, ADT version of more than 20, you can install the NDK plug-in, in addition to the NDK integration of the CDT plug-in, the NDK version of the R7 after the integration of Cygwin, but also a very lite version. Download Link interview: http://developer.android.com/tools/sdk/ndk/index.html

Opencv:opencv-2.4.4-android-sdk,http://opencv.org/downloads.html

?

1.2 Configuring the NDK environment variables

1.2.1 Unzip the NDK compression package and configure the environment variables.

Writes the extracted address to the environment variable path. Enter ndk-build at the command prompt if the following error appears instead of Ndk-build not found, the NDK environment has been installed successfully. In particular, the search engine will tell some of the earlier versions of the NDK to be used, at the command prompt, to enter build/host-setup.sh, but the NDK has been updated and the file is gone. You just need to enter the Ndk-build.

Android ndk:could not find application project directory!???
Android ndk:please define the Ndk_project_path variable to the it.???
/HOME/BRAINCOL/WORKSPACE/ANDROID/ANDROID-NDK-R5/BUILD/CORE/BUILD-LOCAL.MK:85: * * * Android ndk:aborting??? .? Stop.

1.2.2 Eclipse Configuring the NDK environment

Open Eclipse, point window->preferences->android->ndk, set the NDK path, for example the current NDK version is E:\android-ndk-r10.

?

1.3 Configuration OpenCV

Download OPENCV-2.4.4-ANDROID-SDK extracted, you can see the file under the APK file, doc document, sample example program, as well as OPENCV4ANDROID-SDK.

Among them, the OPENCV4ANDROID-SDK directory is the class library we need to develop OPENCV, samples directory contains a number of OPENCV application examples (including face detection, etc.), for our Android under the OpenCV development to provide reference The doc directory is the usage instructions and API documentation for the OpenCV class library, and the APK directory holds the corresponding kernel versions.

?

1.3.1 Installation Opencv_manager

???? opencv_2.4.3.2_manager_2.4 Application installation package. This app is used to manage the OpenCV class library in your mobile device, and before you run the OpenCV app, you must make sure that the opencv_2.4.4.4_manager_2.6_*.apk is installed on your phone. Otherwise, the OpenCV app will not run because it cannot load the OpenCV class library.

You can install the opencv_2.4.4.4_manager.apk according to your phone type.

By querying your phone's CPU information

ADB shell

Cat/proc/cpuinfo

?

My phone is arm V7–neon architecture, Android version is 4.4.4 Select 3rd, and the fourth is the mobile phone for the low-version Android system.

After the installation is complete, you can test opencv_2.4.4.4_manager.apk.

Find an example in the samples file rack to install the package, such as example-15-puzzle.apk, to install to the phone to test whether the puzzle game is working properly.

?

1.3.2 OPENCV4ANDROID-SDK Introduction to work space

(1) Copy the OPENCV4ANDROID-SDK directory to the working space of the Android project behind;

(2) Open Eclipse and introduce opencv-sdk into the workspace as shown in:

?

The second part starts the actual combat---developing Android using the encapsulated OpenCV API (not via JNI interface)

?

1.1 Create a project

??????? (1) open eclipse, create Android application Engineering API_OPENCV;

??????? (2) Add the test image lena.jpg to the resource directory res/drawable-hdpi ;

(2) Create a new directory on the phone's SD card root/sdcard andimg, put the image img1.jpg

??????? (3)in thePackage ExplorerSelect items inAPI_OPENCV, right-click in the pop-up menu and selectProperties, and then in the popupPropertiesleft selection in windowAndroid, then click on the lower rightADDbutton, selectOpenCV Library 2.4.4and clickOK, when the operation is complete, theOpenCVThe class library is added to theAPI_OPENCVof theAndroid Dependencies, as shown in:

?

1.2 Engineering Code:

?

(1) Layout file: Main.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Xmlns:tools= "Http://schemas.android.com/tools"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

tools:context= "${packagename}.${activityclass}" >

<button

Android:id= "@+id/imagegray"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "Image grayscale"/>

?

<imageview

Android:id= "@+id/imageview"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_below= "@+id/imagegray"

Android:layout_centerhorizontal= "true"

android:layout_margintop= "86DP"/>

</RelativeLayout>

?

(2) Add in ANDROIDMANIFEST.XMl File

?

<!--Create and delete files in SDcard permissions--

<uses-permission

Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!--write data to sdcard permissions--

<uses-permission

Android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

?


? (3) Mainactivity.java

?

Package COM.EXAMPLE.API_OPENCV;

?

Import Org.opencv.android.BaseLoaderCallback;

Import Org.opencv.android.LoaderCallbackInterface;

Import Org.opencv.android.OpenCVLoader;

Import Org.opencv.android.Utils;

Import Org.opencv.core.Mat;

Import Org.opencv.imgproc.Imgproc;

?

Import Android.annotation.SuppressLint;

Import android.app.Activity;

Import Android.graphics.Bitmap;

Import Android.graphics.Bitmap.Config;

Import Android.graphics.BitmapFactory;

Import Android.os.Bundle;

Import Android.view.Menu;

Import Android.view.MenuItem;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.ImageView;

?

public class Mainactivity extends Activity {

?

???? ???? Private Button Btngray;

???? Private ImageView ImageView;

???? Private Bitmap bmp;

???? ?

???? The OpenCV class library loads and initializes the successful callback function, where we do nothing

???????? Private Baseloadercallback Mopencvcallback = new Baseloadercallback (this) {

???? @Override

???? public void onmanagerconnected (int status) {

???? Switch (status) {

???? Case Loadercallbackinterface.success:

???? {

???? } break;

???? Default

???? {

???? super.onmanagerconnected (status);

???? } break;

???? }

???? }

???? };

???? ?

[email protected] ("Sdcardpath")

[Email protected]

???????? protected void OnCreate (Bundle savedinstancestate) {

???????????? Super.oncreate (savedinstancestate);

???????????? Setcontentview (R.layout.activity_main);

???????????? This.settitle ("Android API OpenCV");

???????????? Btngray = (Button) Findviewbyid (R.id.imagegray);

???? ImageView = (ImageView) This.findviewbyid (R.id.imageview);

???? Bmp=bitmapfactory.decodefile ("/sdcard/andimg/img2.jpg");

???? Imageview.setimagebitmap (BMP);

?

???? Btngray.setonclicklistener (New Onclicklistener () {????

[Email protected]

???????????????? public void OnClick (View v) {

???????? Mat Rgbmat = new Mat ();

???????? Mat Graymat = new Mat ();

???????? Gets the pixel data for the Lena color image

???????? Utils.bitmaptomat (BMP, Rgbmat);

???????? Convert color image data to grayscale image data and store it in Graymat

???????? Imgproc.cvtcolor (Rgbmat, Graymat, Imgproc.color_rgb2gray);

???????? Create a grayscale image

???????? Bitmap graybmp = Bitmap.createbitmap (Bmp.getwidth (), Bmp.getheight (), config.rgb_565);

???????? Convert matrix Graymat to grayscale images

???????? Utils.mattobitmap (Graymat, graybmp);

???????? Imageview.setimagebitmap (graybmp);

????????????????}

????????????});

???? ?

????????}

?

[Email protected]

???? public void Onresume ()

???? {

???? Loading and initializing the OpenCV class library through the OpenCV engine service, the so-called OPENCV engine service is

???? OPENCV_2.4.4_MANAGER_2.4_*.APK package that exists in the APK directory of the OpenCV installation package

???? Super.onresume ();

???? Opencvloader.initasync (Opencvloader.opencv_version_2_4_4, this, mopencvcallback);

???? }

?

}


1.3 Run Results

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.