LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 592|回复: 0

大虾 进来 看看!!!!!!!!!!!!!

[复制链接]
发表于 2006-6-29 10:22:07 | 显示全部楼层 |阅读模式
为什么 这 代码的结果始终不是我 想要的 呢 ?
结果 怎么 只是一个0X0像素的BMP呢 ??????
如果要 mmap方式采集该怎么 改?
在线等 。。。。。。。。。。。。。。。。。。。。。。
#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 "stdio.h"
#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 "videodev.h"
#define BMP      "image.bmp"
typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER{
     WORD bfType;                // the flag of bmp, value is "BM"
     DWORD    bfSize;                // size BMP file ,unit is bytes
     DWORD    bfReserved;            //
     DWORD    bfOffBits;             // must be 54
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
     DWORD    biSize;                // must be 0x28
     DWORD    biWidth;           //
     DWORD    biHeight;          //
     WORD biPlanes;          // must be 1
     WORD biBitCount;            //
     DWORD    biCompression;         //
     DWORD    biSizeImage;       //
     DWORD    biXPelsPerMeter;   //
     DWORD    biYPelsPerMeter;   //
     DWORD    biClrUsed;             //
     DWORD    biClrImportant;        //
}BITMAPINFOHEADER;
struct video_capability capability;
struct video_picture picture;
struct video_mmap grap_buf;
struct video_mbuf grab_vm;
struct video_window win;

int main(int argc,char**argv)
{
int grab_fd;
int openvideo()
{
grab_fd=open("/dev/video0",O_RDWR);
if(grab_fd<0)
{
perror("/dev/video0");
  return(0);
}
if(ioctl(grab_fd,VIDIOCGCAP,&capability)<0)
{
perror("VIDIOCGCAP");
return(0);
}
if(ioctl(grab_fd,VIDIOCGWIN,&win)<0)
{
perror("VIDIOCGWIN");
return(0);
}
win.width=176;
win.height=144;
if(ioctl(grab_fd,VIDIOCSWIN,&win)<0)
{
perror("VIDIOCSWIN");
return(0);
}
if (ioctl(grab_fd, VIDIOCGPICT, &picture) < 0) {
         perror("VIDIOCGPICT");

         return (0);
     }
picture.depth=24;
picture.palette=VIDEO_PALETTE_RGB24;
if (ioctl(grab_fd, VIDIOCSPICT, &picture) < 0) {
         printf("unable to find a supported caputre format\n");

         return (0);
     }
return (1);
}

int closevideo()
{
if(grab_fd!=-1)
{
close (grab_fd);
return(1);
}
return(0);
}
void getVideoData(unsigned char *data,int size)
{
size=read(grab_fd,data,size);
}

FILE   *  fp;
BITMAPFILEHEADER   bf;
BITMAPINFOHEADER   bi;
  unsigned char * buffer;
  int i;
  fp = fopen(BMP, "wb");
  if(!fp){
      perror(BMP);
       exit(1);
    }
     // if(openVideo() == 0) {
       //  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 = 176;
     bi.biXPelsPerMeter = 0;
     bi.biYPelsPerMeter = 0;
     bi.biClrUsed = 0;
     bi.biClrImportant = 0;
     //Set BITMAPFILEHEADER
    bf.bfType = 0x4d42;
     bf.bfSize =230;
     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;


}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表