|
| 1 | +#include "libuvc/libuvc.h" |
| 2 | +#include <stdio.h> |
| 3 | + |
| 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) { |
| 8 | + uvc_frame_t *bgr; |
| 9 | + uvc_error_t ret; |
| 10 | + |
| 11 | + /* We'll convert the image from YUV/JPEG to BGR, so allocate space */ |
| 12 | + bgr = uvc_allocate_frame(frame->width * frame->height * 3); |
| 13 | + if (!bgr) { |
| 14 | + printf("unable to allocate bgr frame!"); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + /* Do the BGR conversion */ |
| 19 | + ret = uvc_any2bgr(frame, bgr); |
| 20 | + if (ret) { |
| 21 | + uvc_perror(ret, "uvc_any2bgr"); |
| 22 | + uvc_free_frame(bgr); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + /* Call a user function: |
| 27 | + * |
| 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); |
| 31 | + */ |
| 32 | + |
| 33 | + /* Call a C++ method: |
| 34 | + * |
| 35 | + * my_type *my_obj = (*my_type) ptr; |
| 36 | + * my_obj->my_func(bgr); |
| 37 | + */ |
| 38 | + |
| 39 | + /* Use opencv.highgui to display the image: |
| 40 | + * |
| 41 | + * cvImg = cvCreateImageHeader( |
| 42 | + * cvSize(bgr->width, bgr->height), |
| 43 | + * IPL_DEPTH_8U, |
| 44 | + * 3); |
| 45 | + * |
| 46 | + * cvSetData(cvImg, bgr->data, bgr->width * 3); |
| 47 | + * |
| 48 | + * cvNamedWindow("Test", CV_WINDOW_AUTOSIZE); |
| 49 | + * cvShowImage("Test", cvImg); |
| 50 | + * cvWaitKey(10); |
| 51 | + * |
| 52 | + * cvReleaseImageHeader(&cvImg); |
| 53 | + */ |
| 54 | + |
| 55 | + uvc_free_frame(bgr); |
| 56 | +} |
| 57 | + |
| 58 | +int main(int argc, char **argv) { |
| 59 | + uvc_context_t *ctx; |
| 60 | + uvc_device_t *dev; |
| 61 | + uvc_device_handle_t *devh; |
| 62 | + uvc_stream_ctrl_t ctrl; |
| 63 | + uvc_error_t res; |
| 64 | + |
| 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); |
| 69 | + |
| 70 | + if (res < 0) { |
| 71 | + uvc_perror(res, "uvc_init"); |
| 72 | + return res; |
| 73 | + } |
| 74 | + |
| 75 | + puts("UVC initialized"); |
| 76 | + |
| 77 | + /* Locates the first attached UVC device, stores in dev */ |
| 78 | + res = uvc_find_device( |
| 79 | + ctx, &dev, |
| 80 | + 0, 0, NULL); /* filter devices: vendor_id, product_id, "serial_num" */ |
| 81 | + |
| 82 | + if (res < 0) { |
| 83 | + uvc_perror(res, "uvc_find_device"); /* no devices found */ |
| 84 | + } else { |
| 85 | + puts("Device found"); |
| 86 | + |
| 87 | + /* Try to open the device: requires exclusive access */ |
| 88 | + res = uvc_open(dev, &devh); |
| 89 | + |
| 90 | + if (res < 0) { |
| 91 | + uvc_perror(res, "uvc_open"); /* unable to open device */ |
| 92 | + } else { |
| 93 | + puts("Device opened"); |
| 94 | + |
| 95 | + /* Print out a message containing all the information that libuvc |
| 96 | + * knows about the device */ |
| 97 | + uvc_print_diag(devh, stderr); |
| 98 | + |
| 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_COLOR_FORMAT_YUYV, /* YUV 422, aka YUV 4:2:2. try _COMPRESSED */ |
| 103 | + 640, 480, 30 /* width, height, fps */ |
| 104 | + ); |
| 105 | + |
| 106 | + /* Print out the result */ |
| 107 | + uvc_print_stream_ctrl(&ctrl, stderr); |
| 108 | + |
| 109 | + if (res < 0) { |
| 110 | + uvc_perror(res, "get_mode"); /* device doesn't provide a matching stream */ |
| 111 | + } else { |
| 112 | + /* Start the video stream in isochronous mode. The library will |
| 113 | + * call user function cb: cb(frame, (void*) 12345) |
| 114 | + */ |
| 115 | + res = uvc_start_iso_streaming(devh, &ctrl, cb, 12345); |
| 116 | + |
| 117 | + if (res < 0) { |
| 118 | + uvc_perror(res, "start_streaming"); /* unable to start stream */ |
| 119 | + } else { |
| 120 | + puts("Streaming..."); |
| 121 | + |
| 122 | + uvc_set_ae_mode(devh, 1); /* e.g., turn on auto exposure */ |
| 123 | + |
| 124 | + sleep(10); /* stream for 10 seconds */ |
| 125 | + |
| 126 | + /* End the stream. Blocks until last callback is serviced */ |
| 127 | + uvc_stop_streaming(devh); |
| 128 | + puts("Done streaming."); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* Release our handle on the device */ |
| 133 | + uvc_close(devh); |
| 134 | + puts("Device closed"); |
| 135 | + } |
| 136 | + |
| 137 | + /* Release the device descriptor */ |
| 138 | + uvc_unref_device(dev); |
| 139 | + } |
| 140 | + |
| 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. */ |
| 143 | + uvc_exit(ctx); |
| 144 | + puts("UVC exited"); |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
0 commit comments