⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testvideo.c

📁 基于SDL的framegrabber适用于简单的UI下的webcam应用开发
💻 C
字号:
/*     TestVideo - SDL_bgrab Sample Player*/#ifdef WIN32 #include <windows.h>#endif#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include <ctype.h>#include <SDL/SDL.h>#include "SDL/SDL_bgrab.h"#define DEFAULT_W	640#define DEFAULT_H	480/* Commandline configurable items */char *bgrab_device = NULL;int framerate = 60;int deinterlace = 1;int channel = 1;int mode = 1;int region = 1;int frequency = 1;char *channel_name[3] = {"Tuner","Composite","SVideo"};char *mode_name[3] = {"PAL","NTSC","SECAM"};char *deinterlace_name[4] = {"none","blend","smart blend","smooth blend"};/* Drawing loop */void Draw(SDL_Surface *screen, tSDL_bgrab *bgrab){ SDL_Event event;  int inloop;    /* Black screen */ SDL_FillRect(screen,NULL,0); SDL_UpdateRect(screen,0,0,0,0);   inloop=1; while (inloop) {      /* Check for events */  while ( SDL_PollEvent(&event) ) {   switch (event.type) {    case SDL_KEYDOWN:     if (event.key.keysym.sym == 'd') {      deinterlace = ((deinterlace + 1) % 4) ;      fprintf (stderr,"Deinterlace method: %i (%s)\n",deinterlace,deinterlace_name[deinterlace]);     } else     if (event.key.keysym.sym == 'c') {      channel = ((channel + 1) % 3) ;      bgrabSetChannel(bgrab,channel,mode);      fprintf (stderr,"Input Channel: %i (%s)\n",channel,channel_name[channel]);     } else     if (event.key.keysym.sym == 'm') {      mode = ((mode + 1) % 3) ;      bgrabSetChannel(bgrab,channel,mode);      fprintf (stderr,"Video Mode: %i (%s)\n",mode,mode_name[mode]);     } else     if (event.key.keysym.sym == 'r') {      region = ((region + 1) % 9) ;      bgrabSetFrequency(bgrab,region,frequency);      fprintf (stderr,"Frequency Region: %i\n",region);     } else     if (event.key.keysym.sym == 'f') {      frequency = ((frequency + 1) % 128) ;      bgrabSetFrequency(bgrab,region,frequency);      fprintf (stderr,"Frequency Index: %i\n",frequency);     } else     if (event.key.keysym.sym == SDLK_ESCAPE) {      inloop=0;     }     break;         case SDL_QUIT:     inloop=0;     break;    }   }   /* Blit Video screen */   if (bgrabBlitFramebuffer(bgrab, screen, deinterlace) ) {    /* Display by updating display */    SDL_UpdateRect(screen,0,0,0,0);   }       /* Delay to limit rate */                      SDL_Delay(1000/framerate);    }}void PrintUsage(){ fprintf (stderr,"Usage: TestVideo [SDL parameters] [Video parameters]\n"); fprintf (stderr," SDL parameters\n"); fprintf (stderr,"  -width [i]		Set screen width (default: %i)\n",DEFAULT_W); fprintf (stderr,"  -height [i]		Set_screen height (default: %i)\n",DEFAULT_H); fprintf (stderr,"  -bpp [i]		Set [i] bits per pixel\n"); fprintf (stderr,"  -warp			Use hardware palette\n"); fprintf (stderr,"  -hw			Use hardware surface\n"); fprintf (stderr,"  -fullscreen		Go into fullscreen mode\n"); fprintf (stderr," Video Parameters\n"); fprintf (stderr,"  -device [s]		Video device to use\n"); fprintf (stderr," Program Parameters\n"); fprintf (stderr,"  -framerate [i]	Target framerate for screen display\n");}#ifdef WIN32 extern char ** __argv; extern int __argc; int APIENTRY WinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow)#else // non WIN32 int main ( int argc, char *argv[] )#endif{	SDL_Surface *screen;	int w, h;	int desired_bpp;	Uint32 bgrab_flags;#ifdef WIN32	int argc;	char **argv;	argv = __argv;	argc = __argc;#endif	tSDL_bgrab bgrab;	/* Title */	fprintf (stderr,"SDL_bgrab Sample Video Player - LGPL, A. Schiffler, aschiffler@appwares.com\n\n");	/* Set default options and check command-line */	w = DEFAULT_W;	h = DEFAULT_H;	desired_bpp = 0;	bgrab_flags = 0;	while ( argc > 1 ) {		/* SDL specific arguments */				if ( strcmp(argv[1], "-width") == 0 ) {			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {				argv += 2;				argc -= 2;			} else {				fprintf(stderr,				"The -width option requires an argument\n");				exit(1);			}		} else		if ( strcmp(argv[1], "-height") == 0 ) {			if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {				argv += 2;				argc -= 2;			} else {				fprintf(stderr,"The -height option requires an argument\n");				exit(1);			}		} else		if ( strcmp(argv[1], "-bpp") == 0 ) {			if ( argv[2] ) {				desired_bpp = atoi(argv[2]);				argv += 2;				argc -= 2;			} else {				fprintf(stderr,"The -bpp option requires an argument\n");				exit(1);			}		} else		if ( strcmp(argv[1], "-warp") == 0 ) {			bgrab_flags |= SDL_HWPALETTE;			argv += 1;			argc -= 1;		} else		if ( strcmp(argv[1], "-hw") == 0 ) {			bgrab_flags |= SDL_HWSURFACE;			argv += 1;			argc -= 1;		} else		if ( strcmp(argv[1], "-fullscreen") == 0 ) {			bgrab_flags |= SDL_FULLSCREEN;			argv += 1;			argc -= 1;		} else				/* Video specific arguments */		if ( strcmp(argv[1], "-device") == 0 ) {			if (argv[2]) {				bgrab_device = strdup(argv[2]);				argv += 2;				argc -= 2;			} else {				fprintf(stderr,"The -device option requires an argument\n");				exit(1);			}		} else		/* Program specific arguments */				if ( strcmp(argv[1], "-framerate") == 0 ) {			if (argv[2]) {				framerate = atoi(argv[2]);				argv += 2;				argc -= 2;			} else {				fprintf(stderr,"The -framerate option requires an argument\n");				exit(1);			}		} else {			PrintUsage();			exit(0);			break;		}	}	/* Force double buffering */	bgrab_flags |= SDL_DOUBLEBUF;	/* Check/Adjust Video parameters */	if (bgrab_device==NULL) {	 bgrab_device=strdup("/dev/v4l/video0");	 fprintf(stderr,"Defaulting to device '%s'.\n",bgrab_device);        }        if ((framerate<1) || (framerate>300)) {	 fprintf (stderr,"Bad framerate (%i). Use a value from 1 to 300.\n",framerate);	 PrintUsage();         exit(1);	}                	/* Initialize SDL */	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());		exit(1);	}	atexit(SDL_Quit);			/* Clean up on exit */	/* Initialize the display */	screen = SDL_SetVideoMode(w, h, desired_bpp, bgrab_flags);	if ( screen == NULL ) {		fprintf(stderr, "Couldn't set %dx%dx%d bgrab mode: %s\n",					w, h, desired_bpp, SDL_GetError());		exit(1);	}	/* Set the window manager title bar */	SDL_WM_SetCaption("TestVideo", "testbgrab");	/* Start grabbing */	bgrabOpen(&bgrab,bgrab_device);			/* Print some device info */	bgrabPrintInfo(&bgrab);	/* Configure card */	bgrabSetChannel(&bgrab,channel,mode);        fprintf (stderr,"Input Channel: %i (%s)\n",channel,channel_name[channel]);        fprintf (stderr,"Video Mode: %i (%s)\n",mode,mode_name[mode]);        fprintf (stderr,"Frequency Region: %i\n",region);        fprintf (stderr,"Frequency Index: %i\n",frequency);        /* Deinterlace Info */                fprintf (stderr,"Deinterlace method: %i (%s)\n",deinterlace,deinterlace_name[deinterlace]);		/* Keyboard info */	fprintf (stderr,"\nKeyboard:\n\tESC = quit\n\tm = video mode\n\tc = input channel\n\td = deinterlace mode\n\tr = frequency region\n\tf = frequency\n\n");	/* Start grabbing */	bgrabStart(&bgrab,640,480 , 1);		/* Do all the drawing work */	Draw (screen, &bgrab);			/* Stop grabbing */	bgrabStop(&bgrab);	bgrabClose(&bgrab);		return(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -