1 #include "libuvc/libuvc.h"
4 /* This callback function runs once per frame. Use it to perform any
5 * quick processing you need, or have it put the frame into your application's
6 * input queue. If this function takes too long, you'll start losing frames. */
7 void cb(uvc_frame_t *frame, void *ptr) {
11 /* We'll convert the image from YUV/JPEG to BGR, so allocate space */
12 bgr = uvc_allocate_frame(frame->width * frame->height * 3);
14 printf("unable to allocate bgr frame!");
18 /* Do the BGR conversion */
19 ret = uvc_any2bgr(frame, bgr);
21 uvc_perror(ret, "uvc_any2bgr");
26 /* Call a user function:
28 * my_type *my_obj = (*my_type) ptr;
29 * my_user_function(ptr, bgr);
30 * my_other_function(ptr, bgr->data, bgr->width, bgr->height);
35 * my_type *my_obj = (*my_type) ptr;
36 * my_obj->my_func(bgr);
39 /* Use opencv.highgui to display the image:
41 * cvImg = cvCreateImageHeader(
42 * cvSize(bgr->width, bgr->height),
46 * cvSetData(cvImg, bgr->data, bgr->width * 3);
48 * cvNamedWindow("Test", CV_WINDOW_AUTOSIZE);
49 * cvShowImage("Test", cvImg);
52 * cvReleaseImageHeader(&cvImg);
58 int main(int argc, char **argv) {
61 uvc_device_handle_t *devh;
62 uvc_stream_ctrl_t ctrl;
65 /* Initialize a UVC service context. Libuvc will set up its own libusb
66 * context. Replace NULL with a libusb_context pointer to run libuvc
67 * from an existing libusb context. */
68 res = uvc_init(&ctx, NULL);
71 uvc_perror(res, "uvc_init");
75 puts("UVC initialized");
77 /* Locates the first attached UVC device, stores in dev */
78 res = uvc_find_device(
80 0, 0, NULL); /* filter devices: vendor_id, product_id, "serial_num" */
83 uvc_perror(res, "uvc_find_device"); /* no devices found */
87 /* Try to open the device: requires exclusive access */
88 res = uvc_open(dev, &devh);
91 uvc_perror(res, "uvc_open"); /* unable to open device */
93 puts("Device opened");
95 /* Print out a message containing all the information that libuvc
96 * knows about the device */
97 uvc_print_diag(devh, stderr);
99 /* Try to negotiate a 640x480 30 fps YUYV stream profile */
100 res = uvc_get_stream_ctrl_format_size(
101 devh, &ctrl, /* result stored in ctrl */
102 UVC_FRAME_FORMAT_YUYV, /* YUV 422, aka YUV 4:2:2. try _COMPRESSED */
103 640, 480, 30 /* width, height, fps */
106 /* Print out the result */
107 uvc_print_stream_ctrl(&ctrl, stderr);
110 uvc_perror(res, "get_mode"); /* device doesn't provide a matching stream */
112 /* Start the video stream in isochronous mode. The library will
113 * call user function cb: cb(frame, (void*) 12345)
115 res = uvc_start_iso_streaming(devh, &ctrl, cb, 12345);
118 uvc_perror(res, "start_streaming"); /* unable to start stream */
120 puts("Streaming...");
122 uvc_set_ae_mode(devh, 1); /* e.g., turn on auto exposure */
124 sleep(10); /* stream for 10 seconds */
126 /* End the stream. Blocks until last callback is serviced */
127 uvc_stop_streaming(devh);
128 puts("Done streaming.");
132 /* Release our handle on the device */
134 puts("Device closed");
137 /* Release the device descriptor */
138 uvc_unref_device(dev);
141 /* Close the UVC context. This closes and cleans up any existing device handles,
142 * and it closes the libusb context if one was not provided. */