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運行效果




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章