安装cmake
打开cmake官网 https://cmake.org/download/,下载安装包:
安装时选择将cmake加到系统环境变量里。安装完成后在新的CMD命令窗口执行cmake --version可看到输出:
D:\>cmake --version
cmake version 3.29.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
安装Mingw64 GCC
Mingw目标是为支持Windows平台上的GCC编译,它主要提供头文件和支持库,Mingw自身不包括GCC和binutils,所以官网提供了集成这些组件的各种安装包。列表里有Linux平台的安装包,那些是用来在Linux平台生成Widows程序的。
https://www.mingw-w64.org/
这里我选择了w64devkit,点击后安装链接指向了github:https://github.com/skeeto/w64devkit/releases ,下载w64devkit-1.23.0.zip。
解压后放在C盘,将目录 C:\w64devkit\bin\ 加入系统PATH环境变量。
然后新打开的CMD命令窗口输入gcc –v 可以看到输出:
D:\>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/w64devkit/bin/../libexec/gcc/x86_64-w64-mingw32/14.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /gcc-14.1.0/configure --prefix=/w64devkit --with-sysroot=/w64devkit/x86_64-w64-mingw32 --with-native-system-header-dir=/include --target=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-static --disable-shared --with-pic --with-gmp-include=/deps/include --with-gmp-lib=/deps/lib --with-mpc-include=/deps/include --with-mpc-lib=/deps/lib --with-mpfr-include=/deps/include --with-mpfr-lib=/deps/lib --enable-languages=c,c++ --enable-libgomp --enable-threads=posix --enable-version-specific-runtime-libs --disable-dependency-tracking --disable-lto --disable-multilib --disable-nls --disable-win32-registry --enable-mingw-wildcard CFLAGS_FOR_TARGET=-Os CXXFLAGS_FOR_TARGET=-Os LDFLAGS_FOR_TARGET=-s CFLAGS=-Os CXXFLAGS=-Os LDFLAGS=-s
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.1.0 (GCC)
下载libusb
Libuvc依赖libusb,libusb是一个低级的通用访问USB设备的用户空间库。我们直接下载官网已经编译好的二进制库。
https://libusb.info/
点击图中Latest Windows Binaries下载 https://github.com/libusb/libusb/releases/download/v1.0.27/libusb-1.0.27.7z 解压后将 include\libusb.h, MinGW64下面的 .a 及 .dll 库提取到一个目录,如D:\libusb:
下载编译libuvc
https://github.com/libuvc/libuvc
从官网以git clone下载源码,源码所放位置为D:\libuvc
由于cmake最小版本过低会产生如下告警:
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.5 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
所以我们把libuvc/CmakeList.txt开头的cmake最低版本改为3.10
如果直接执行cmake .. 会提示找不到libusb,打开D:\libuvc\CmakeLists.txt,删除find_package(LibUSB)一行,替换为下面内容(目录分隔符这里用的是 /, 如果用 \ 分隔,转义的原因还要改为 \\):
# Locate the libusb-1.0 package manually if not found
#find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h PATH_SUFFIXES libusb-1.0)
#find_library(LIBUSB_LIBRARY NAMES usb-1.0 libusb-1.0)
set(LIBUSB_INCLUDE_DIR D:/libusb)
set(LIBUSB_LIBRARY D:/libusb/libusb-1.0.dll D:/libusb/libusb-1.0.dll.a)
#set(LIBUSB_LIBRARY D:/libusb/libusb-1.0.a) # for static link
if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARY)
set(LIBUSB_FOUND TRUE)
else()
set(LIBUSB_FOUND FALSE)
message(WARNING "LibUSB not found. Please install it.")
endif()
静态链接用lbusb-1.0.a就注释掉46行,打开47行, 这样会更方便一点,不会产生找不到dll的问题。
然后在130行左右添加libusb.h头文件目录和链接库,
打开CMD窗口,执行下面命令:
cd D:\libuvc
mkdir build install
cd build
rm –fr *; #如果cmake有错,重新执行时建议把目录清空。
cmake -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=D:\libuvc\install -DBUILD_SHARED_LIBS=ON ..
第一次执行cmake报错, 系统会报病毒拦截,再执行一次上面的cmake 命令即可。 不确定是误报还是真有病毒。
CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCompilerId.cmake:922 (file):
file STRINGS file "D:/libuvc/build/CMakeFiles/3.29.3/CompilerIdC/a.exe"
cannot be read.
执行cmake成功后生成Makefie,接下来执行make命令进行编译。编译过程中会提示ARRAYSIZE重复定义的警告:
D:/libuvc/include/libuvc/libuvc_internal.h:75:9: warning: "ARRAYSIZE" redefined
75 | #define ARRAYSIZE(arr) (sizeof(arr) / (IS_ARRAY(arr) ? sizeof(arr[0]) : 0))
| ^~~~~~~~~
In file included from C:/w64devkit/x86_64-w64-mingw32/include/minwindef.h:163,
from C:/w64devkit/x86_64-w64-mingw32/include/windef.h:9,
from C:/w64devkit/x86_64-w64-mingw32/include/windows.h:69,
from D:/libusb/libusb.h:64,
from D:/libuvc/include/libuvc/libuvc_internal.h:14:
C:/w64devkit/x86_64-w64-mingw32/include/winnt.h:681:
note: this is the location of the previous definition
681 | #define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A)
如果不想看到太多输出警告信息,可以编辑D:\libuvc\include\libuvc\libuvc_internal.h, 在ARRAYSIZE外面加上#ifndef ARRAYSIZE 进行防御。
编译成功:
D:\libuvc\build>make
[ 5%] Building C object CMakeFiles/uvc.dir/src/ctrl.c.obj
[ 10%] Building C object CMakeFiles/uvc.dir/src/ctrl-gen.c.obj
[ 15%] Building C object CMakeFiles/uvc.dir/src/device.c.obj
[ 20%] Building C object CMakeFiles/uvc.dir/src/diag.c.obj
[ 25%] Building C object CMakeFiles/uvc.dir/src/frame.c.obj
[ 30%] Building C object CMakeFiles/uvc.dir/src/init.c.obj
[ 35%] Building C object CMakeFiles/uvc.dir/src/stream.c.obj
[ 40%] Building C object CMakeFiles/uvc.dir/src/misc.c.obj
[ 45%] Linking C shared library libuvc.dll
[ 45%] Built target uvc
[ 50%] Building C object CMakeFiles/uvc_static.dir/src/ctrl.c.obj
[ 55%] Building C object CMakeFiles/uvc_static.dir/src/ctrl-gen.c.obj
[ 60%] Building C object CMakeFiles/uvc_static.dir/src/device.c.obj
[ 65%] Building C object CMakeFiles/uvc_static.dir/src/diag.c.obj
[ 70%] Building C object CMakeFiles/uvc_static.dir/src/frame.c.obj
[ 75%] Building C object CMakeFiles/uvc_static.dir/src/init.c.obj
[ 80%] Building C object CMakeFiles/uvc_static.dir/src/stream.c.obj
[ 85%] Building C object CMakeFiles/uvc_static.dir/src/misc.c.obj
[ 90%] Linking C static library libuvc.a
[ 90%] Built target uvc_static
[ 95%] Building C object CMakeFiles/example.dir/src/example.c.obj
[100%] Linking C executable example.exe
[100%] Built target example
然后安装到install目录,
D:\libuvc\build>make install
[ 45%] Built target uvc
[ 90%] Built target uvc_static
[100%] Built target example
Install the project...
-- Install configuration: "Release"
-- Installing: D:/libuvc/install/lib/libuvc.dll.a
-- Installing: D:/libuvc/install/bin/libuvc.dll
-- Installing: D:/libuvc/install/include/libuvc/libuvc.h
-- Installing: D:/libuvc/install/include/libuvc/libuvc_config.h
-- Installing: D:/libuvc/install/lib/libuvc.a
-- Up-to-date: D:/libuvc/install/include/libuvc/libuvc.h
-- Up-to-date: D:/libuvc/install/include/libuvc/libuvc_config.h
-- Installing: D:/libuvc/install/lib/cmake/libuvc/libuvcTargets.cmake
-- Installing: D:/libuvc/install/lib/cmake/libuvc/libuvcTargets-release.cmake
-- Installing: D:/libuvc/install/lib/cmake/libuvc/FindLibUSB.cmake
-- Installing: D:/libuvc/install/lib/cmake/libuvc/FindJpegPkg.cmake
-- Installing: D:/libuvc/install/lib/cmake/libuvc/libuvcConfigVersion.cmake
-- Installing: D:/libuvc/install/lib/pkgconfig/libuvc.pc
-- Installing: D:/libuvc/install/lib/cmake/libuvc/libuvcConfig.cmake
测试
以example.exe来进行测试。
首先利用USBDeview (https://usbdeview.me)工具找到电脑上外置UVC摄像头的VID和PID。
然后打开D:\libuvc\src\example.c, 修改其中的VID和PID:
重新在build目录执行make,然后执行example.exe, 会提示找不到libusb-1.0.dll,可以将D:\libusb-1.0.dll复制一份放在example.exe同一个目录。 或者一劳永逸的复制到C:\Windows\System32下。 如果给别人发编译好的程序,需要带上libusb-1.0.dll。若前面Cmake配置静态链接libusb则没有此麻烦。
解决完dll问题后,example跑起来会报错,找不到设备:
D:\libuvc\build>example.exe
UVC initialized
Device found
uvc_open: Not found (-5)
UVC exited
先用USBDeview查看驱动,此UVC设备使用了usbvideo驱动,需要安装WinUSB驱动。
https://learn.microsoft.com/zh-cn/windows-hardware/drivers/usbcon/winusb-installation 面向开发人员的 WinUSB (Winusb.sys) 安装 文档中描述到:
对于某些通用串行总线 (USB) 设备(例如仅由单个应用程序访问的设备),可以在设备的内核模式堆栈中安装 WinUSB (Winusb.sys) 作为 USB 设备的功能驱动程序,而不是实现驱动程序。libusb正是此种场景。可以认为WinUSB.sys驱动是一种低层次的驱动,libusb要跟低层的驱动打交道,usbvideo.sys驱动是高层次的驱动。可以31按链接中微软官方的指导安装WinUSB驱动,也可以用Zadig来安装此驱动。
从 https://zadig.akeo.ie下载Zadig。由于在此电脑上,USB Camera(Interface 0)是Video类型,USB Camera(Interface 2)是Audio类型, 所以在从Options菜单中选择List All Devices后选择USB Camera(Interface 0),换用WinUSB驱动。
安装WinUSB驱动后用USBDeview查看:
example跑成功的日志:
D:\libuvc\build>example.exe
UVC initialized
Device found
Device opened
DEVICE CONFIGURATION (b349:b182/[none]) ---
Status: idle
VideoControl:
bcdUVC: 0x0100
VideoStreaming(1):
bEndpointAddress: 129
Formats:
UncompressedFormat(1)
bits per pixel: 16
GUID: 5955593200001000800000aa00389b71 (YUY2)
default frame: 1
aspect ratio: 0x0
interlace flags: 00
copy protect: 00
FrameDescriptor(1)
capabilities: 01
size: 1600x1200
bit rate: 153600000-153600000
max frame size: 3840000
default interval: 1/5
interval[0]: 1/5
FrameDescriptor(2)
capabilities: 01
size: 1280x720
bit rate: 110592000-110592000
max frame size: 1843200
default interval: 1/7
interval[0]: 1/7
FrameDescriptor(3)
capabilities: 01
size: 640x480
bit rate: 147456000-147456000
max frame size: 614400
default interval: 1/30
interval[0]: 1/30
MJPEGFormat(2)
bits per pixel: 0
GUID: 4d4a5047000000000000000000000000 (MJPG)
default frame: 1
aspect ratio: 0x0
interlace flags: 00
copy protect: 00
FrameDescriptor(1)
capabilities: 01
size: 1600x1200
bit rate: 921600000-921600000
max frame size: 3840000
default interval: 1/30
interval[0]: 1/30
FrameDescriptor(2)
capabilities: 01
size: 1280x720
bit rate: 442368000-442368000
max frame size: 1843200
default interval: 1/30
interval[0]: 1/30
FrameDescriptor(3)
capabilities: 01
size: 640x480
bit rate: 147456000-147456000
max frame size: 614400
default interval: 1/30
interval[0]: 1/30
END DEVICE CONFIGURATION
First format: (YUY2) 1600x1200 5fps
bmHint: 0001
bFormatIndex: 1
bFrameIndex: 1
dwFrameInterval: 2000000
wKeyFrameRate: 0
wPFrameRate: 0
wCompQuality: 61
wCompWindowSize: 0
wDelay: 0
dwMaxVideoFrameSize: 3840000
dwMaxPayloadTransferSize: 3060
bInterfaceNumber: 1
Streaming...
Enabling auto exposure ...
... full AE not supported, trying aperture priority mode
... enabled aperture priority auto exposure mode
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
callback! frame_format = 3, width = 1600, height = 1200, length = 3840000, ptr = 0000000000003039
Done streaming.
Device closed
UVC exited
恢复驱动
安装过WinUSB驱动的UVC设备,不会出现在设备管理器的照像机下面。 如果要恢复正常的UVC Camera功能,可以在设备管理器中卸载此设备的驱动。
卸载驱动后在设备管理器中右击鼠标,扫描检测硬件改动,USB Camera可以自动识别出来。驱动也恢复为原来的usbvideo.sys。