首页 > 解决方案 > How to fix permission issue with opencv in native code for android platform

问题描述

I have built opencv(with ffmpeg) for android arm64-v8a successfully, however, cvCreateCameraCapture returns null. cvCreateFileCapture works well. I found we need to set the following item for support camera:

-DWITH_V4L=1 -DHAVE_CAMV4L2=ON

Now, v4l have been built in opencv. However, when app start, native source code will open camera by opencv's function cvCreateCameraCapture. The following interface in cap_libv4l.cpp(opencv's source code) can not open device successfully.

static void icvInitCapture_V4L() {
    //...
    CameraNumber = 0;
    while(CameraNumber < MAX_CAMERAS) {
      /* Print the CameraNumber at the end of the string with a width of one character */
      sprintf(deviceName, "/dev/video%1d", CameraNumber);

      /* Test using an open to see if this new device name really does exists. */
      deviceHandle = open(deviceName, O_RDONLY);
      char* strno = strerror(errno);
      LOGD("icvInitCapture_V4L wwwwwwwwwww %s !!\n", strno );
      if (deviceHandle != -1) {
        /* This device does indeed exist - add it to the total so far */
        // add indexList
        indexList|=(1 << CameraNumber);
        numCameras++;
      }
      //...
    }

That open function failed because of permission denied.

icvInitCapture_V4L wwwwwwwwwww Permission denied !!

I think my android app doesn't have related permission. However, I've added the following settings in AndroidManifest.xml:

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

And opencv functions are called in a thread in my app. My app is a service for android.

I checked the permission about /dev/videoX as below.

1|gts4lltechn:/dev $ ls -l | grep video ls: ./event-log-tags: Permission denied

crw-rw---- 1 system camera 81, 0 2017-04-01 08:37 video0

crw-rw---- 1 system camera 81, 1 2017-04-01 08:37 video1

crw-rw---- 1 system camera 81, 2 2017-04-01 08:37 video2

crw-rw---- 1 system camera 81, 3 2017-04-01 08:37 video3

crw-rw---- 1 system camera 81, 32 2017-04-01 08:37 video32

crw-rw---- 1 system camera 81, 33 2017-04-01 08:37 video33

crw-rw---- 1 system camera 81, 4 2017-04-01 08:37 video4

crw-rw---- 1 system camera 81, 5 2017-04-01 08:37 video5

crw-rw---- 1 system camera 81, 6 2018-12-20 21:28 video6

How to fix the permission issue for my app?

PS: Before app start, i open the permissions for Camera and Storage by setting.

标签: androidopencvpermissionscameranative

解决方案


如果您需要相机权限,您应该向用户询问实时权限。如果您不知道如何实现它们,请查看这个库


推荐阅读