Camera

From FriendlyELEC WiKi
Revision as of 06:57, 14 August 2018 by Tzs (Talk | contribs) (Created page with "==查询摄像头设备文件名== <syntaxhighlight lang="c"> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/videodev.h> int...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

查询摄像头设备文件名

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>
 
int is_camera(const char* filename){
        FILE* f = fopen( filename, "r" );
        if (f == NULL) {
                return 0;
        }
        char buff[256] = {0};
        int ret = 0;
 
        if (fread(buff, 1, 255, f) <= 0) {
                fclose(f);
                return 0;
        }
        fclose( f );
        if (strstr(buff, "Camera") == NULL || strstr(buff, "UVC") == NULL) {
                return 1;
        }
        return 0;
}
 
int main() {
    int i;
    char filename[255];
    for (i=0; i<20; i++) {
        sprintf(filename, "/sys/class/video4linux/video%d/name", i);
        if (is_camera(filename) == 1) {
            printf("/dev/video%d is a camera.\n", i);
            break;
        }
    }
}