libuvc介绍及简单使用

article/2025/10/30 0:36:01

      libuvc是一个用于USB视频设备的跨平台库,构建在libusb之上,编译libuvc时需要依赖libusb。libuvc的License为BSD,最新发布版本为0.0.6,源码地址: https://github.com/libuvc/libuvc

      libuvc支持在非windows系统上直接编译,因为libuvc源码中会include pthread.h等头文件,因此在windows上并不能直接进行编译。

      libuvc支持对导出标准USB视频类(USB Video Class, UVC)接口的USB视频设备进行细粒度控制,使开发人员能够为以前不受支持的设备编写驱动程序,或仅以通用方式访问UVC设备。

      关于libusb的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/105712776

      在linux上插入同一厂家两个相同型号的摄像头时,通过lsusb命令获取的两个摄像头设备信息是一样的,不能做出区分,此时可以通过libuvc来获取摄像头设备较为详细的信息,从此信息可以分辨出具体是从哪个摄像头上获取到的视频数据。

      以下是测试代码,参考 https://ken.tossell.net/libuvc/doc/

namespace {
void cb(uvc_frame_t* frame, void* ptr)
{// We'll convert the image from YUV/JPEG to BGR, so allocate spaceuvc_frame_t* bgr = uvc_allocate_frame(frame->width * frame->height * 3);if (!bgr) {printf("unable to allocate bgr frame!\n");return;}// Do the BGR conversionuvc_error_t ret = uvc_any2bgr(frame, bgr);if (ret) {uvc_perror(ret, "uvc_any2bgr");uvc_free_frame(bgr);return;}/* Call a user function:** my_type *my_obj = (*my_type) ptr;* my_user_function(ptr, bgr);* my_other_function(ptr, bgr->data, bgr->width, bgr->height);*//* Call a C++ method:** my_type *my_obj = (*my_type) ptr;* my_obj->my_func(bgr);*//* Use opencv.highgui to display the image:** cvImg = cvCreateImageHeader(*     cvSize(bgr->width, bgr->height),*     IPL_DEPTH_8U,*     3);** cvSetData(cvImg, bgr->data, bgr->width * 3);** cvNamedWindow("Test", CV_WINDOW_AUTOSIZE);* cvShowImage("Test", cvImg);* cvWaitKey(10);** cvReleaseImageHeader(&cvImg);*/uvc_free_frame(bgr);
}} // namespaceint test_libuvc_get_webcam_info()
{// reference: https://ken.tossell.net/libuvc/doc/// Initialize a UVC service context. Libuvc will set up its own libusb context.// Replace NULL with a libusb_context pointer to run libuvc from an existing libusb context.uvc_context_t* ctx = nullptr;uvc_error_t res = uvc_init(&ctx, nullptr);if (res < 0) {uvc_perror(res, "uvc_init");return res;}fprintf(stdout, "UVC initialized\n");// Locates the first attached UVC device, stores in devuvc_device_t* dev = nullptr;uvc_device_handle_t* devh = nullptr;res = uvc_find_device(ctx, &dev, 0, 0, nullptr); // filter devices: vendor_id, product_id, "serial_num"if (res < 0) {uvc_perror(res, "uvc_find_device"); // no devices found} else {fprintf(stdout, "Device found\n");// Try to open the device: requires exclusive accessres = uvc_open(dev, &devh);if (res < 0) {uvc_perror(res, "uvc_open"); // unable to open device} else {fprintf(stdout, "Device opened\n");// Print out a message containing all the information that libuvc knows about the deviceuvc_print_diag(devh, stderr);// Try to negotiate a 640x480 30 fps YUYV stream profileuvc_stream_ctrl_t ctrl;res = uvc_get_stream_ctrl_format_size(devh, &ctrl, /* result stored in ctrl */UVC_FRAME_FORMAT_YUYV, /* YUV 422, aka YUV 4:2:2. try _COMPRESSED */640, 480, 30 /* width, height, fps */);// Print out the resultuvc_print_stream_ctrl(&ctrl, stderr);if (res < 0) {uvc_perror(res, "get_mode"); // device doesn't provide a matching streamreturn res;} else {// Start the video stream. The library will call user function cb: cb(frame, (void*) 12345)void* user_ptr = nullptr;res = uvc_start_streaming(devh, &ctrl, cb, user_ptr, 0);if (res < 0) {uvc_perror(res, "start_streaming"); // unable to start stream} else {fprintf(stdout, "Streaming...\n");uvc_set_ae_mode(devh, 1); // e.g., turn on auto exposurestd::this_thread::sleep_for(std::chrono::seconds(1)); // stream for 1 seconds// End the stream. Blocks until last callback is serviceduvc_stop_streaming(devh);fprintf(stdout, "Done streaming.\n");}}// Release our handle on the deviceuvc_close(devh);fprintf(stdout, "Device closed\n");}// Release the device descriptoruvc_unref_device(dev);}// Close the UVC context. This closes and cleans up any existing device handles,// and it closes the libusb context if one was not provided.uvc_exit(ctx);fprintf(stdout, "UVC exited\n");return 0;
}

      执行结果如下:

 

     GitHub:https://github.com/fengbingchun/OpenCV_Test


