Last active
February 25, 2020 04:53
-
-
Save saccadic/1b76796d9e20d5217be102cfedb1841f to your computer and use it in GitHub Desktop.
LinuxでカメラのIDと名前を取得するコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Author : Katsuyoshi Hotta | |
| // sudo apt-get install libudev | |
| // udevadm info --query=all --name=/dev/video0 | |
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <libudev.h> | |
| int main() | |
| { | |
| struct udev *udev; | |
| /* create udev object */ | |
| udev = udev_new(); | |
| if (!udev) { | |
| fprintf(stderr, "Can't create udev\n"); | |
| return 1; | |
| } | |
| /* Create a list of the devices in the 'video4linux' subsystem. */ | |
| struct udev_enumerate* enumerate = udev_enumerate_new(udev); | |
| udev_enumerate_scan_devices(enumerate); | |
| struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
| struct udev_list_entry* dev_list_entry; | |
| //Device ID | |
| static int device_id = 0; | |
| //Search camera devices | |
| udev_list_entry_foreach(dev_list_entry, devices) | |
| { | |
| const char * name = udev_list_entry_get_name(dev_list_entry); | |
| struct udev_device * device = udev_device_new_from_syspath(udev, name); | |
| const char * subsystem = udev_device_get_subsystem(device); | |
| if (strcmp(subsystem, "video4linux") == 0) { | |
| // const char * vendor_id; | |
| // const char * product_id; | |
| // const char * serial_id; | |
| const char * device_name; | |
| //Search camera properties | |
| struct udev_list_entry * properties = udev_device_get_properties_list_entry(device); | |
| struct udev_list_entry * property; | |
| udev_list_entry_foreach(property, properties) { | |
| //Get camera propertiy name | |
| const char * name = udev_list_entry_get_name(property); | |
| // if(strcmp(name,"ID_VENDOR_ID")==0){ | |
| // vendor_id = udev_list_entry_get_value(property); | |
| // printf("%s\n",vendor_id); | |
| // } | |
| // if(strcmp(name,"ID_MODEL_ID")==0){ | |
| // product_id = udev_list_entry_get_value(property); | |
| // printf("%s\n",product_id); | |
| // } | |
| // if(strcmp(name,"ID_SERIAL")==0){ | |
| // serial_id = udev_list_entry_get_value(property); | |
| // printf("%s\n",serial_id); | |
| // } | |
| //Get camera name | |
| if (strcmp(name, "ID_V4L_PRODUCT") == 0) { | |
| device_name = udev_list_entry_get_value(property); | |
| } | |
| } | |
| printf("%d : %s\n", device_id, device_name); | |
| printf("---\n"); | |
| device_id++; | |
| } | |
| } | |
| /* Free the enumerator object */ | |
| udev_enumerate_unref(enumerate); | |
| udev_unref(udev); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment