|
/****************************************************/
/* */
/* v4lgrab.c */
/* */
/****************************************************/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev.h>
#include "v4lgrab.h"
#define TRUE 1
#define FALSE 0
#define WIDTHBYTES(i) ((i+31)/32*4)
#define FILE_VIDEO "/dev/video0"
//#define BMP "image.bmp"
#define IMAGEWIDTH 176
#define IMAGEHEIGHT 144
static int fd;
static struct video_capability cap;
static struct video_window win;
static struct video_picture vpic;
int openVideo()
{
//Open video device
fd = open(FILE_VIDEO, O_RDONLY);
if (fd < 0) {
perror(FILE_VIDEO);
return (FALSE);
}
//Get capabilities
if (ioctl(fd, VIDIOCGCAP, &cap) < 0) {
perror("VIDIOGCAP");
printf("(" FILE_VIDEO " not a video4linux device?)\n");
close(fd);
return (FALSE);
}
//Get the video overlay window
if (ioctl(fd, VIDIOCGWIN, &win) < 0) {
perror("VIDIOCGWIN");
close(fd);
return (FALSE);
}
//Set the video overlay window
win.width = IMAGEWIDTH;
win.height = IMAGEHEIGHT;
if(ioctl(fd, VIDIOCSWIN, &win) <0) {
perror("VIDIOCSWIN");
close(fd);
return (FALSE);;
}
//Get picture properties
if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) {
perror("VIDIOCGPICT");
close(fd);
return (FALSE);
}
// if (cap.type & VID_TYPE_MONOCHROME) {
// } else {
vpic.depth=24;
vpic.palette=VIDEO_PALETTE_RGB24;
if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
printf("Unable to find a supported capture format.\n");
return (FALSE);
}
// }
return (TRUE);
}
int closeVideo()
{
if(fd != -1) {
close(fd);
return (TRUE);
}
return (FALSE);
}
void getVideoData(unsigned char *data, int size)
{
size = read(fd, data, size);
}
/*
int main(int argc, char ** argv)
{
FILE * fp;
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
unsigned char * buffer;
int i;
fp = fopen(BMP, "wb");
if(!fp){
perror(BMP);
exit(1);
}
if(openVideo() == FALSE) {
return(-1);
}
//Set BITMAPINFOHEADER
bi.biSize = 40;
bi.biWidth = win.width;
bi.biHeight = win.height;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = 0;
bi.biSizeImage = WIDTHBYTES(bi.biWidth * bi.biBitCount) * bi.biHeight;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
//Set BITMAPFILEHEADER
bf.bfType = 0x4d42;
bf.bfSize = 54 + bi.biSizeImage;
bf.bfReserved = 0;
bf.bfOffBits = 54;
buffer = malloc(bi.biSizeImage);
if (!buffer) {
printf("Out of memory.\n");
exit(1);
}
for(i=0; i<20; i++)
getVideoData(buffer, bi.biSizeImage);
fwrite(&bf, 14, 1, fp);
fwrite(&bi, 40, 1, fp);
fwrite(buffer, bi.biSizeImage, 1, fp);
printf("get bmp form video\t[OK]\n");
fclose(fp);
closeVideo();
return 0;
}
*/
|