【问题标题】:native activity camera on lollipop with opencv带有opencv的棒棒糖上的本机活动相机
【发布时间】:2015-09-04 12:44:48
【问题描述】:

opencv 似乎无法在 Android 5.+ ( lollipop ) 上使用本机摄像头。 参考: http://code.opencv.org/issues/4185

还有其他方法可以从原生活动中获取图片然后转换为 cv::mat 吗? 或者,也许我可以使用 jni 从我的 c++ 活动中调用 java 中的抓取函数?

感谢您的帮助

查尔斯

【问题讨论】:

    标签: android opencv android-camera android-5.0-lollipop native


    【解决方案1】:

    您可以使用 jni 从 c++ 活动中调用 java 中的抓取函数,如下所示(阈值示例):

    Java 代码:

    //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++ 代码:

    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()");
        }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      相关资源
      最近更新 更多