Andriod Studio OpenCV 3.2,无法使用本机OpenCV打开视频文件或Android摄像头 [英] Andriod Studio OpenCV 3.2, Cannot open video file or android camera with native OpenCV

查看:257
本文介绍了Andriod Studio OpenCV 3.2,无法使用本机OpenCV打开视频文件或Android摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用相机/视频进行Android本机图像处理.

I'm currently working on Android native image processing with camera/video.

我正在使用OpenCV 3.2捕获和处理图像.
问题是我无法打开任何类型的视频源或相机

I'm using OpenCV 3.2 to capture and process image.
The problem is that I cannot open any kind of video sources or camera!

这是我的c ++代码.

This is my c++ code.

cv::VideoCapture cap;
cv::Mat frame;

JNIEXPORT void JNICALL
Java_com_project_vmtest_Native_openVideo(JNIEnv *env, jobject instance, jstring fileName) {

    const char * fileNameNative;
    jboolean isCopy;
    fileNameNative = env->GetStringUTFChars(fileName, &isCopy);

    cap = cv::VideoCapture(fileNameNative);
    cap.open(fileNameNative);
    if(cap.isOpened())        // ALWAYS FALSE HERE!!!!
        cap.read(frame);
}

JNIEXPORT void JNICALL
Java_com_project_vmtest_Native_render(JNIEnv *env, jclass type) {
    if(engine){
        cap.read(frame);
        if (frame.empty())      return;    // ALWAYS RETURNS HERE!!!!
        engine->prepare(frame);
        engine->renderObjToFBO();
        engine->renderFrame();
        engine->clearFrameData();
    }
}

我检查了fileName,这似乎是正确的.
即这是我的文件结构:

I checked fileName and it seems to be proper.
i.e. This is my file structure:

app\src\main\cpp\native_main.cpp    // native code
app\src\main\assets\vmData\test\testFile.mp4   // video file

那么fileName是:

fileName = "../assets/vmData/test/testFile.mp4"

我尝试使用".. \ assets \ vmData \ test \ testFile.mp4",但没有任何改变.

I tried with "..\assets\vmData\test\testFile.mp4" and nothing changed.

我尝试cap.open(CV_CAP_ANDROID + 0);来获取相机预览并得到相同的结果,VideoCapture根本无法打开!

I tried cap.open(CV_CAP_ANDROID + 0); to grab camera preview and got the same result, VideoCapture won't open at all!

我使用以下代码启用了Android摄像头权限.

I enabled the Android camera permission with below code.

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-feature android:name="android.hardware.camera.front"/>
<uses-feature android:name="android.hardware.camera.front.autofocus"/>

我的测试设备是Android 5.0 Lollypop,所以我认为这不是许可问题.

My test device is Android 5.0 Lollypop so I don't think it's any kind of permission issue.

奋斗了几个小时.
任何帮助表示赞赏!

Struggling for hours.
Any helps are appreciated!

推荐答案

我无法在本机代码中打开照相机,相反,我最终使用了Android OpenCV照相机实现opencv.android.CameraBridgeViewBase.

I failed to open camera inside Native code, instead I ended up using android OpenCV Camera implementation, opencv.android.CameraBridgeViewBase.


MainActivity.java


MainActivity.java

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

    private CameraBridgeViewBase mCvCamView;
    Mat matInput, matOutput;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    mCvCamView.enableView();
                    break;
                default:
                    super.onManagerConnected(status);
                    break;
            }
        }
    };


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

        mCvCamView = (CameraBridgeViewBase)findViewById(R.id.cam_view);
        mCvCamView.setVisibility(SurfaceView.VISIBLE);
        mCvCamView.setCvCameraViewListener(this);
        mCvCamView.setCameraIndex(1); // front-camera(1),  back-camera(0)
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }

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

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

        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "onResume :: Internal OpenCV library not found.");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "onResume :: OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

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

    @Override
    public void onCameraViewStarted(int width, int height) {
        if ( matOutput == null || matOutput.empty() )
            matOutput = new Mat(height, width, CvType.CV_8UC4, new Scalar(255, 0, 0, 255));
    }

    @Override
    public void onCameraViewStopped() {}

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        matInput = inputFrame.rgba();
        Native.extract(matInput.getNativeObjAddr(), matOutput.getNativeObjAddr());
        return matOutput;
    }

}


activity_main.xml


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:id="@+id/cam_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

这篇关于Andriod Studio OpenCV 3.2,无法使用本机OpenCV打开视频文件或Android摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