|
发表于 2006-4-17 13:09:41
|
显示全部楼层
[code:1]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xvlib.h>
#define YUV420P_SIZE 115200
#define RGB_SIZE 230400
Display* display;
Window win;
GC gc;
int create_win(int x,int y,int width,int height)
{
display = XOpenDisplay(NULL);
if (display == NULL) {
printf("cannot open display\n");
return -1;
}
win = XCreateSimpleWindow(display, RootWindow(display, 0),
x, y, width, height, 2,
WhitePixel(display, 0),
BlackPixel(display, 0));
gc=XCreateGC(display, win, 0, NULL);
XSelectInput(display,win,KeyPressMask|StructureNotifyMask);
XMapWindow(display, win);
XMoveWindow(display,win,x,y);
XSync(display, False);
}
XvImage *create_XvImage(Display *dsp,Window window,void *imgdata,XvPortID *xvport)
{
int i,num;
XvImage *image;
unsigned int nadap=0;
XvAdaptorInfo *adaps;
XvImageFormatValues *format;
XvQueryAdaptors(dsp,window,&nadap,&adaps);
for(i=0;i<nadap;i++)
{
if((adaps[i].type & XvInputMask) && (adaps[i].type & XvImageMask))
{
*xvport=adaps[i].base_id;
break;
}
}
if(*xvport < 0)
{
printf("Xv Port failed\n");
return NULL;
}
format=XvListImageFormats(dsp,*xvport,&num);
if(!format)
{
printf("XvListImageFormats failed\n");
return NULL;
}
image=XvCreateImage(dsp,*xvport,format[1].id,imgdata,320,240);
return image;
}
Bool predicate(Display *dsp,XEvent *event,XPointer arg)
{
return True;
}
main()
{
void *imgdata=NULL;
XvImage *image;
XvPortID xvport;
int fd;
int imgsize,i;
XEvent xevent;
fd = open("/dev/video0",O_RDWR);
if(!fd)
return;
create_win(50,50,320,240);
imgsize=YUV420P_SIZE;
imgdata=malloc(imgsize);
image=create_XvImage(display,win,imgdata,&xvport);
if(!image)
return;
while(1)
{
XCheckIfEvent(display,&xevent,predicate,NULL);
if(xevent.type==KeyPress)
{
xevent.type=0;
break;
}
read(fd,imgdata,imgsize);
XvPutImage(display,xvport,win,gc,image,0,0,320,240,0,0,320,240);
}
failed:
XCloseDisplay(display);
close(fd);
}
[/code:1] |
|