没有OpenCV Manager的OpenCV Android

时间:2014-05-08 12:35:49

标签: android opencv

我知道这个问题已在这里被多次询问和回答。但这些解决方案都不适合我:

这是我的代码(来自this page):

package com.example.helloopencv;

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.Mat;

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

public class HelloOpenCVActivity extends Activity implements CvCameraViewListener2 {

    protected static final String TAG = "HelloOpenCV";

    static {
        if (!OpenCVLoader.initDebug())
            Log.e(TAG, "Failed to load OpenCV!");
    }

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

    private CameraBridgeViewBase mOpenCvCameraView;

    @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.activity_hello_open_cv);
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCVView);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);
    }

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

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

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

    public void onCameraViewStarted(int width, int height) {
    }

    public void onCameraViewStopped() {
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        return inputFrame.rgba();
    }
}

我也遵循了设置here。但是,每次我尝试在我的设备中运行代码时(Galaxy Note 2),我总是被要求安装OpenCV Manager包。我可以错过什么吗?有人可以帮帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:9)

添加

mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

在onCreate()

之后的mOpenCvCameraView.setCvCameraViewListener(this);

并从onResume()

中删除OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);

如果其他一切都正确完成,则不应要求OpenCV Manager。

编辑:

如果您将以下代码放在onResume

Activity 方法中,那会更好

mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

答案 1 :(得分:3)

复制所有图书馆的文件。

或者只是在应用程序中安装管理器,如:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
    .setDataAndType(Uri.parse("file:///path/to/org.opencv.engine.v2.14.apk"), 
                    "application/vnd.android.package-archive");
startActivity(promptInstall); 

添加此权限:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />