int main(int argc, char **argv) { for (;;) { int index, c = 0; c = getopt_long(argc, argv, short_options, long_options, &index); if (-1 == c) break; switch (c) { case 0: /* getopt_long() flag */ break; case 'd': deviceName = optarg; break; case 'h': // print help usage(stdout, argc, argv); exit(EXIT_SUCCESS); case 'o': // set jpeg filename jpegFilename = optarg; break; case 'q': // set jpeg quality jpegQuality = atoi(optarg); break; case 'm': #ifdef IO_MMAP io = IO_METHOD_MMAP; #else fprintf(stderr, "You didn't compile for mmap support.\n"); exit(EXIT_FAILURE); #endif break; case 'r': #ifdef IO_READ io = IO_METHOD_READ; #else fprintf(stderr, "You didn't compile for read support.\n"); exit(EXIT_FAILURE); #endif break; case 'u': #ifdef IO_USERPTR io = IO_METHOD_USERPTR; #else fprintf(stderr, "You didn't compile for userptr support.\n"); exit(EXIT_FAILURE); #endif break; case 'W': // set width width = atoi(optarg); break; case 'H': // set height height = atoi(optarg); break; case 'I': // set fps fps = atoi(optarg); break; case 'c': // set flag for continuous capture, interuptible by sigint continuous = 1; InstallSIGINTHandler(); break; case 'v': printf("Version: %s\n", VERSION); exit(EXIT_SUCCESS); break; default: usage(stderr, argc, argv); exit(EXIT_FAILURE); } } // check for need parameters if (!jpegFilename) { fprintf(stderr, "You have to specify JPEG output filename!\n\n"); usage(stdout, argc, argv); exit(EXIT_FAILURE); } if(continuous == 1) { int max_name_len = snprintf(NULL,0,continuousFilenameFmt,jpegFilename,UINT32_MAX,INT64_MAX); jpegFilenamePart = jpegFilename; jpegFilename = calloc(max_name_len+1,sizeof(char)); strcpy(jpegFilename,jpegFilenamePart); } // open and initialize device deviceOpen(); deviceInit(); // start capturing captureStart(); // process frames mainLoop(); // stop capturing captureStop(); // close device deviceUninit(); deviceClose(); if(jpegFilenamePart != 0) { free(jpegFilename); } exit(EXIT_SUCCESS); return 0; }
int main() { fd = open("/dev/video0", O_RDWR | O_NONBLOCK, 0); if (-1 == fd) { perror("camera open error"); return -1; } else printf("camera opened & fd=%d\n",fd); struct v4l2_capability cap; struct v4l2_cropcap cropcap; struct v4l2_crop crop; struct v4l2_format fmt; if (-1 == ioctl(fd, VIDIOC_QUERYCAP, &cap)) { if (EINVAL == errno) { perror(" no V4L2 device"); return -1; } else { perror("VIDIOC_QUERYCAP"); return -1; } } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { perror(" no video capture device"); return -1; } struct v4l2_input input; input.index = 0; if ( ioctl(fd, VIDIOC_ENUMINPUT, &input) != 0) { perror("set input error"); return -1; } if ((ioctl(fd, VIDIOC_S_INPUT, &input)) < 0) { perror("set s_input error"); return -1; } /* CLEAR(cropcap); cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) { crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; crop.c = cropcap.defrect; if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) { switch (errno) { case EINVAL: break; default: break; } } }*/ //CLEAR (fmt); // v4l2_format fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == ioctl(fd, VIDIOC_G_FMT, &fmt)) {perror("VIDIOC_G_FMT");return -1;} printf("Now show the default fmt\n\n"); printf("width=%d\n",fmt.fmt.pix.width); printf("height=%d\n",fmt.fmt.pix.height); printf("fmt=%x\n\n",fmt.fmt.pix.pixelformat); printf("field=%x\n\n",fmt.fmt.pix.field); printf("Now set fmt to 800*600-BGR32\n\n"); fmt.fmt.pix.width = 1280; fmt.fmt.pix.height = 960; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR32; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; printf("width=%d\n",fmt.fmt.pix.width); printf("height=%d\n",fmt.fmt.pix.height); printf("fmt=%x\n\n",fmt.fmt.pix.pixelformat); printf("field=%x\n\n",fmt.fmt.pix.field); if (-1 == ioctl(fd, VIDIOC_S_FMT, &fmt)) perror("VIDIOC_S_FMT"); if (-1 == ioctl(fd, VIDIOC_G_FMT, &fmt)) {perror("VIDIOC_G_FMT");return -1;} printf("Now see what have been changed \n\n"); printf("width=%d\n",fmt.fmt.pix.width); printf("height=%d\n",fmt.fmt.pix.height); printf("fmt=%x\n\n",fmt.fmt.pix.pixelformat); printf("field=%x\n\n",fmt.fmt.pix.field); cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (0 == ioctl(fd, VIDIOC_CROPCAP, &cropcap)) { //crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; printf("defrect:w=%d,h=%d\n", cropcap.defrect.width,cropcap.defrect.height); printf("bounds:w=%d,h=%d\n", cropcap.bounds.width,cropcap.bounds.height); crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; crop.c.height= cropcap.defrect.height/2; /* reset to default */ crop.c.width= cropcap.defrect.width/2; } crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; //if (-1 == ioctl(fd, VIDIOC_G_CROP, &crop)) // perror("VIDIOC_G_CROP"); //else printf("crop.c.height=%d width=%d",crop.c.height,crop.c.width); if (-1 == ioctl(fd, VIDIOC_S_CROP, &crop)) perror("VIDIOC_S_CROP"); else printf("crop.c.height=%d width=%d",crop.c.height,crop.c.width); //mmapInit(); //captureStart(); deviceUninit(); }
int main(int argc, char **argv) { for (;;) { int index, c = 0; c = getopt_long(argc, argv, short_options, long_options, &index); if (-1 == c) break; switch (c) { case 0: /* getopt_long() flag */ break; case 'd': deviceName = optarg; break; case 'h': // print help usage(stdout, argc, argv); exit(EXIT_SUCCESS); case 'o': // set jpeg filename jpegFilename = optarg; break; case 'q': // set jpeg quality jpegQuality = atoi(optarg); break; case 'm': #ifdef IO_MMAP io = IO_METHOD_MMAP; #else fprintf(stderr, "You didn't compile for mmap support.\n"); exit(EXIT_FAILURE); #endif break; case 'r': #ifdef IO_READ io = IO_METHOD_READ; #else fprintf(stderr, "You didn't compile for read support.\n"); exit(EXIT_FAILURE); #endif break; case 'u': #ifdef IO_USERPTR io = IO_METHOD_USERPTR; #else fprintf(stderr, "You didn't compile for userptr support.\n"); exit(EXIT_FAILURE); #endif break; case 'W': // set width width = atoi(optarg); break; case 'H': // set height height = atoi(optarg); break; case 'I': // set fps fps = atoi(optarg); break; case 'v': printf("Version: %s\n", VERSION); exit(EXIT_SUCCESS); break; default: usage(stderr, argc, argv); exit(EXIT_FAILURE); } } // check for need parameters if (!jpegFilename) { fprintf(stderr, "You have to specify JPEG output filename!\n\n"); usage(stdout, argc, argv); exit(EXIT_FAILURE); } // open and initialize device deviceOpen(); deviceInit(); // start capturing captureStart(); // process frames mainLoop(); // stop capturing captureStop(); // close device deviceUninit(); deviceClose(); exit(EXIT_SUCCESS); return 0; }