http://chatgpt.dhexx.cn/article/4aXIcsWN.shtml

相关文章

linux下封装函数库——动态库.so和静态库.a(代码实现及链接方式)

在linux环境下的链接库分为静态链接库&#xff08;.a库&#xff09;和动态链接库&#xff08;.so库&#xff09;&#xff0c;其作用是把C程序编译好做成一种可执行链接文件&#xff0c;主程序文件调用这些程序的函数接口是可以使用a库或so库&#xff0c;在主程序中只需要includ…

Hyperledger fabric应用的多机部署(自动化一键部署)

前面关于fabric部署的介绍都是基于单机环境下的&#xff0c;实际生产环境中一般会根据应用场景将节点分开部署在多台物理机上&#xff0c;面临的难题主要是不同主机间的节点如何通过网络进行通信。文章最后会分享一键完成多机增加组织的自动化部署脚本。 前言 这里仍然以balan…

Linux系统编程makefile制作动态库和静态库

目录 制作动态库 制作静态库 首先准备简单的add.c&#xff0c;sub.c,main.c,head.h.具体代码如下 #head.h文件 int Add(int a, int b); int Sub(int a, int b);#add.c文件 #include <stdio.h> int Add(int a, int b) {return a b; }#sub.c文件 #include <stdio.h&…

Linux动态库的下载与配置(以libevent库为例)

** Linux动态库的下载与配置 ** 本章以下载、安装、配置libevent库为例(安装libevent的前提是已经成功安装openssl库) 一、libevent的下载 https://libevent.org/ //官网下载源码包如果学习下载1.0版本为好&#xff0c;使用可以下载2.0版本 1.如果Linux已经进行桥接联网&am…

Linux下编译安装libusb动态库(.so) - libus1-0 vs libusb-0.1

最近在调试代码的过程中&#xff0c;发现libusb库中的一些方法没办法debug到&#xff0c;所以试着下载了一版源码&#xff0c;编译安装到指定的目录。这样&#xff0c;在工程的pro文件中&#xff0c;直接指定库和头文件的目录就可以引用自己编译的libusb库了。 在网上查了相关的…

linux编译生成动态库、静态库,以及使用

一、介绍 在实际开发过程中&#xff0c;当代码的文件较多&#xff0c;可以将一部分代码编译成动态库或者静态库然后再加载到程序中使用 编译过程 1、预编译 2、编译 3、汇编 4、链接 静态库和动态库的差异 1、链接静态库简单理解就是复制目标代码嵌入可执行文件中 2、动态库是…

Linux·libusb源码编译

libusb系列--Linux下libusb源码编译​​ ​ ​源码下载及解压​​ ​​下载源码​​ ​​解压下载的源码压缩包​​ ​​打开终端​​ ​​准备编译环境​​ ​​安装make dh-autoreconf​​ ​​安装 libudev-dev​​ sudo ​./autogen.sh​ ​​​​sudo ./configure --prefi…

ESP32如何用makefile直接编译生成.a静态库

目录 编译痛点前提环境编译准备makefile代码编译痛点 开发过esp系列的攻城狮们都知道,esp32的sdk包含了编译器、组件代码和例程代码,编译都是基于cmake的,都是整体一起编译,CMakeList.txt,component.mk,project.mk等等一系列和编译相关的文件,如果你想要自己编译一个自…

Linux 环境下的静态库生成与使用

目录 一&#xff0c;简介 二&#xff0c;如何生成静态链接库 1.准备测试程序 三&#xff0c;如何使用静态链接库 1.生成静态链接库 2.使用静态链接库 一&#xff0c;简介 这里我们只讲 Linux 环境下的静态库与动态库的生成与使用&#xff0c; Windows直接用VS直接就能生成…

Linux 下libusb编译与生成动态链接库

