native activity camera on lollipop with opencv

774 views Asked by At

It seems that opencv can't use native camera on Android 5.+ ( lollipop ). cf : http://code.opencv.org/issues/4185

Is there an other way to grab pictures from a native activity and then convert into cv::mat ? Or, maybe I could use jni to call a grab function in java from my c++ activity ?

Thank you for your help

Charles

1

There are 1 answers

0
Bill Rock On

You could use jni to call a grab function in java from c++ activity, like this(threshold example):

Java code:

//Override JavaCameraView OpenCV function
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    ProcessImage.threshold(mGray, mGray, 97, 0);

    return mGray;
}

// Your functions
public static void threshold(Mat srcGray, Mat dst, int thresholdValue, int thresholdType) {
    nativeThreshold(srcGray.getNativeObjAddr(), dst.getNativeObjAddr(), thresholdValue, thresholdType.ordinal());
}

private static native void nativeThreshold(long srcGray, long dst, int thresholdValue, int thresholdType);

JNI c++ code:

JNIEXPORT void JNICALL Java_{package}_nativeThreshold
  (JNIEnv * jenv, jobject jobj, jlong scrGray, jlong dst, jint thresholdValue, jint thresholdType)
{

    try
    {
        Mat matDst = *((Mat*)dst);
        Mat matSrcGray = *((Mat*)scrGray);
        threshold( matSrcGray, matDst, thresholdValue, max_BINARY_value, thresholdType );
    }
    catch(cv::Exception& e)
    {
        LOGD("nativeThreshold caught cv::Exception: %s", e.what());
        jclass je = jenv->FindClass("org/opencv/core/CvException");
        if(!je)
            je = jenv->FindClass("java/lang/Exception");
        jenv->ThrowNew(je, e.what());
    }
    catch (...)
    {
        LOGD("nativeThreshold caught unknown exception");
        jclass je = jenv->FindClass("java/lang/Exception");
        jenv->ThrowNew(je, "Unknown exception in JNI code ProcessImage.nativeThreshold()");
    }
}

Hope this helps!