1 /*********************************************************************
2 * Software License Agreement (BSD License)
4 * Copyright (C) 2010-2012 Ken Tossell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the author nor other contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
35 #include <opencv/highgui.h>
37 #include "libuvc/libuvc.h"
39 void cb(uvc_frame_t *frame, void *ptr) {
44 printf("callback! length = %u, ptr = %d\n", frame->data_bytes, (int) ptr);
46 bgr = uvc_allocate_frame(frame->width * frame->height * 3);
48 printf("unable to allocate bgr frame!");
52 ret = uvc_any2bgr(frame, bgr);
54 uvc_perror(ret, "uvc_any2bgr");
59 cvImg = cvCreateImageHeader(
60 cvSize(bgr->width, bgr->height),
64 cvSetData(cvImg, bgr->data, bgr->width * 3);
66 cvNamedWindow("Test", CV_WINDOW_AUTOSIZE);
67 cvShowImage("Test", cvImg);
70 cvReleaseImageHeader(&cvImg);
75 int main(int argc, char **argv) {
79 uvc_device_handle_t *devh;
80 uvc_stream_ctrl_t ctrl;
82 res = uvc_init(&ctx, NULL);
85 uvc_perror(res, "uvc_init");
89 puts("UVC initialized");
91 res = uvc_find_device(
96 uvc_perror(res, "uvc_find_device");
100 res = uvc_open(dev, &devh);
103 uvc_perror(res, "uvc_open");
105 puts("Device opened");
107 uvc_print_diag(devh, stderr);
109 res = uvc_get_stream_ctrl_format_size(
110 devh, &ctrl, UVC_FRAME_FORMAT_YUYV, 640, 480, 30
113 uvc_print_stream_ctrl(&ctrl, stderr);
116 uvc_perror(res, "get_mode");
118 res = uvc_start_streaming(devh, &ctrl, cb, 12345, 0);
121 uvc_perror(res, "start_streaming");
123 puts("Streaming for 10 seconds...");
124 uvc_error_t resAEMODE = uvc_set_ae_mode(devh, 1);
125 uvc_perror(resAEMODE, "set_ae_mode");
127 for (i = 1; i <= 10; i++) {
128 /* uvc_error_t resPT = uvc_set_pantilt_abs(devh, i * 20 * 3600, 0); */
129 /* uvc_perror(resPT, "set_pt_abs"); */
130 uvc_error_t resEXP = uvc_set_exposure_abs(devh, 20 + i * 5);
131 uvc_perror(resEXP, "set_exp_abs");
136 uvc_stop_streaming(devh);
137 puts("Done streaming.");
142 puts("Device closed");
145 uvc_unref_device(dev);