android - 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.
解决方案
推荐阅读
- apache-spark - 将源文件存储在 Google dataproc HDFS 与谷歌云存储(谷歌存储桶)中
- java - 我怎样才能让我的复选框自己检查
- sql-server - 代码可以在一台计算机上运行 - 但不能在其他计算机上运行
- rest - 使用 POSTMAN 或任何其他方式批量 POST/PUT API 请求
- c# - 如何阻止 XmlSerializer 发出集合的空标记?
- tensorflow - TensorFlow 自动混合精度 fp16 比官方 resnet 上的 fp32 慢
- scope - 为什么在交互式 julia 会话中仍然失败本地/全局范围?
- excel - 如何访问工作表中的所有红色行?
- python - 在列表的循环中初始化类实例
- python - 如何使用 pycuda 进行一维 fft?