一、前言 上一篇文章提到了怎样使用Ubuntu 安装libusb&#xff0c;忘了如何将如何编译。 二、libusb使用GCC 编译 正常来讲&#xff0c;使用 gccc xxx.c -o xxx -I/usr/include -lusb-1.0 就可以了&#xff0c;如下图&#xff1a; 但是&#xff0c;当使用Ubuntu 12.04 使用…

关于libusb开源库的使用

关于libusb开源库的使用 文章目录 关于libusb开源库的使用1. 概述1.1 介绍1.2 用法 2. API接口2.1 分类2.2 初始化/反初始化2.3 获取设备2.4 打开/关闭设备2.5 根据ID打开设备2.6 描述符相关函数2.6.1 获得设备描述符2.6.2 获得/释放配置描述符 2.7 detach/attach驱动2.7.1 两种…

【Linux】 自动化编译工具make

目录 一&#xff1a;make工具 二&#xff1a;Makefile 三&#xff1a;Makefile基本规则 四&#xff1a;make是如何工作的 五&#xff1a;示例(五个阶段的Makefile) 5.1 第一阶段Makefile 5.2 第二阶段Makefile 5.3 第三阶段Makefile 5.4 第四阶段Makefile 5.5 第五阶…

【Linux操作系统】动态库,静态库的制作和使用,软链接、硬链接简介

目录 软硬链接咋创建一个软链接&#xff1f;咋创建一个软链接&#xff1f; 文件的三个时间动静态库制作静态库静态库的使用制作动态库动态库的使用 秃头侠们好呀&#xff0c;今天来说 软硬连接&#xff0c;动静态库 软硬链接 咋创建一个软链接&#xff1f; ln -s log.txt log…

【Linux】零基础学习动静态库打包

文章目录 一、软硬链接二、动静态库理解动静态库静态库的打包使用静态库打包Makefile编写动态库的打包使用动态库打包Makefile编写对于小版本的理解 总结 一、软硬链接 站在先前的讲过的文件系统的角度来看&#xff0c;下图test这个文件有自己独立的inode&#xff0c;在他的ino…

Linux | 动静态库 | 动静态链接 | makefile库打包 | 第三方库使用

文章目录 何为动静态库库文件的链接静态链接静态库打包动态链接动态库打包 第三方库的使用静态库的使用动态库的使用 在系统层面上的动态链接理解 何为动静态库 静态库(.a)&#xff1a;在程序编译链接时将静态库二进制码拷贝到程序代码中&#xff0c;程序运行时不再需要外部的静…

覆盖libc.so.6的惨痛教训

覆盖libc.so.6的惨痛教训 背景问题原因解决1、当前session未断开2、OS崩溃重启&#xff0c;所有ssh session断开 惨痛教训1、对于上产环境的内核依赖库文件不能随意覆盖、删除。2、 scp 文件覆盖问题 总结参考 背景 发生时间: 2022年11月28日08:55:20 偷了个懒&#xff0c;在安…

【Linux】自动生成makefile(ubuntu)

文章目录 前言一、Automake工具二、具体步骤1.下载Automake2.autoscan3.重命名configure.scan为configure.ac4.修改重命名后的configure.ac5.执行aclocal命令6.autoheader7.autoconf8.创建Makefile.am9.automake10.执行configure11.执行make12.执行Makefile编译完生成的可执行文…

【Linux】基础IO —— 下(实现动静态库)

&#x1f387;Linux&#xff1a;基础IO 博客主页&#xff1a;一起去看日落吗分享博主的在Linux中学习到的知识和遇到的问题博主的能力有限&#xff0c;出现错误希望大家不吝赐教分享给大家一句我很喜欢的话&#xff1a; 看似不起波澜的日复一日&#xff0c;一定会在某一天让你看…

【Linux】第九章 动态库和静态库(生成原理+生成和使用+动态链接)

&#x1f3c6;个人主页&#xff1a;企鹅不叫的博客 ​ &#x1f308;专栏 C语言初阶和进阶C项目Leetcode刷题初阶数据结构与算法C初阶和进阶《深入理解计算机操作系统》《高质量C/C编程》Linux ⭐️ 博主码云gitee链接&#xff1a;代码仓库地址 ⚡若有帮助可以【关注点赞收藏】…

Linux——自动化编译(make的使用)、库文件(静态库和共享库的使用)

一、make的使用&#xff08;自动化编译&#xff09; make是Linux上的工程管理工具&#xff0c;可以实现自动化编译&#xff1b;make可以提高我们的编译效率。 1.安装make 命令&#xff1a;sudo apt install make 2.make的用法 &#xff08;1&#xff09;创建makefile文件&am…