📄 videotest.c
字号:
/*
*
*
*
*
*
*
* Just grabs a frame from v4l device or set its picture defaults. Useful for scripts, webcam scripts.
* Output: a raw frame ( RGB ) or pnm frame ( -p option )
*
*
*ov511 ( webcam III usb )
*
* options you need:
*
* h - help
* x - width
* y - height
* w - depth ( in bytes )
* d - device
* p - pnm output (24 or 8 bits only )
*
* Optional:
*
* u - Hue
* c - Contrast
* b - Brightness
* l - Colour
* s - show device report
* n - no capture, just setup and exit clean
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <asm/types.h>
#include <linux/videodev.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define VERSION "0.04"
#define OPTSTR "hx:y:w:d:pu:c:b:l:sn"
#define DOT fprintf(stderr,".");
void showhelp(void) {
fprintf (stderr,"Video Dog tool Version %s\n", VERSION);
fprintf (stderr,"Gleicon S. Moraes\n");
fprintf (stderr,"Options you really need:\n");
fprintf (stderr,"-x (number) image width\n");
fprintf (stderr,"-y (number) image height\n");
fprintf (stderr,"-w (number) image depth (in bytes) \n");
fprintf (stderr,"-d device (like /dev/video0 )\n");
fprintf (stderr,"-p set the output in pnm format (24 or 8 bits only)\n");
fprintf (stderr, "Other options :\n");
fprintf (stderr,"-h just show this lines, doesn't help you ok ?\n");
fprintf (stderr,"-u image Hue\n");
fprintf (stderr,"-c image Contrast\n");
fprintf (stderr,"-b image Brightness \n");
fprintf (stderr,"-l image Colour\n");
fprintf (stderr,"-s show device report\n");
fprintf (stderr,"-n Dont capture an image, just setup the device (Useful if you want to correct something before start grabbing)\n");
exit(0);
}
/* global */
int x=320, y=240, w=3;
typedef int INT32;
/*typedef short INT16;*/
typedef char INT8;
typedef unsigned int UINT32;
typedef unsigned short UINT16;
typedef unsigned char UINT8;
static struct video_picture grab_pic;
static struct video_capability grab_cap;
static struct video_mmap grab_buf;
static int grab_fd,grab_size=0;
static unsigned char *grab_data;
int hue=128, contrast=86, brightness=145,colour=192;
int planes,biBitCount;
typedef struct tagBITMAPFILEHEADER {
//char bfType;
UINT32 bfSize;
UINT32 bfReserved;
UINT32 bfOffBits;
UINT32 bhedsize;
UINT32 width;
UINT32 height;
//UINT16 planes;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER {
//UINT16 biBitCount;
UINT32 compression;
UINT32 biSize;
UINT32 biXPelsPerMeter;
UINT32 biYPelsPerMeter;
UINT32 biClrUsed;
UINT32 biClrImportant;
} BITMAPINFOHEADER;
/*typedef struct tagRGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;*/
BITMAPFILEHEADER finfo;
BITMAPINFOHEADER binfo;
/*RGBQUAD rgbquad[256];*/
/* prototype */
//void swap_rgb24(char *, int );
unsigned char* grab_one(int *, int *);
int grab_init();
/* signal handler */
void _sighandler (int sig) {
switch (sig){
case SIGINT: /* ctrl+x */
//free (v_device);
close (grab_fd);
munmap(grab_data, grab_size);
fprintf (stderr, "Caught SIGINT - Cleaning \n");
break;
}
}
void set_picture() {
grab_pic.hue=hue;
grab_pic.contrast=contrast;
grab_pic.brightness=brightness;
grab_pic.colour=colour;
if (ioctl(grab_fd, VIDIOCSPICT, &grab_pic) == -1) {
perror ("PICTURE");
exit (1);
}
}
void show_cap() { /* mostra settings atuais */
//fprintf (stderr,"\nVideo Cat tool Version %s\n", VERSION);
fprintf (stderr, "Device Name: %s\n", grab_cap.name);
//fprintf (stderr, "Device: %s\n", v_device);
fprintf (stderr, "Max Width : %d\n", grab_cap.maxwidth);
fprintf (stderr, "Max Height : %d\n", grab_cap.maxheight);
fprintf (stderr, "Current Settings :\n\n");
fprintf (stderr, "\tBrightness : %i\n", grab_pic.brightness);
fprintf (stderr, "\tHue : %i\n", grab_pic.hue);
fprintf (stderr, "\tColour : %i\n", grab_pic.colour);
fprintf (stderr, "\tContrast : %i\n", grab_pic.contrast);
fprintf (stderr, "\n");
}
main ( ) {
// unsigned char *grab_one();
FILE *stream;
int c;
int showc=1;
signal (SIGINT, _sighandler);
grab_init();
set_picture();
if (showc) show_cap();
stream=fopen("/var/test.bmp","wb");
/*fprintf(stream,"P6%d%d",x,y);*/
//finfo.bfType=0x4d42;
fprintf(stream,"BM");
finfo.bfSize=230460;
finfo.bfReserved=0;
finfo.bfOffBits=54;
finfo.bhedsize=40;
finfo.width=320;
finfo.height=240;
//finfo.planes=1;
fwrite(&finfo,sizeof(finfo),1,stream);
planes=1;
fwrite(&planes,2,1,stream);
biBitCount=24;
fwrite(&biBitCount,2,1,stream);
binfo.compression=0; binfo.biSize=230406;
binfo.biXPelsPerMeter=0;
binfo.biYPelsPerMeter=0;
binfo.biClrUsed=0;
binfo.biClrImportant=0;
fwrite(&binfo,sizeof(binfo),1,stream);
/*for ( i=0;i<256;i++)
{ rgbquad[i].rgbBlue=i;
rgbquad[i].rgbGreen= i;
rgbquad[i].rgbRed=i;
rgbquad[i].rgbReserved=0;
}
fwrite(rgbquad,1024,1,stream);*/
fwrite ((unsigned char*)grab_one( &x, &y), x * y * w, 1, stream);
fclose(stream);
close (grab_fd);
munmap(grab_data, grab_size);
} /* main */
void swap_rgb24(unsigned char *mem, int n) {
unsigned char c;
unsigned char *p=mem;
int i=n;
while (--i) {
c=p[0];
p[0]=p[2];
p[2]=c;
p+=3;}
}
int grab_init() {
if ((grab_fd = open("/dev/video0",O_RDWR)) == -1 ) {
perror("open videodev");
exit(0);
}
if ((ioctl(grab_fd,VIDIOCGCAP,&grab_cap)) == -1)
{
fprintf(stderr,"wrong device\n");
exit(0);
}
memset (&grab_pic, 0, sizeof(struct video_picture));
if (ioctl (grab_fd, VIDIOCGPICT, &grab_pic) == -1) {
fprintf (stderr, "no pict");
exit(0);
}
grab_buf.format = VIDEO_PALETTE_RGB24;
grab_buf.frame = 0;
grab_buf.width = x;
grab_buf.height = y;
grab_size = x* y* w;
grab_data = mmap(0,grab_size,PROT_READ|PROT_WRITE,MAP_SHARED,grab_fd,0);
if (grab_data==(unsigned char *)-1)
{
perror("mmap");
exit(-1);
}
return(1);
}
unsigned char* grab_one(int *width, int *height) {
if (ioctl(grab_fd,VIDIOCMCAPTURE,&grab_buf)==-1) {
perror("ioctl VIDIOCMCAPTURE");
} else {
if (ioctl(grab_fd,VIDIOCSYNC,&grab_buf)==-1) {
perror("ioctl VIDIOCSYNC");
} else {
swap_rgb24(grab_data,grab_buf.width*grab_buf.height);
*width = grab_buf.width;
*height = grab_buf.height;
return( grab_data);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -