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

📄 张昆修改.c

📁 摄像头ov7620读取 并支持ARM芯片 获取图像到LCD
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************Copyright (c)**************************************************
**                                        cc.c
**                   功能:在英蓓特实验箱STN LCD上显示两个ov511摄像头测距数字的程序
**                                  
**
**--------------File Info-------------------------------------------------------------------------------

** Created By:陈超	            删掉试试编译,,程序能编译成功,,在试验箱能运行就行了
** Created date: 2008-03-07 ,,估计是宏定义和头文件+子函数一起配套的,,少了的话可能有的子函数不能工作,
** Version: v1.0
** Descriptions:

********************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/videodev.h>
#include <sys/ioctl.h>
#include<string.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <math.h>


#define ERR_FRAME_BUFFER	       1
#define ERR_VIDEO_OPEN		       2
#define ERR_VIDEO_GCAP		       3
#define ERR_VIDEO_GPIC		       4
#define ERR_VIDEO_SPIC		       5
#define ERR_SYNC		           6
#define ERR_FRAME_USING            7
#define ERR_GET_FRAME              8

typedef struct	_fb_v4l
{
	// FrameBuffer 信息
	int fbfd ;							                  // FrameBuffer设备句柄
	struct fb_var_screeninfo vinfo;		                  // FrameBuffer屏幕可变的信息
	struct fb_fix_screeninfo finfo;		                  // FrameBuffer固定不变的信息
	char *fbp;							                  // FrameBuffer 内存指针
	// video4linux信息  
	int fd;							                      // 
	struct video_capability 	capability;	                  // 
	struct video_buffer 	    buffer;			              // 
	struct video_window   	window;			              // 
	struct video_channel 	channel[8];	                  // 
	struct video_picture 	picture;		              // 
	struct video_tuner 	    tuner;			              // 
	struct video_audio 	    audio[8];		              // 
	struct video_mmap 	    mmap;				          // 
	struct video_mbuf 	    mbuf;				          // 
	unsigned char 	        *map;					      //mmap方式获取数据时,数据的首地址				
	int frame_current;
	int frame_using[VIDEO_MAX_FRAME];
}fb_v4l1,fb_v4l2;                                                  //定义新类型名fb_v4l,它代表上面定义的结构体类型

#define DEFAULT_PALETTE VIDEO_PALETTE_RGB24

#define FB_FILE "/dev/fb/0"   //设备路径和设备名  device/framebuffer/设备号0


#define V4L_FILE0 "/dev/v4l/video0"
#define V4L_FILE1 "/dev/v4l/video1"
#define X_SIZE 960
#define Y_SIZE 240

