12 #define CLEAR(x) memset (&(x), 0, sizeof (x)) 17 unsigned char *src,
int length,
20 cv::Mat temp=cv::imdecode(cv::Mat(std::vector<uchar>(src, src + length)), 1);
21 if( !temp.data || temp.cols != width || temp.rows != height )
23 memcpy(dst, temp.data, width*height*3);
31 sprintf(dev_name,
"/dev/video%1d", camera_index);
32 fprintf(stderr,
"Opening device '%s'\n", dev_name);
38 fd = open(dev_name, O_RDWR, 0);
41 fprintf(stderr,
"Cannot open '%s': %d, %s\n",
42 dev_name, errno, strerror(errno));
47 struct v4l2_capability cap;
48 struct v4l2_format fmt;
50 if (ioctl(
fd, VIDIOC_QUERYCAP, &cap) == -1) {
51 if (EINVAL == errno) {
52 fprintf(stderr,
"%s is no V4L2 device\n", dev_name);
54 perror(
"VIDIOC_QUERYCAP");
58 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
59 fprintf(stderr,
"%s is no video capture device\n", dev_name);
63 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
64 fprintf(stderr,
"%s does not support streaming i/o\n", dev_name);
70 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
71 fmt.fmt.pix.width =
width;
72 fmt.fmt.pix.height =
height;
73 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
74 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
76 if (ioctl(
fd, VIDIOC_S_FMT, &fmt) == -1) {
77 perror(
"VIDIOC_S_FMT");
82 struct v4l2_requestbuffers req;
87 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
88 req.memory = V4L2_MEMORY_MMAP;
90 if (ioctl(
fd, VIDIOC_REQBUFS, &req) == -1) {
91 perror(
"VIDIOC_REQBUFS");
96 fprintf(stderr,
"Insufficient buffer memory on %s\n", dev_name);
103 fprintf(stderr,
"Out of memory\n");
108 struct v4l2_buffer buf;
112 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
113 buf.memory = V4L2_MEMORY_MMAP;
116 if (ioctl(
fd, VIDIOC_QUERYBUF, &buf) == -1) {
117 perror(
"VIDIOC_QUERYBUF");
123 buf.length, PROT_READ | PROT_WRITE
136 enum v4l2_buf_type type;
139 struct v4l2_buffer buf;
143 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
144 buf.memory = V4L2_MEMORY_MMAP;
147 if (ioctl(
fd, VIDIOC_QBUF, &buf) == -1) {
153 perror(
"VIDIOC_QBUF");
158 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
160 if (ioctl(
fd, VIDIOC_STREAMON, &type) == -1) {
166 perror(
"VIDIOC_STREAMON");
170 fprintf(stderr,
"video capabilities\n");
171 fprintf(stderr,
"cap.driver = %s\n", cap.driver);
172 fprintf(stderr,
"cap.card = %s\n", cap.card);
173 fprintf(stderr,
"cap.buf_info = %s\n", cap.bus_info);
174 fprintf(stderr,
"cap.version = %d\n", cap.version);
175 fprintf(stderr,
"cap.capabilities = 0x%08x ", cap.capabilities);
176 if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
177 fprintf(stderr,
" VIDEO_CAPTURE");
178 if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)
179 fprintf(stderr,
" VIDEO_OUTPUT");
180 if (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY)
181 fprintf(stderr,
" VIDEO_OVERLAY");
182 if (cap.capabilities & V4L2_CAP_VBI_CAPTURE)
183 fprintf(stderr,
" VBI_CAPTURE");
184 if (cap.capabilities & V4L2_CAP_VBI_OUTPUT)
185 fprintf(stderr,
" VBI_OUTPUT");
186 #ifdef V4L2_CAP_SLICED_VBI_CAPTURE 187 if (cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE)
188 fprintf(stderr,
" SLICED_VBI_CAPTURE");
190 #ifdef V4L2_CAP_SLICED_VBI_OUTPUT 191 if (cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT)
192 fprintf(stderr,
" VBI_SLICED_OUTPUT");
194 if (cap.capabilities & V4L2_CAP_RDS_CAPTURE)
195 fprintf(stderr,
" RDS_CAPTURE");
196 #if V4L2_CAP_VIDEO_OUTPUT_OVERLAY 197 if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
198 fprintf(stderr,
" VIDEO_OUTPUT_OVERLAY");
200 if (cap.capabilities & V4L2_CAP_TUNER)
201 fprintf(stderr,
" TUNER");
202 if (cap.capabilities & V4L2_CAP_AUDIO)
203 fprintf(stderr,
" AUDIO");
204 if (cap.capabilities & V4L2_CAP_RADIO)
205 fprintf(stderr,
" RADIO");
206 if (cap.capabilities & V4L2_CAP_READWRITE)
207 fprintf(stderr,
" READWRITE");
208 if (cap.capabilities & V4L2_CAP_ASYNCIO)
209 fprintf(stderr,
" ASYNCIO");
210 if (cap.capabilities & V4L2_CAP_STREAMING)
211 fprintf(stderr,
" STREAMING");
212 fprintf(stderr,
"\n");
213 fprintf(stderr,
"cap.width = %d\n",
width);
214 fprintf(stderr,
"cap.height = %d\n",
height);
217 frame = (IplImage *)malloc(
sizeof(IplImage));
218 cvInitImageHeader(
frame,
220 IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
222 frame->imageData = (
char *)cvAlloc(
frame->imageSize);
228 enum v4l2_buf_type type;
230 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
232 if (ioctl(
fd, VIDIOC_STREAMOFF, &type) == -1) {
233 perror(
"VIDIOC_STREAMOFF");
251 if (close(
fd) == -1) {
261 struct v4l2_buffer buf;
265 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
266 buf.memory = V4L2_MEMORY_MMAP;
268 if (ioctl(
fd, VIDIOC_DQBUF, &buf) == -1) {
269 perror(
"VIDIOC_DQBUF");
278 (
unsigned char*)
frame->imageData)) {
279 perror(
"mjpeg_to_rgb24");
283 if (ioctl(
fd, VIDIOC_QBUF, &buf) == -1) {
284 perror(
"VIDIOC_QBUF");
297 fprintf(stderr,
"ERROR : try sudo -E ./bin/init_xu_register\n");
301 fprintf(stderr,
"serialId -> %s\n",
getSerialID().c_str());
315 fprintf(stderr,
"ERROR : read_frame returns NULL\n");
337 int height = frame->height;
338 cvGetSubRect(frame, &subframe, cvRect(0, 0, height/2, height/2));
348 cvGetSubRect(frame, &subframe, cvRect(0,
height/2, width/2,
height/2));
353 cvGetSubRect(frame, &subframe, cvRect(width/2,
height/2, width/2,
height/2));
359 cvSetImageROI(
frame, cvRect(0, 0, height/2, height/2));
361 cvResetImageROI(
frame);
369 cvResetImageROI(
frame);
377 cvResetImageROI(
frame);
385 cvResetImageROI(
frame);
391 return (ioctl(
fd, VIDIOC_S_INPUT, &mode)==0);
451 if ( ioctl(
fd, VIDIOC_QUERYCTRL, &q) < 0 )
453 perror(
"unable to query control");
460 fprintf(stderr,
"VIDIOC_S_CTRL: sel = 0x%08x, data =0x%08x\n",
462 if (ioctl(
fd, VIDIOC_S_CTRL, &c) < 0)
464 perror(
"unable to set control");
473 fprintf(stderr,
"VIDIOC_S_CTRL: sel = 0x%02x\n", selector);
474 if (ioctl(
fd, VIDIOC_G_CTRL, &c) == 0)
476 perror(
"unable to get control");
485 std::string str(255,0);
487 std::string str(32,0);
490 return str.substr(0,str.find_first_of(
'\0'));
505 std::string str(16,0);
507 return str.substr(0,str.find_first_of(
'\0'));
540 return xu_ioctl(selector, UVC_GET_CUR, (
void *)str);
543 return xu_ioctl(selector, UVC_GET_CUR, (
void *)value);
546 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
549 struct {
short v1, v2;} value;
552 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
555 struct {
short v1, v2, v3, v4, v5;} value;
556 value.v1 = v1; value.v2 = v2; value.v3 = v3; value.v4 = v4; value.v5 = v5;
557 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
563 struct uvc_xu_control_query xctrl;
567 xctrl.data = (__u8*)value;
569 fprintf(stderr,
"%s: name = %s, sel = 0x%02x, size = %d, data =",
570 (ctrl == UVC_SET_CUR) ?
"UVC_SET_CUR" :
"UVC_GET_CUR",
574 if (ioctl(
fd, UVCIOC_CTRL_QUERY, &xctrl) != 0) {
576 fprintf(stderr,
"\nioctl error %s\n", strerror(errno));
579 for (
int j = 0; j < xctrl.size; j++)
580 fprintf(stderr,
" 0x%02x", *(xctrl.data + j));
581 fprintf(stderr,
"\n");
std::string getFirmwareVersion()
bool getXuValue(int selector, const char *str)
bool setTiltAbsolute(double value)
bool setXuValue(int selector, char value)
bool setRollAbsolute(double value)
bool setLensType(char value)
IplImage * queryOmniFrame()
IplImage * queryWideFrame()
bool xu_ioctl(int selector, int ctrl, void *value)
bool setWhitebalance(int value)
bool setBrightness(int value)
bool setFlipScreen(char value)
std::string getSerialID()
IplImage * queryNarrowFrame()
bool setCaptureFPS(short value)
#define XU_CAPTURE_FPS_CONTROL
#define XU_ZOOM_ABSOLUTE_CONTROL
#define XU_PAN_ABSOLUTE_CONTROL
#define XU_FIRMWARE_VERSION_CONTROL
bool setAutoExposure(bool mode)
bool setExposure(int value)
void getOmniImage(IplImage *frame, CvMat &subframe)
#define XU_JPEG_QUALITY_CONTROL
bool setSharpness(int value)
#define XU_TILT_ABSOLUTE_CONTROL
int v4l2_get_ioctl(int selector)
void getNarrowImage(IplImage *frame, CvMat &subframe)
#define V4L2_CID_IRIS_ABSOLUTE
bool setZoomAbsolute(double value)
void device_open(int camera_index)
bool setPanAbsolute(double value)
void getMiddleImage(IplImage *frame, CvMat &subframe)
bool v4l2_set_ioctl(int selector, int value)
#define XU_SERIAL_ID_CONTROL
#define XU_SMALL_HEMISPHERE_CONTROL
uvc_xu_tbl_info xu_control_tbl[]
#define XU_ROLL_ABSOLUTE_CONTROL
#define XU_LOCATION_ABSOLUTE_CONTROL
bool setLocationAbsolute(int no, int pan, int tilt, int roll, int zoom)
bool setJpegQuality(char value)
#define XU_MEDIAN_FILTER_CONTROL
#define XU_LENS_TYPE_CONTROL
void getWideImage(IplImage *frame, CvMat &subframe)
bool setSmallHemisphere(char value)
#define XU_INFO_DISPLAY_CONTROL
struct OptNM3xCamera::buffer * buffers
bool setInfoDisplay(bool mode)
OptNM3xCamera(int camera_index)
bool setMedianFilter(bool mode)
bool setAutoWhitebalance(bool mode)
#define V4L2_CID_ROLL_ABSOLUTE
IplImage * queryMiddleFrame()
static bool mjpeg_to_rgb24(int width, int height, unsigned char *src, int length, unsigned char *dst)
#define XU_FLIP_SCREEN_CONTROL