Example usage for org.opencv.android BaseLoaderCallback onManagerConnected

List of usage examples for org.opencv.android BaseLoaderCallback onManagerConnected

Introduction

In this page you can find the example usage for org.opencv.android BaseLoaderCallback onManagerConnected.

Prototype

public void onManagerConnected(int status) 

Source Link

Usage

From source file:org.lasarobotics.vision.opmode.VisionOpModeCore.java

License:Open Source License

@Override
public void init() {
    //Initialize camera view
    BaseLoaderCallback openCVLoaderCallback = null;
    try {/*from w ww  . ja v  a 2s  . co m*/
        openCVLoaderCallback = new BaseLoaderCallback(hardwareMap.appContext) {
            @Override
            public void onManagerConnected(int status) {
                switch (status) {
                case LoaderCallbackInterface.SUCCESS: {
                    //Woohoo!
                    Log.d("OpenCV", "OpenCV Manager connected!");
                    openCVInitialized = true;
                }
                    break;
                default: {
                    super.onManagerConnected(status);
                }
                    break;
                }
            }
        };
    } catch (NullPointerException e) {
        error("Could not find OpenCV Manager!\r\n" + "Please install the app from the Google Play Store.");
    }

    final Activity activity = (Activity) hardwareMap.appContext;
    final VisionOpModeCore t = this;

    if (!OpenCVLoader.initDebug()) {
        Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
        boolean success = OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, hardwareMap.appContext,
                openCVLoaderCallback);
        if (!success) {
            Log.e("OpenCV", "Asynchronous initialization failed!");
            error("Could not initialize OpenCV!\r\n"
                    + "Did you install the OpenCV Manager from the Play Store?");
        } else {
            Log.d("OpenCV", "Asynchronous initialization succeeded!");
        }
    } else {
        Log.d("OpenCV", "OpenCV library found inside package. Using it!");
        if (openCVLoaderCallback != null)
            openCVLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        else {
            Log.e("OpenCV", "Failed to load OpenCV from package!");
            return;
        }
    }

    while (!openCVInitialized) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            LinearLayout layout = new LinearLayout(activity);
            layout.setOrientation(LinearLayout.VERTICAL);

            layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            openCVCamera = new JavaCameraView(hardwareMap.appContext, 0);

            layout.addView(openCVCamera);
            layout.setVisibility(View.VISIBLE);

            openCVCamera.setCvCameraViewListener(t);
            if (openCVCamera != null)
                openCVCamera.disableView();
            openCVCamera.enableView();
            if (!openCVCamera.connectCamera(initialMaxSize, initialMaxSize))
                error("Could not initialize camera!\r\n"
                        + "This may occur because the OpenCV Manager is not installed,\r\n"
                        + "CAMERA permission is not allowed in AndroidManifest.xml,\r\n"
                        + "or because another app is currently locking it.");

            //Initialize FPS counter and sensors
            fps = new FPS();
            sensors = new Sensors();

            //Done!
            width = openCVCamera.getFrameWidth();
            height = openCVCamera.getFrameHeight();
            initialized = true;
        }
    });

    while (!initialized) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}