解决Android USB摄像头热擦拔问题

usb到camera本来是支持热插拔的,但是由于正在preview时拔出camera,再插入时,camera注册到设备节点会由/dev/video0变为/dev/video1,或者插入多个video设备时,会变为/dev/video1、/dev/video2.。。。。。。而HAL层的代码是固定打开/dev/video0,所以就会打开失败。


方法1

对video0到video63循环打开。

  1. int i;
  2. char* devname= "/dev/video";
  3. char s[ 16];
  4. char index[ 10];
  5. for( i= 0; i<= 63; i++)
  6.                 sprintf(index, "%d",i); //将int格式化为char
  7.                 sprintf(s, "%s%s",devname,index);
  8. LOGW( "devname=%s",s);
  9. if ((mCameraHandle = open(s, O_RDWR)) != -1)
  10. break;
  11. }
  12. if(i== 64)
  13. {
  14. LOGINFO( "Error while opening handle to V4L2 Camera: %s", strerror(errno));
  15. return -EINVAL;
  16. }


方法2 使用popen调用shell命令

  1. FILE * fp;
  2. char name[ 12]; //只能video0到video9
  3.         fp=popen( "ls /dev/v*", "r");
  4.         fgets(name, sizeof(name),fp); 
  5. pclose(fp); 
  6. if ((mCameraHandle = open(name, O_RDWR)) == -1)
  7. {
  8. LOGINFO( "Error while opening handle to V4L2 Camera: %s", strerror(errno));
  9. return -EINVAL;
  10. }

FILE *popen(const char *command, const char *type);
调用fork产生一个子进程,建立管道连接到子进程的标准输出或输入设备,然后在子进程中调用shell命令,shell的输出可以采用读取文件的方式获得。

参数type可使用“r”表示读取,“w”表示写入

pclose则用于使用结束后关闭这个指针。


调用shell命令也可以用system函数,但是不能返回shell命令到结果。




有两个地方都进行来打开设备操作

vendor/marvell/generic/libcamera/V4LCameraAdapter.cpp   V4LCameraAdapter::initialize()

vendor/marvell/generic/libcamera/CameraProperties.cpp    CameraProperties::loadProperties()


vendor/marvell/berlin_bg2_freud/88DE3100_FILES/prebuild/ics/rootdir/ueventd.rc 中

/dev/video0               0666   root       root

/dev/video1               0666   root       root

。。。。。



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值