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

📄 test_cam.c

📁 linux下使用摄像头
💻 C
字号:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <linux/videodev.h>


typedef struct{ 
int fd; 
int use_mmap; 
int width, height; 
int frame_rate; 
int frame_size; 
struct video_capability capability; 
struct video_buffer buffer; 
struct video_window vwin; 
struct video_picture picture; 
struct video_mmap vmmap; 
struct video_mbuf vmbuf; 
unsigned char *frame_buffer; 
int dev; 
int capturing;
int buffer_size ;
} video_device; 


#define DEFAULT_DEVICE "/dev/v4l/video0" ;

int camera_open(char *dev, video_device *vd); 
int camera_close(video_device *vd); 
int camera_get_capability(video_device *vd); 
int camera_get_picture(video_device *vd); 
//int camera_grab_init(v4l_device *vd, int input, int norm); 
unsigned char * camera_grab_image(video_device *vd); 
//int camera_grab_sync(v4l_device *vd); 
//int camera_mmap_init(v4l_device *vd); 
//int camera_get_mbuf(v4l_device *vd);

video_device vd; 

int camera_open (char *dev, video_device *vd) 
{ 
	printf("step1 \n");
	
	//if (!dev) 
	//dev = DEFAULT_DEVICE; 
	
	
	if ((vd->fd = open("/dev/v4l/video0", O_RDWR))<0)
	{
		printf("open error \n");
		return -1;	
	} 
	
	
	printf("step2 \n");
	
	if (camera_get_capability(vd)<0)
	{
		printf("camera_get_capability error \n");
		return -1;	
	} 
	
	printf("step3 \n");
	
	if (camera_get_picture(vd)<0)
	{
		printf("camera_get_picture error \n");
		return -1;	
	} 
	
	printf("step4 \n");

	return 0;


}


int camera_get_capability(video_device *vd)
{ 
	if (ioctl(vd->fd, VIDIOCGCAP, &(vd->capability))<0)
	{		
	return -1;
	}
	return 0;
}

int camera_get_picture(video_device *vd) 
{ 
	if (ioctl(vd->fd, VIDIOCGPICT, &(vd->picture))<0)
	{		
	return -1;
	}
	return 0;

}

int camera_close(video_device *vd) 
{
	close(vd->fd); 
	return 0; 
} 

unsigned char * camera_grab_image(video_device *vd) { 
int len; 
int i; 
//assert(vd != (video_device *)0); 
	if (vd->use_mmap) { 
		if (!vd->capturing) { 
			// 等待请求获取完整的帧 
			for (i = 0; i< vd->vmbuf.frames; ++i) { 
				vd->vmmap.frame = i; 
				if (ioctl(vd, VIDIOCMCAPTURE, &vd->vmmap)) 
				{ 
				perror("VIDIOCMCAPTURE"); 
				return 0; 
				} 
			} 
			// 从零开始读 
			vd->vmmap.frame = 0; 
			vd->capturing = 1; 
		} 
		
			// 给ioctl()传入VIDIOCSYNC,检查帧是否已经获取完成 
		if (ioctl(vd->dev, VIDIOCSYNC, &vd->vmmap.frame)) { 
		//perror("VIDIOCSYNC:"); 
		return 0; 
		} 
		
		return vd->frame_buffer + vd->vmbuf.offsets[vd->vmmap.frame]; 
	} 
	
	// 否则去读取确切的大小 
	len = read(vd->dev, vd->frame_buffer, vd->buffer_size); 
	if(len<=0)
	return 0;
	
	if (len!=vd->buffer_size) { 
	fprintf(stderr, "Expected to read %d bytes, actually read %d\n", vd->buffer_size, len); 
	return 0; 
	} 
	
	return vd->frame_buffer; 
	
} 



int main()
{
	if (camera_open("/dev/v4l/video0", &vd)) 
	return -1; 

	while(1){
		sleep(1);
		camera_grab_image(&vd);
	}
	
	camera_close(&vd);
	
	return 1;

}

⌨️ 快捷键说明

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