libuvc介绍及简单使用
? ? ? 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
總結
以上是生活随笔為你收集整理的libuvc介绍及简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3中生成器介绍
- 下一篇: Python3中装饰器介绍