Created
March 13, 2020 04:34
-
-
Save notogawa/87dd5d645a5887a09d5d1007ab9c243e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -o main main.c -lv4l2 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <libv4l2.h> | |
#include <linux/videodev2.h> | |
int main(int argc, char* argv[]) { | |
char* device = "/dev/video0"; | |
int width = 624; | |
int height = 480; | |
int opt = 0; | |
while ((opt = getopt(argc, argv, "d:w:h:")) != -1) { | |
switch (opt) { | |
case 'd': { device = optarg; } break; | |
case 'w': { width = atoi(optarg); } break; | |
case 'h': { height = atoi(optarg); } break; | |
default: { fprintf(stderr, "error: \'%c\' \'%c\'\n", opt, optopt); return 1;} | |
} | |
} | |
int fd = v4l2_open(device, O_RDWR); | |
if (fd < 0) { | |
perror("error: v4l2_open"); | |
return 1; | |
} | |
struct v4l2_format fmt; | |
memset(&fmt, 0, sizeof(fmt)); | |
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
fmt.fmt.pix.width = width; | |
fmt.fmt.pix.height = height; | |
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; | |
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; | |
int res = v4l2_ioctl(fd, VIDIOC_S_FMT, &fmt); | |
if (res != 0) { | |
perror("error"); | |
return 1; | |
} | |
if (fmt.fmt.pix.width != width || | |
fmt.fmt.pix.height != height || | |
fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) { | |
printf("unexpected format\n"); | |
return 1; | |
} | |
puts("[format]"); | |
printf("width : %u\n", fmt.fmt.pix.width); | |
printf("height : %u\n", fmt.fmt.pix.height); | |
printf("bytesperline : %u\n", fmt.fmt.pix.bytesperline); | |
printf("sizeimage : %u\n", fmt.fmt.pix.sizeimage); | |
printf("bytesperline * height: %u\n", fmt.fmt.pix.bytesperline * fmt.fmt.pix.height); | |
v4l2_close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment