如何使用两个网络摄像头使用 Zbar 库扫描二维码?
void *scanning_f(void *ptr)
{
const char *first_cam = "/dev/video0";
const char *second_cam = "/dev/video2";
zbar_processor_t *proc = zbar_processor_create(1);
zbar_processor_set_config(proc, 0, ZBAR_CFG_ENABLE, 1);
zbar_processor_request_size(proc, 1024, 860);
zbar_processor_init(proc, first_cam, 0);
zbar_processor_set_data_handler(proc, handling, NULL);
zbar_processor_set_visible(proc, 0);
zbar_processor_set_active(proc, 1);
zbar_processor_t *proc2 = zbar_processor_create(1);
zbar_processor_set_config(proc2, 0, ZBAR_CFG_ENABLE, 1);
zbar_processor_request_size(proc2, 1024, 860);
zbar_processor_init(proc2, second_cam, 0);
zbar_processor_set_data_handler(proc2, handling, NULL);
zbar_processor_set_visible(proc2, 0);
zbar_processor_set_active(proc2, 1);
while(1)
{
zbar_process_one(proc, 1000);
zbar_process_one(proc2, 1000);
}
}
这个功能
zbar_processor_set_active(proc, 1);
似乎将读数锁定到第一个激活的相机。即使两者都工作(LED 亮起)。
这确保两个摄像头可以同时扫描二维码:
#include <pthread.h>
#include <zbar.h>
void handling(zbar_image_t *image); // Define your handling function
typedef struct {
const char *cam_device;
} camera_info;
void *scanning_f(void *ptr) {
camera_info *cam_info = (camera_info *)ptr;
const char *cam_device = cam_info->cam_device;
zbar_processor_t *proc = zbar_processor_create(1);
zbar_processor_set_config(proc, 0, ZBAR_CFG_ENABLE, 1);
zbar_processor_request_size(proc, 1024, 860);
zbar_processor_init(proc, cam_device, 0);
zbar_processor_set_data_handler(proc, handling, NULL);
zbar_processor_set_visible(proc, 0);
zbar_processor_set_active(proc, 1);
while (1) {
zbar_process_one(proc, 1000);
}
zbar_processor_destroy(proc);
pthread_exit(NULL);
}
int main() {
pthread_t thread1, thread2;
camera_info cam1 = {"/dev/video0"};
camera_info cam2 = {"/dev/video2"};
pthread_create(&thread1, NULL, scanning_f, &cam1);
pthread_create(&thread2, NULL, scanning_f, &cam2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}