通过摄像头设备采集一帧数据的例子程序(完整版)

本文提供了一个通过摄像头设备采集一帧数据的完整程序实例,详细讲解了如何操作摄像头并获取图像数据,适用于计算机视觉或图像处理的相关项目。
摘要由CSDN通过智能技术生成

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <getopt.h>           

#include <fcntl.h>             
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <asm/types.h>         
#include <linux/videodev2.h>


#define VIDEO_WIDTH 640   
#define VIDEO_HEIGHT 480  
#define BUFFER_COUNT 4   

#define CLEAR(x) memset (&(x), 0, sizeof (x))


struct buffer {
	void * 		start;		//记录缓冲帧地址
	size_t 		length;		//一帧的大小
};


//pic process---------------------------------------------------------------------------------------------------------------------------------------------------------------

#pragma pack(1)  
typedef struct BITMAPFILEHEADER{
      unsigned short bfType;//位图文件的类型,  
      unsigned long bfSize;//位图文件的大小,以字节为单位  
      unsigned short bfReserved1;//位图文件保留字,必须为0  
      unsigned short bfReserved2;//同上  
      unsigned long bfOffBits;//位图阵列的起始位置,以相对于位图文件   或者说是头的偏移量表示,以字节为单位  
} BITMAPFILEHEADER;
#pragma pack()  

typedef struct BITMAPINFOHEADER{//位图信息头类型的数据结构,用于说明位图的尺寸      
      unsigned long biSize;//位图信息头的长度,以字节为单位  
      unsigned long biWidth;//位图的宽度,以像素为单位  
      unsigned long biHeight;//位图的高度,以像素为单位  
      unsigned short biPlanes;//目标设备的级别,必须为1  
      unsigned short biBitCount;//每个像素所需的位数,必须是1(单色),4(16色),8(256色)或24(2^24色)之一  
      unsigned long biCompression;//位图的压缩类型,必须是0-不压缩,1-BI_RLE8压缩类型或2-BI_RLE4压缩类型之一  
      unsigned long biSizeImage;//位图大小,以字节为单位  
      unsigned long biXPelsPerMeter;//位图目标设备水平分辨率,以每米像素数为单位  
      unsigned long biYPelsPerMeter;//位图目标设备垂直分辨率,以每米像素数为单位  
      unsigned long biClrUsed;//位图实际使用的颜色表中的颜色变址数  
      unsigned long biClrImportant;//位图显示过程中被认为重要颜色的变址数  
} BITMAPINFOHEADER;


void create_bmp_header(struct BITMAPFILEHEADER * bfh,struct BITMAPINFOHEADER * bih){
      bfh->bfType = (unsigned short)0x4D42;
      bfh->bfSize = (unsigned long)(14 + 40 + VIDEO_WIDTH * VIDEO_HEIGHT*3);
      bfh->bfReserved1 = 0;
      bfh->bfReserved2 = 0;
      bfh->bfOffBits = (unsigned long)(14 + 40);


      bih->biBitCount = 24;
      bih->biWidth = VIDEO_WIDTH;
      bih->biHeight = VIDEO_HEIGHT;
      bih->biSizeImage = VIDEO_WIDTH * VIDEO_HEIGHT * 3;
      bih->biClrImportant = 0;
      bih->biClrUsed = 0;
      bih->biCompression = 0;
      bih->biPlanes = 1;
      bih->biSize = 40;//sizeof(bih);  
      bih->biXPelsPerMeter = 0x00000ec4;
      bih->biYPelsPerMeter = 0x00000ec4;

     printf("----create bmp header successfully !----\n");
}

void store_bmp(FILE * fd,char * file_name,unsigned char * newBuf,int bmp_len,struct BITMAPFILEHEADER * bfh,struct BITMAPINFOHEADER * bih){
    
    fd = fopen(file_name, "wb");
    if (fd < 0) {
            printf("open frame data file failed\n");
            return;
    }
    fwrite(bfh,sizeof(*bfh),1,fd);
    fwrite(bih,sizeof(*bih),1,fd);
    fwrite(newBuf, 1, bmp_len, fd);
    fclose(fd);
    prin
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值