/*********************************************************************************************************
** Function name: get_grab_frame
** Descriptions:  获取图像帧,该函数调用了VIDIOCMCAPTURE的ioctl,获取一帧图片
** Input: *vd1,   参数指针    
**        frame,  帧号
** Output : 无
** Created by:函数名: ioctl 
  功 能: 控制I/O设备 
  用 法: int ioctl(int handle, int cmd,[int *argdx, int argcx]); 

** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_grab_frame1(fb_v4l1 *vd1, int frame)     //vd1是一个指向fb_v4l类型结构体的指针变量
{
	if (vd1->frame_using[frame]) {
		fprintf(stderr, "get_grab_frame: frame %d is already used.\n", frame);
		return ERR_FRAME_USING;
	}

	vd1->mmap.frame = frame;       
	if (ioctl(vd1->fd, VIDIOCMCAPTURE, &(vd1->mmap)) < 0)
	{
		perror("v4l_grab_frame");
		return ERR_GET_FRAME;
	}
	vd1->frame_using[frame] = 1;
	vd1->frame_current = frame;
	return 0;
}
int get_grab_frame2(fb_v4l2 *vd2, int frame)     //vd2是一个指向fb_v4l类型结构体的指针变量
{
	if (vd2->frame_using[frame]) {
		fprintf(stderr, "get_grab_frame: frame %d is already used.\n", frame);
		return ERR_FRAME_USING;
	}

	vd2->mmap.frame = frame;
	if (ioctl(vd2->fd, VIDIOCMCAPTURE, &(vd2->mmap)) < 0)
	{
		perror("v4l_grab_frame");
		return ERR_GET_FRAME;
	}
	vd2->frame_using[frame] = 1;
	vd2->frame_current = frame;
	return 0;
}
/*********************************************************************************************************
** Function name: get_next_frame
** Descriptions: 获取下一帧的图像
** Input: *vd1 ,参数指针函数名: ioctl 
  功 能: 控制I/O设备 
  用 法: int ioctl(int handle, int cmd,[int *argdx, int argcx]); 

** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_first_frame1(fb_v4l1 *vd1)
{
	int ret;

	vd1->frame_current = 0;
	ret = get_grab_frame1( vd1, 0 );
	if ( ret<0 )
		return ret;
	// 等待帧同步
	if (ioctl(vd1->fd, VIDIOCSYNC, &(vd1->frame_current)) < 0) 
	{   perror("v4l1_grab_sync");
	return ERR_SYNC;
	}
	vd1->frame_using[vd1->frame_current] = 0 ;		
	return (0);
}
int get_first_frame2(fb_v4l2 *vd2)
{
	int ret;

	vd2->frame_current = 0;
	ret = get_grab_frame2( vd2, 0 );
	if ( ret<0 )
		return ret;
	// 等待帧同步
	if (ioctl(vd2->fd, VIDIOCSYNC, &(vd2->frame_current)) < 0) 
	{   perror("v4l2_grab_sync");
	return ERR_SYNC;
	}
	vd2->frame_using[vd2->frame_current] = 0 ;		
	return (0);
}

/*********************************************************************************************************
** Function name: get_next_frame
** Descriptions: 获取下一帧的图像
** Input: *vd ,参数指针
** Output : 返回0表示正常完成返回。
** Created by:
** Created Date: 
**-----------------------------------------------------------     123123123123123-------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int get_next_frame1(fb_v4l1 *vd1)
{
	int ret;
	vd1->frame_current ^= 1;                                         // ^为位 异或算式运算符
	ret = get_grab_frame1( vd1,vd1->frame_current);			        // 获取图像数据
	//if( ret < 0 )
	//	return ret;

	if (ioctl(vd1->fd, VIDIOCSYNC, &(vd1->frame_current)) < 0)        // 等待帧同步
	{   perror("v4l1_grab_sync");
	return ERR_SYNC;
	}
	vd1->frame_using[vd1->frame_current] = 0 ;
	return 0;	
}
int get_next_frame2(fb_v4l2 *vd2)
{
	int ret;
	vd2->frame_current ^= 1;                                         // ^为位 异或算式运算符
	ret = get_grab_frame2( vd2,vd2->frame_current);			        // 获取图像数据
	//if( ret < 0 )
	//	return ret;

	if (ioctl(vd2->fd, VIDIOCSYNC, &(vd2->frame_current)) < 0)        // 等待帧同步
	{   perror("v4l2_grab_sync");
	return ERR_SYNC;
	}
	vd2->frame_using[vd2->frame_current] = 0 ;
	return 0;	
}
/*********************************************************************************************************
** Function name: get_frame_address
** Descriptions: 获取帧地址.调用该函数可以获取当前帧的缓冲地址
** Input: *vd ,参数指针
** Output : 返回帧图像数据的指针地址.
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
unsigned char *get_frame_address1(fb_v4l1 *vd1)
{
	return (vd1->map + vd1->mbuf.offsets[vd1->frame_current]); 	// 从MAP内存中找到当前帧的起始指针
}
unsigned char *get_frame_address2(fb_v4l1 *vd1)
{
	return (vd1->map + vd1->mbuf.offsets[vd1->frame_current]); 	// 从MAP内存中找到当前帧的起始指针
}
unsigned char *get_frame_address3(fb_v4l2 *vd2)
{
	return (vd2->map + vd2->mbuf.offsets[vd2->frame_current]); 	// 从MAP内存中找到当前帧的起始指针
}
unsigned char *get_frame_address4(fb_v4l2 *vd2)
{
	return (vd2->map + vd2->mbuf.offsets[vd2->frame_current]); 	// 从MAP内存中找到当前帧的起始指针
}
/*******************************************************************************************************************
**把rgb颜色,,,填充到内存framebuffer1中
/**************************************** ****************************************************************************/

