9 #include <opencv/highgui.h> 10 #include <opencv2/opencv.hpp> 16 #define CLEAR(x) memset (&(x), 0, sizeof (x)) 21 unsigned char *src,
int length,
24 cv::Mat temp=cv::imdecode(cv::Mat(std::vector<uchar>(src, src + length)), 1);
25 if( !temp.data || temp.cols != width || temp.rows != height )
27 memcpy(dst, temp.data, width*height*3);
35 sprintf(dev_name,
"/dev/video%1d", camera_index);
36 fprintf(stderr,
"Opening device '%s'\n", dev_name);
42 fd = open(dev_name, O_RDWR, 0);
45 fprintf(stderr,
"Cannot open '%s': %d, %s\n",
46 dev_name, errno, strerror(errno));
51 struct v4l2_capability cap;
52 struct v4l2_format fmt;
54 if (ioctl(
fd, VIDIOC_QUERYCAP, &cap) == -1) {
55 if (EINVAL == errno) {
56 fprintf(stderr,
"%s is no V4L2 device\n", dev_name);
58 perror(
"VIDIOC_QUERYCAP");
62 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
63 fprintf(stderr,
"%s is no video capture device\n", dev_name);
67 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
68 fprintf(stderr,
"%s does not support streaming i/o\n", dev_name);
74 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
75 fmt.fmt.pix.width =
width;
76 fmt.fmt.pix.height =
height;
77 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
78 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
80 if (ioctl(
fd, VIDIOC_S_FMT, &fmt) == -1) {
81 perror(
"VIDIOC_S_FMT");
86 struct v4l2_requestbuffers req;
91 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
92 req.memory = V4L2_MEMORY_MMAP;
94 if (ioctl(
fd, VIDIOC_REQBUFS, &req) == -1) {
95 perror(
"VIDIOC_REQBUFS");
100 fprintf(stderr,
"Insufficient buffer memory on %s\n", dev_name);
107 fprintf(stderr,
"Out of memory\n");
112 struct v4l2_buffer buf;
116 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
117 buf.memory = V4L2_MEMORY_MMAP;
120 if (ioctl(
fd, VIDIOC_QUERYBUF, &buf) == -1) {
121 perror(
"VIDIOC_QUERYBUF");
127 buf.length, PROT_READ | PROT_WRITE
140 enum v4l2_buf_type type;
143 struct v4l2_buffer buf;
147 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
148 buf.memory = V4L2_MEMORY_MMAP;
151 if (ioctl(
fd, VIDIOC_QBUF, &buf) == -1) {
157 perror(
"VIDIOC_QBUF");
162 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
164 if (ioctl(
fd, VIDIOC_STREAMON, &type) == -1) {
170 perror(
"VIDIOC_STREAMON");
174 fprintf(stderr,
"video capabilities\n");
175 fprintf(stderr,
"cap.driver = %s\n", cap.driver);
176 fprintf(stderr,
"cap.card = %s\n", cap.card);
177 fprintf(stderr,
"cap.buf_info = %s\n", cap.bus_info);
178 fprintf(stderr,
"cap.version = %d\n", cap.version);
179 fprintf(stderr,
"cap.capabilities = 0x%08x ", cap.capabilities);
180 if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
181 fprintf(stderr,
" VIDEO_CAPTURE");
182 if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)
183 fprintf(stderr,
" VIDEO_OUTPUT");
184 if (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY)
185 fprintf(stderr,
" VIDEO_OVERLAY");
186 if (cap.capabilities & V4L2_CAP_VBI_CAPTURE)
187 fprintf(stderr,
" VBI_CAPTURE");
188 if (cap.capabilities & V4L2_CAP_VBI_OUTPUT)
189 fprintf(stderr,
" VBI_OUTPUT");
190 #ifdef V4L2_CAP_SLICED_VBI_CAPTURE 191 if (cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE)
192 fprintf(stderr,
" SLICED_VBI_CAPTURE");
194 #ifdef V4L2_CAP_SLICED_VBI_OUTPUT 195 if (cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT)
196 fprintf(stderr,
" VBI_SLICED_OUTPUT");
198 if (cap.capabilities & V4L2_CAP_RDS_CAPTURE)
199 fprintf(stderr,
" RDS_CAPTURE");
200 #if V4L2_CAP_VIDEO_OUTPUT_OVERLAY 201 if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
202 fprintf(stderr,
" VIDEO_OUTPUT_OVERLAY");
204 if (cap.capabilities & V4L2_CAP_TUNER)
205 fprintf(stderr,
" TUNER");
206 if (cap.capabilities & V4L2_CAP_AUDIO)
207 fprintf(stderr,
" AUDIO");
208 if (cap.capabilities & V4L2_CAP_RADIO)
209 fprintf(stderr,
" RADIO");
210 if (cap.capabilities & V4L2_CAP_READWRITE)
211 fprintf(stderr,
" READWRITE");
212 if (cap.capabilities & V4L2_CAP_ASYNCIO)
213 fprintf(stderr,
" ASYNCIO");
214 if (cap.capabilities & V4L2_CAP_STREAMING)
215 fprintf(stderr,
" STREAMING");
216 fprintf(stderr,
"\n");
217 fprintf(stderr,
"cap.width = %d\n",
width);
218 fprintf(stderr,
"cap.height = %d\n",
height);
221 frame = (IplImage *)malloc(
sizeof(IplImage));
222 cvInitImageHeader(
frame,
224 IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
226 frame->imageData = (
char *)cvAlloc(
frame->imageSize);
232 enum v4l2_buf_type type;
234 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
236 if (ioctl(
fd, VIDIOC_STREAMOFF, &type) == -1) {
237 perror(
"VIDIOC_STREAMOFF");
255 if (close(
fd) == -1) {
265 struct v4l2_buffer buf;
269 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
270 buf.memory = V4L2_MEMORY_MMAP;
272 if (ioctl(
fd, VIDIOC_DQBUF, &buf) == -1) {
273 perror(
"VIDIOC_DQBUF");
282 (
unsigned char*)
frame->imageData)) {
283 perror(
"mjpeg_to_rgb24");
287 if (ioctl(
fd, VIDIOC_QBUF, &buf) == -1) {
288 perror(
"VIDIOC_QBUF");
301 fprintf(stderr,
"ERROR : try sudo -E ./bin/init_xu_register\n");
305 fprintf(stderr,
"serialId -> %s\n",
getSerialID().c_str());
319 fprintf(stderr,
"ERROR : read_frame returns NULL\n");
341 int height = frame->height;
342 cvGetSubRect(frame, &subframe, cvRect(0, 0, height/2, height/2));
352 cvGetSubRect(frame, &subframe, cvRect(0,
height/2, width/2,
height/2));
357 cvGetSubRect(frame, &subframe, cvRect(width/2,
height/2, width/2,
height/2));
363 cvSetImageROI(
frame, cvRect(0, 0, height/2, height/2));
365 cvResetImageROI(
frame);
373 cvResetImageROI(
frame);
381 cvResetImageROI(
frame);
389 cvResetImageROI(
frame);
395 return (ioctl(
fd, VIDIOC_S_INPUT, &mode)==0);
455 if ( ioctl(
fd, VIDIOC_QUERYCTRL, &q) < 0 )
457 perror(
"unable to query control");
464 fprintf(stderr,
"VIDIOC_S_CTRL: sel = 0x%08x, data =0x%08x\n",
466 if (ioctl(
fd, VIDIOC_S_CTRL, &c) < 0)
468 perror(
"unable to set control");
477 fprintf(stderr,
"VIDIOC_S_CTRL: sel = 0x%02x\n", selector);
478 if (ioctl(
fd, VIDIOC_G_CTRL, &c) == 0)
480 perror(
"unable to get control");
489 std::string str(255,0);
491 std::string str(32,0);
494 return str.substr(0,str.find_first_of(
'\0'));
509 std::string str(16,0);
511 return str.substr(0,str.find_first_of(
'\0'));
544 return xu_ioctl(selector, UVC_GET_CUR, (
void *)str);
547 return xu_ioctl(selector, UVC_GET_CUR, (
void *)value);
550 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
553 struct {
short v1, v2;} value;
556 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
559 struct {
short v1, v2, v3, v4, v5;} value;
560 value.v1 = v1; value.v2 = v2; value.v3 = v3; value.v4 = v4; value.v5 = v5;
561 return xu_ioctl(selector, UVC_SET_CUR, (
void *)&value);
567 struct uvc_xu_control_query xctrl;
571 xctrl.data = (__u8*)value;
573 fprintf(stderr,
"%s: name = %s, sel = 0x%02x, size = %d, data =",
574 (ctrl == UVC_SET_CUR) ?
"UVC_SET_CUR" :
"UVC_GET_CUR",
578 if (ioctl(
fd, UVCIOC_CTRL_QUERY, &xctrl) != 0) {
580 fprintf(stderr,
"\nioctl error %s\n", strerror(errno));
583 for (
int j = 0; j < xctrl.size; j++)
584 fprintf(stderr,
" 0x%02x", *(xctrl.data + j));
585 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