📄 cam_set.c
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <sys/ioctl.h>#include <linux/videodev.h>#include "pwc-ioctl.h"int fd = -1;struct pwc_whitebalance wb;inline void print_help(char *argv0) { fprintf(stderr, "Usage: %s [option]\n" "-d n\tset cam id n (default 0)\n" "-i n\tshow info\n" "-a n\tset agc\n" "-w s\tset wb\n" "-r n\treset cam\n" "-h\tprint this help\n", argv0);}inline void cam_open(int id) { if (id != 0 && id != 1) { fprintf(stderr, "video id should be 0 or 1\n"); exit(-1); } char dev[12]; sprintf(dev, "/dev/video%d", id); if ((fd = open(dev, O_RDONLY)) < 0) { perror(dev); exit(-1); }}inline void check_start() { if (fd >= 0) return; cam_open(0); printf("using video0\n");}inline void show_info() { int t; // agc; if (ioctl(fd, VIDIOCPWCGAGC, &t) < 0) { perror("VIDIOCPWCGAGC"); exit(-1); } printf("agc=%d (", t); if (t < 0) printf("auto)\n"); else printf("fixed)\n"); // wb if (ioctl(fd, VIDIOCPWCGAWB, &wb) < 0) { perror("VIDIOCPWCGAWB"); exit(-1); } printf("wb="); switch (wb.mode) { case PWC_WB_AUTO: printf("auto, red=%d, blue=%d\n", wb.read_red, wb.read_blue); break; case PWC_WB_MANUAL: printf("manual, red=%d, blue=%d\n", wb.manual_red, wb.manual_blue); break; case PWC_WB_INDOOR: printf("indoor\n"); break; case PWC_WB_OUTDOOR: printf("outdoor\n"); break; case PWC_WB_FL: printf("fl\n"); break; default: fprintf(stderr, "wb mode error\n"); exit(-1); } // compression; if (ioctl(fd, VIDIOCPWCGCQUAL, &t) < 0) { perror("VIDIOCPWCGCQUAL"); exit(-1); } printf("compression=%d (", t); switch (t) { case 0: printf("no)\n"); break; case 1: printf("less)\n"); break; case 2: printf("middle)\n"); break; case 3: printf("high)\n"); } struct pwc_wb_speed wbs; if (ioctl(fd, VIDIOCPWCGAWBSPEED, &wbs) < 0) { perror("VIDIOCPWCGAWBSPEED"); exit(-1); } printf("wb speed=%d, delay=%d\n", wbs.control_speed, wbs.control_delay); /* VIDIOCPWCGLED */ // contour; if (ioctl(fd, VIDIOCPWCGCONTOUR, &t) < 0) { perror("VIDIOCPWCGCONTOUR"); exit(-1); } printf("contour=%d\n", t); // bl; if (ioctl(fd, VIDIOCPWCGBACKLIGHT, &t) < 0) { perror("VIDIOCPWCGBACKLIGHT"); exit(-1); } printf("backlight mode=%d (", t); if (t) printf("on)\n"); else printf("off)\n"); // af; if (ioctl(fd, VIDIOCPWCGFLICKER, &t) < 0) { perror("VIDIOCPWCGFLICKER "); exit(-1); } printf("anti-flicker=%d (", t); if (t) printf("on)\n"); else printf("off)\n"); // noise; if (ioctl(fd, VIDIOCPWCGDYNNOISE, &t) < 0) { perror("VIDIOCPWCGDYNNOISE"); exit(-1); } printf("dynamic noise reduction=%d (", t); switch (t) { case 0: printf("none)\n"); break; case 1: printf("less)\n"); break; case 2: printf("middle)\n"); break; case 3: printf("high)\n"); break; default: fprintf(stderr, "noise mode error\n"); exit(-1); }}int main(int argc, char *argv[]) { for (;;) { int t = getopt(argc, argv, "d:ia:w:rh"); if (t == -1) { break; } switch (t) { case 'd': if (sscanf(optarg, "%d", &t) != 1) { fprintf(stderr, "error optarg: %s\n", optarg); exit(-1); } if (fd >= 0) close(fd); cam_open(t); printf("using video%d\n", t); break; case 'i': check_start(); show_info(); break; case 'a': check_start(); if (sscanf(optarg, "%d", &t) != 1) { fprintf(stderr, "error optarg: %s\n", optarg); exit(-1); } if (ioctl(fd, VIDIOCPWCSAGC, &t) < 0) { perror("VIDIOCPWCSAGC"); exit(-1); } if (ioctl(fd, VIDIOCPWCGAGC, &t) < 0) { perror("VIDIOCPWCGAGC"); exit(-1); } printf("reread agc=%d\n", t); break; case 'w': check_start(); t = sscanf(optarg, "%d,%d", &wb.manual_red, &wb.manual_blue); switch (t) { case 2: wb.mode = PWC_WB_MANUAL; break; default: wb.mode = wb.manual_red; } if (ioctl(fd, VIDIOCPWCSAWB, &wb) < 0) { perror("VIDIOCPWCSAWB"); exit(-1); } if (ioctl(fd, VIDIOCPWCGAWB, &wb) < 0) { perror("VIDIOCPWCGAWB"); exit(-1); } if (t == 2) printf("reread wb red=%d blue=%d\n", wb.manual_red, wb.manual_blue); else { printf("reread wb="); switch (wb.mode) { case PWC_WB_AUTO: printf("auto\n"); break; case PWC_WB_INDOOR: printf("indoor\n"); break; case PWC_WB_OUTDOOR: printf("outdoor\n"); break; case PWC_WB_FL: printf("fl\n"); break; default: fprintf(stderr, "wb mode error\n"); exit(-1); } } break; case 'r': check_start(); if (ioctl(fd, VIDIOCPWCFACTORY, NULL) < 0) { perror("VIDIOCPWCFACTORY"); exit(-1); } printf("cam reset\n"); break; case 'h': print_help(argv[0]); return 0; case '?': break; default: fprintf(stderr, "error option: %d\n", t); exit(-1); } } if (fd >= 0) close(fd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -