Android Studio + OpenCV(JNI问题)

时间:2016-06-27 07:42:01

标签: android opencv android-studio java-native-interface

我正在开发一款可以实时应用DFT的应用程序。 但首先我要努力建立JNI和OpenCV。但我认为我没有正确链接,因为我在构建项目时遇到了以下错误: Error Image 1

似乎无法找到与JNI相关的任何lib: Error Image 2

我正在学习本教程,但它使用Eclipse而不是Android Studio: http://www.jayrambhia.com/blog/ndk-android-opencv

这是我的项目树:

Project Tree

我不明白的一件奇怪的事情是,Windows资源管理器中的文件夹jni被称为jniMy。

以下代码是我的主要应用程序代码:

package com.example.pedroglp.dftopencv;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.*;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View.OnTouchListener;
import android.view.WindowManager;

public class opencvDft extends Activity implements CvCameraViewListener2 {

    public native int convertNativeGray(long matAddrRgba, long matAddrGray);

    private Mat mRgba;
    private Mat mGray;

    private CameraBridgeViewBase mOpenCvCameraView;

    private static final String TAG = "OCVSample::NDK";

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    System.loadLibrary("nativegray");
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opencv_dft);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //setContentView(R.layout.activity_opencvpart);
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.opencv_part_java_surface_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat();
        mGray = new Mat();
    }

    public void onCameraViewStopped() {
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        convertNativeGray(mRgba.getNativeObjAddr(), mGray.getNativeObjAddr());
        return mGray;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.opencvpart, menu);
        return true;
    }

}

我的build.graddle:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "24"

        defaultConfig.with {
            applicationId = "android.overloaded.opencv31v1" //Name of package
            minSdkVersion.apiLevel = 11
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"

        }
    }

    android.ndk {
        moduleName = "native" // Name of C++ module, for System.loadLibrary("native")
        cppFlags.add("-std=c++11") // Add provisions to allow C++11 functionality
        cppFlags.add("-fexceptions")
        // YOUR OPENCV DIRECTORY!!!
        cppFlags.add("-I${file("D:\\Users\\PedroGabriel\\Downloads\\OpenCV-3.1.0-android-sdk\\OpenCV-android-sdk\\sdk\\native\\jni\\include\\")}".toString())
        ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
        stl = "gnustl_shared"
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file("proguard-rules.txt"))
        }
    }

    android.productFlavors {
        create("arm") {
            ndk.with{
                abiFilters.add("armeabi")

                File curDir = file('./')
                curDir = file(curDir.absolutePath)
                String libsDir = curDir.absolutePath + "\\src\\main\\jniLibs\\armeabi\\"

                ldLibs.add(libsDir + "libopencv_core.a")
                ldLibs.add(libsDir + "libopencv_highgui.a")
                ldLibs.add(libsDir + "libopencv_imgproc.a")
                ldLibs.add(libsDir + "libopencv_java3.so")
                ldLibs.add(libsDir + "libopencv_ml.a")

            }
        }
        create("armv7") {
            ndk.with {
                abiFilters.add("armeabi-v7a")

                File curDir = file('./')
                curDir = file(curDir.absolutePath)
                String libsDir = curDir.absolutePath + "\\src\\main\\jniLibs\\armeabi-v7a\\"
                ldLibs.add(libsDir + "libopencv_core.a")
                ldLibs.add(libsDir + "libopencv_highgui.a")
                ldLibs.add(libsDir + "libopencv_imgproc.a")
                ldLibs.add(libsDir + "libopencv_java3.so")
                ldLibs.add(libsDir + "libopencv_ml.a")
                ldLibs.add(libsDir + "libopencv_ts.a")

            }
        }
    }
    android.sources{
        main{
            jni{
                source{
                    srcDirs += ['src\\main\\jniMy']
                }
            }
        }
    }

}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'
    //Do not use 23.2.0
    compile project(':openCVLibrary310')
}

我的Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

include D:\Users\PedroGabriel\Downloads\OpenCV-3.1.0-android-sdk\OpenCV-android-sdk\sdk\native\jniOpenCV.mk

LOCAL_MODULE    := nativegray
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

我真的很困惑所有这些目录结构和用于链接JNI的代码。如果你不知道答案,但也有一些更新教程。

谢谢你, 佩德罗。

0 个答案:

没有答案