w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can I use OpenCV with stored images, without a camera?

If someone is still looking for an answer:

package com.example.ocv4androidwithoutcamera;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.InstallCallbackInterface;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity implements
LoaderCallbackInterface {

    protected BaseLoaderCallback mOpenCVCallBack = new
BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                onOpenCVReady();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        Log.i("DEMO", "Trying to load OpenCV library");
        if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4,
this, mOpenCVCallBack))
        {
            Log.e("DEMO", "Cannot connect to OpenCV Manager");
        }
    }

    protected void onOpenCVReady(){
        //this should crash if opencv is not loaded
        Mat img = new Mat();
        Toast.makeText(getApplicationContext(), "opencv ready",
Toast.LENGTH_LONG).show();

    }

    @Override
    public void onManagerConnected(int status) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPackageInstall(int operation,
            InstallCallbackInterface callback) {
        // TODO Auto-generated method stub

    }

}

Don’t forget to add opencv library in Project Properties => Android => Library.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.