void rgb_to_framebuffer1( fb_v4l1 *vd1,				                          // 
						int width,int height,			                      // 图像大小
						int xoffset,int yoffset,		                      // 图像在Framebuffer偏移位置
						unsigned char *img_ptr )                        // 图像数据指针
{
	int x,y;
	int location;
	// Figure out where in memory to put the pixel

	for ( y = 0; y < height; y++ )				                  // 纵扫描
	{
		for ( x = 0; x < width; x++ ) 		              // 行扫描		
		{ 
			location = (x + xoffset) +
				(y + yoffset) * vd1->finfo.line_length;		
			*((unsigned short int*)(vd1->fbp + location )) = *img_ptr++;
		}
	}
}
void rgb_to_framebuffer2( fb_v4l2 *vd2,				                          // 
						int width,int height,			                      // 图像大小
						int xoffset,int yoffset,		                      // 图像在Framebuffer偏移位置
						unsigned char *img_ptr )                        // 图像数据指针
{
	int x,y;
	int location;
	// Figure out where in memory to put the pixel

	for ( y = 0; y < height; y++ )				                  // 纵扫描
	{
		for ( x = 0; x < width; x++ ) 		              // 行扫描		
		{ 
			location = (x + xoffset) +
				(y + yoffset) * vd2->finfo.line_length;		
			*((unsigned short int*)(vd2->fbp + location )) = *img_ptr++;
		}
	}
}
/*********************************************************************************************************
** Function name: open_framebuffer
** Descriptions: 该函数用于初始化FrameBuffer设备,在该函数中打开FrameBuffer设备,并将设备影射到内存
** Input: *ptr,打开Framebuffer设备路径指针
**        *vd ,参数指针
** Output : 返回非0值表示出错
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int open_framebuffer1(char *ptr,fb_v4l1 *vd1)
{
	int fbfd,screensize;
	// Open the file for reading and writing
	fbfd = open( ptr, O_RDWR);
	if (fbfd < 0) 
	{
		printf("Error: cannot open framebuffer device.%x\n",fbfd);
		return ERR_FRAME_BUFFER;
	}
	printf("The framebuffer device was opened successfully1.\n");

	vd1->fbfd = fbfd;	                                                   // 保存打开FrameBuffer设备的句柄

	// Get fixed screen information	获取FrameBuffer固定不变的信息
	if (ioctl(fbfd, FBIOGET_FSCREENINFO, &vd1->finfo)) 
	{
		printf("Error: reading fixed information1.\n");
		return ERR_FRAME_BUFFER;
	}

	// Get variable screen information 获取FrameBuffer屏幕可变的信息
	if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vd1->vinfo)) 
	{
		printf("Error: reading variable information1.\n");
		return ERR_FRAME_BUFFER;
	}

	printf("%dx%d, %dbpp, xoffset=%d ,yoffset=%d \n", vd1->vinfo.xres, 
		vd1->vinfo.yres, vd1->vinfo.bits_per_pixel,vd1->vinfo.xoffset,vd1->vinfo.yoffset );

	// Figure out the size of the screen in bytes用字节
	screensize = vd1->vinfo.xres * vd1->vinfo.yres * vd1->vinfo.bits_per_pixel / 8;

	// Map the device to memory 映射设备到内存
	vd1->fbp = (char *)mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fbfd,0); // 影射Framebuffer设备到内存
	if ((int)vd1->fbp == -1) 
	{
		printf("Error: failed to map framebuffer device to memory1.\n");
		return ERR_FRAME_BUFFER;
	}
	printf("The framebuffer device was mapped to memory successfully1.\n");
	return  0;
}
/*********************************************************************************************************
** Function name: open_framebuffer
** Descriptions: 该函数用于初始化FrameBuffer设备,在该函数中打开FrameBuffer设备,并将设备影射到内存
** Input: *ptr,打开Framebuffer设备路径指针
**        *vd1 ,参数指针
** Output : 返回非0值表示出错
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int open_framebuffer2(char *ptr,fb_v4l2 *vd2)
{
	int fbfd,screensize;
	// Open the file for reading and writing
	fbfd = open( ptr, O_RDWR);
	if (fbfd < 0) 
	{
		printf("Error: cannot open framebuffer device2.%x\n",fbfd);
		return ERR_FRAME_BUFFER;
	}
	printf("The framebuffer device was opened successfully2.\n");

	vd2->fbfd = fbfd;	                                                   // 保存打开FrameBuffer设备的句柄

	// Get fixed screen information	获取FrameBuffer固定不变的信息
	if (ioctl(fbfd, FBIOGET_FSCREENINFO, &vd2->finfo)) 
	{
		printf("Error: reading fixed information2.\n");
		return ERR_FRAME_BUFFER;
	}

	// Get variable screen information 获取FrameBuffer屏幕可变的信息
	if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vd2->vinfo)) 
	{
		printf("Error: reading variable information2.\n");
		return ERR_FRAME_BUFFER;
	}

	printf("%dx%d, %dbpp, xoffset=%d ,yoffset=%d \n", vd2->vinfo.xres, 
		vd2->vinfo.yres, vd2->vinfo.bits_per_pixel,vd2->vinfo.xoffset,vd2->vinfo.yoffset );

	// Figure out the size of the screen in bytes用字节
	screensize = vd2->vinfo.xres * vd2->vinfo.yres * vd2->vinfo.bits_per_pixel / 8;

	// Map the device to memory
	vd2->fbp = (char *)mmap(0,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fbfd,0); // 影射Framebuffer设备到内存
	if ((int)vd2->fbp == -1) 
	{
		printf("Error: failed to map framebuffer device to memory2.\n");
		return ERR_FRAME_BUFFER;
	}
	printf("The framebuffer device was mapped to memory successfully2.\n");
	return  0;
}
/*********************************************************************************************************
** Function name: open_video
** Descriptions: 通过该函数初始化视频设备
** Input: *fileptr,打开的文件名指针
** 	   *vd,     参数指针
** 	   dep,     像素深度
** 	   pal,     调色板
** 	   width,   宽度
** 	   height,  高度
** Output : 无
** Created by:
** Created Date: 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int open_video1( char *fileptr,fb_v4l1 *vd1 ,int dep,int pal,int width,int height)
{
	// 打开视频设备
	if ((vd1->fd = open(fileptr, O_RDWR)) < 0) 
	{
		perror("v4l1_open:");
		return ERR_VIDEO_OPEN;
	}
	// 获取设备
	if (ioctl(vd1->fd, VIDIOCGCAP, &(vd1->capability)) < 0) 
	{
		perror("v4l1_get_capability:");
		return ERR_VIDEO_GCAP;
	}
	// 获取图象  
	if (ioctl(vd1->fd, VIDIOCGPICT, &(vd1->picture)) < 0) 
	{
		perror("v4l1_get_picture");
		return ERR_VIDEO_GPIC;
	}
	// 设置图象
	vd1->picture.palette = pal;		// 调色板
	vd1->picture.depth = dep;			// 像素深度

	vd1->mmap.format =pal;
	if (ioctl(vd1->fd, VIDIOCSPICT, &(vd1->picture)) < 0) 
	{
		perror("v4l1_set_palette");
		return ERR_VIDEO_SPIC;
	}
	// 
	vd1->mmap.width = width; 		// width;
	vd1->mmap.height = height; 	// height;
	vd1->mmap.format = vd1->picture.palette; 

	vd1->frame_current = 0;
	vd1->frame_using[0] = 0;
	vd1->frame_using[1] = 0;

	// 获取缓冲影射信息
	if (ioctl(vd1->fd, VIDIOCGMBUF, &(vd1->mbuf)) < 0) 
	{
		perror("v4l1_get_mbuf");
		return -1;
	}

	// 建立设备内存影射
	vd1->map = mmap(0, vd1->mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, vd1->fd, 0);
	if ( vd1->map < 0) 
	{
		perror("v4l1_mmap_init:mmap");
		return -1;
	}
	printf("The video device was opened successfully1.\n");

⌨️ 快捷键说明

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