安卓openCV和NDK在安卓工作室2.1.2



我刚刚开始在Android studio中使用Android NDKopenCV

我已经完成了这里提到的所有步骤(包括导入openCV-sdk)https://www.learn2crack.com/2016/03/setup-opencv-sdk-android-studio.html并遵循了openCV Github示例中的基本教程https://github.com/opencv/opencv/tree/master/samples/android.在教程2中,当我编译和运行应用程序时,它使用jni文件夹来存储C/C++代码。下面是错误

07-27 17:55:43.407 13197-13197/com.sample.opencvtest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.sample.opencvtest, PID: 13197
                                                                   java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.sample.opencvtest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.sample.opencvtest-1/lib/arm, /data/app/com.sample.opencvtest-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]] couldn't find "libopenCVLibrary310.so"
                                                                       at java.lang.Runtime.loadLibrary(Runtime.java:367)
                                                                       at java.lang.System.loadLibrary(System.java:1076)
                                                                       at com.sample.opencvtest.Tutorial2$1.onManagerConnected(Tutorial2.java:54)
                                                                       at com.sample.opencvtest.Tutorial2.onResume(Tutorial2.java:121)
                                                                       at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
                                                                       at android.app.Activity.performResume(Activity.java:6327)
                                                                       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
                                                                       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

错误发生在语句

// Load native library after(!) OpenCV initialization
System.loadLibrary("openCVLibrary310"); // Confusing part here what should be pointed here in place of "openCVLibrary"

下面是MainActivity代码

public class Tutorial2 extends Activity implements CameraBridgeViewBase.CvCameraViewListener2
{
    private static final String TAG = "OCVSample::Activity";
    private static final int VIEW_MODE_RGBA = 0;
    private static final int VIEW_MODE_GRAY = 1;
    private static final int VIEW_MODE_CANNY = 2;
    private static final int VIEW_MODE_FEATURES = 5;
    private int mViewMode;
    private Mat mRgba;
    private Mat mIntermediateMat;
    private Mat mGray;
    private MenuItem mItemPreviewRGBA;
    private MenuItem mItemPreviewGray;
    private MenuItem mItemPreviewCanny;
    private MenuItem mItemPreviewFeatures;
    private CameraBridgeViewBase mOpenCvCameraView;
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
    {
        @Override
        public void onManagerConnected(int status)
        {
            switch (status)
            {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    // Load native library after(!) OpenCV initialization
                    System.loadLibrary("openCVLibrary310");
                    mOpenCvCameraView.enableView();
                }
                break;
                default:
                {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
    public Tutorial2()
    {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.tutorial2_surface_view);
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial2_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        Log.i(TAG, "called onCreateOptionsMenu");
        mItemPreviewRGBA = menu.add("Preview RGBA");
        mItemPreviewGray = menu.add("Preview GRAY");
        mItemPreviewCanny = menu.add("Canny");
        mItemPreviewFeatures = menu.add("Find features");
        return true;
    }
    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }
    @Override
    public void onResume()
    {
        super.onResume();
        if (!OpenCVLoader.initDebug())
        {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        }
        else
        {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }
    public void onDestroy()
    {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }
    public void onCameraViewStarted(int width, int height)
    {
        mRgba = new Mat(height, width, CvType.CV_8UC4);
        mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);
        mGray = new Mat(height, width, CvType.CV_8UC1);
    }
    public void onCameraViewStopped()
    {
        mRgba.release();
        mGray.release();
        mIntermediateMat.release();
    }
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
    {
        final int viewMode = mViewMode;
        switch (viewMode)
        {
            case VIEW_MODE_GRAY:
                // input frame has gray scale format
                Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
                break;
            case VIEW_MODE_RGBA:
                // input frame has RBGA format
                mRgba = inputFrame.rgba();
                break;
            case VIEW_MODE_CANNY:
                // input frame has gray scale format
                mRgba = inputFrame.rgba();
                Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100);
                Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
                break;
            case VIEW_MODE_FEATURES:
                // input frame has RGBA format
                mRgba = inputFrame.rgba();
                mGray = inputFrame.gray();
                FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr());
                break;
        }
        return mRgba;
    }
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
        if (item == mItemPreviewRGBA)
        {
            mViewMode = VIEW_MODE_RGBA;
        }
        else if (item == mItemPreviewGray)
        {
            mViewMode = VIEW_MODE_GRAY;
        }
        else if (item == mItemPreviewCanny)
        {
            mViewMode = VIEW_MODE_CANNY;
        }
        else if (item == mItemPreviewFeatures)
        {
            mViewMode = VIEW_MODE_FEATURES;
        }
        return true;
    }
    public native void FindFeatures(long matAddrGr, long matAddrRgba);
}

我在app--> src--> main下添加了jniLibs文件夹,并从openCV-SDK-->native-->libs复制了armeabi-v7ax86_64文件夹。还在app--> src--> main下添加了jni文件夹,并有三个文件Android.mk, Application.mk and jni_part.cpp

jni_part.cpp代码

#include <jni.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
    Mat& mGr  = *(Mat*)addrGray;
    Mat& mRgb = *(Mat*)addrRgba;
    vector<KeyPoint> v;
    Ptr<FeatureDetector> detector = FastFeatureDetector::create(50);
    detector->detect(mGr, v);
    for( unsigned int i = 0; i < v.size(); i++ )
    {
        const KeyPoint& kp = v[i];
        circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
    }
}
}

我被困在这里了。任何类型的帮助都将不胜感激

其beacause android正在您的/jniLibs文件夹中查找"libopenCVLibrary310.so",但找不到它。

你应该检查你的Android.mk文件和android build.gradle文件,你已经提到

如果你想检查opencv是否成功集成,你可以尝试

System.loadLibrary("libopencv_java3");

如果您仍然出现错误,那么您已经错误地集成了opencv。

相关内容

  • 没有找到相关文章

最新更新