Android配置OpenCV,不需要安装OpenCV Manager



本文配套源码下载:源码下载

1.工具版本&下载

ADT-Bundle版本:adt-bundle-windows-x86-20140702

下载地址:http://pan.baidu.com/s/1gdjaqPH

NDK版本:android-ndk-r10d

下载地址(x86):http://pan.baidu.com/s/1dD2bj2L

OpenCV版本:OpenCV-2.4.10-android-sdk

下载地址:http://pan.baidu.com/s/1i31TczV

2.环境配置

2.1配置NDK:

下载android-ndk:见博文开头。

解压缩后,复制文件夹根目录地址,如:E:\Tools\android-ndk-r10d

新建系统环境变量NDKROOT,变量值为根目录的地址


安装eclipse 的NDK插件,下载地址:http://download.csdn.net/detail/cqugao/8638433,下载后拷贝到eclipse根目录的plugins文件夹中,重启eclipse即可,重启后,点击Window->Preferences,在Android中,出现NDK选项,点解NDK选项,配置NDK目录。


2.2配置OpenCV forAndroid:

        下载OpenCV:见博文开头

解压缩后,复制文件夹根目录地址,如:E:\Tools\OpenCV-2.4.10-android-sdk

新建系统环境变量CVROOT,变量值为根目录的地址


3.工程配置

3.1配置NDK

        首先在adt中新建一个安卓工程,在工程名处右键,选择Android Tools->Add Native Support…


        在弹出的对话框中输入库名称,点击Finish。


         添加后,工程文件夹中多了一个jni文件夹,包含一个Android.mk与一个cpp文件。

 

3.2导入OpenCV Library 的jar包:

         在eclipse中点击File->New->Project,导入OpenCVLibrary工程。

 

        选择Android->AndroidProject from Existing Code,点击Next。


         点击Browse,选择OpenCV-android-sdk的解压根目录\sdk目录,点击Finish。

         工程文件夹中 出现OpenCV Library


右键自己的工程文件,选择Properties,在弹出的对话框中选择Android,在右侧栏中点击Add,在弹出的ProjectSelection中选择OpenCV Library,点击OK。



4.使用OpenCV for Android 打开手机摄像头

4.1修改Android.mk文件

include $(CLEAR_VARS)include$(BUILD_SHARED_LIBRARY)中间替换成如下代码

OPENCV_CAMERA_MODULES := on

OPENCV_INSTALL_MODULES := on

OPENCV_LIB_TYPE := SHARED

include${CVROOT}\sdk\native\jni\OpenCV.mk

 

LOCAL_SRC_FILES  := CVDemo.cpp

LOCAL_C_INCLUDES += $(LOCAL_PATH)

LOCAL_LDLIBS     += -llog -ldl

 

LOCAL_MODULE     := CVDemo

其中,LOCAL_SRC_FILES对应jni文件夹中的cpp文件,LOCAL_MODULE对应最终编译结果的库文件,这里要修改成自己对应的文件名和库名。


4.2在jni文件夹中新建Application.mk文件:

添加如下代码:

APP_STL := gnustl_static

APP_CPPFLAGS := -frtti -fexceptions

APP_ABI := armeabi-v7a

APP_PLATFORM := android-8


4.3修改布局文件,添加相机控件

<LinearLayout 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" >

   <org.opencv.android.JavaCameraView

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:id="@+id/activity_camera_view" />

 

</LinearLayout>

4.4修改工程配置文件,添加相应权限

<?xml version="1.0"encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.cvdemo"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

       android:minSdkVersion="8"

       android:targetSdkVersion="21"/>

 

    <supports-screens android:resizeable="true"

                      android:smallScreens="true"

                      android:normalScreens="true"

                      android:largeScreens="true"

                      android:anyDensity="true"/>

   

    <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="@style/AppTheme" >

        <activity

           android:name="com.example.cvdemo.MainActivity"

           android:label="@string/app_name"

           android:screenOrientation="landscape"

           android:configChanges="keyboardHidden|orientation">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 </manifest>


4.5修改MainActivity

令MainActivity继承CvCameraViewListener2接口:

MainActivity extends Activity implements CvCameraViewListener2

         添加摄像头控件

private CameraBridgeViewBase mOpenCvCameraView;

         添加当前帧数据

private Mat mRgba;

         实现CvCameraViewListener2接口函数

public void onCameraViewStarted(int width, int height) {

        mRgba = new Mat(height, width, CvType.CV_8UC4);   // 为图像帧数据申请空间,8位4通道图像,包括透明alpha通道

}

public void onCameraViewStopped() {

        mRgba.release();   // 回收帧数据

}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        mRgba = inputFrame.rgba();   // 获取摄像头前景图像
        return mRgba;

}

         在OnCreate函数添加初始化过程:

requestWindowFeature(Window.FEATURE_NO_TITLE);   // 删除标题
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 设置全屏
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.activity_camera_view);    // 绑定摄像头控件
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();

         添加静态过程,导入opencv_java

static {
    if (OpenCVLoader.initDebug()) {
        System.loadLibrary("opencv_java");// load other libraries
       }
}

4.6运行效果




猜你喜欢

转载自blog.csdn.net/liu_xiao_cheng/article/details/52956142