OK6410 v4l2 采集OV9650图片并显示在LCD上

  • #include <sys/time.h>
    #include <sys/types.h>
    #include <asm/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/mman.h>
    #include <errno.h>
    
    #include <jpeglib.h>
    
    #include <linux/fs.h>
    #include <linux/kernel.h>
    #include "videodev.h"
    #include <linux/fb.h>
    
    typedef struct v4l2_format   V_FORMAT;
    #define Picture_Width	 	320
    #define Picture_Height 		240
    
    #define LCD_Width	 		480
    #define LCD_Height 			272
    
    #define V4L2_DEV_NODE "/dev/video0"
    #define FB_DEV_NODE "/dev/fb0"
    
    static void show_rgb565_img(void *scr, __u16 *data_buf)
    {	
    	__u16 x,y;
    	__u32	i=0;
    	__u16 *fb_buf = (__u16 *)scr;
    	// LCD 480*272   272-240=32  32/2=16;
    	// 480-320=160   160/2=60
    	fb_buf += (LCD_Width*16+60);
    		
    	for(y=0; y<Picture_Height/*240*/; y++) 
    	{
    		for(x=0; x<Picture_Width/*320*/; x++)
    		{	
    			fb_buf[x+28]   = *data_buf++ ;						
    		}
    		fb_buf += LCD_Width;
    	}
    }
    
    
    int main(void)
    {
    	int v4l2_fd = -1;
    	int fb_fd = -1;
    	int n=0;
    	__u8 *fb_buf;
    	__u8 *buf;
    	__u32 screensize;
    	V_FORMAT fmt;
    
    	char c_start='O';
    	int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    	
    	printf("Start Main \n");
    	v4l2_fd = open(V4L2_DEV_NODE, O_RDWR);
    	printf("already open the devicise codec \n");
    	if (v4l2_fd < 0) {
    		printf(" open video ERR\n");
    		return 0;
    	}
    
    	printf("Before openning FB \n");
    	fb_fd = open(FB_DEV_NODE, O_RDWR);
    	if (fb_fd < 0) {
    		printf(" open FB ERR\n");
    		return 0;
    	}
    	
    	screensize = LCD_Width * LCD_Height* 16 / 8;//LCD 480*272 一个像素2个字节
    	fb_buf = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
    				fb_fd, 0);
    	if ((int)fb_buf == -1) {
    		printf("Error: failed to map framebuffer device to memory.\n");
    		close(fb_fd);
    	     	return -1;
    	}
    /*
    	if((n =(ioctl(v4l2_fd, VIDIOC_QUERYCAP, &caminfo)))<0)
    	 {
    		printf("Error: failed to get camera info.%d\n",n);
    		return 0;
    	}
    */
    	 //设置图象模式
    	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    	fmt.fmt.pix.width = Picture_Width;
    	fmt.fmt.pix.height = Picture_Height;
    	fmt.fmt.pix.depth = 16;
    	fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
    		
    	printf("VIDIOC_S_FMT is %x\n",VIDIOC_S_FMT);
    	if((ioctl(v4l2_fd, VIDIOC_S_FMT, &fmt))<0)
    	{
    		printf("Error: failed to set video format.\n");
    		return 0;
    	}
    	buf = malloc(Picture_Width*Picture_Height*2);//一个图片大小
    		
    
    		
    	printf("VIDIOC_STREAMON is %x\n",VIDIOC_STREAMON);
    	if((ioctl(v4l2_fd, VIDIOC_STREAMON, &type))<0)
    	{
    		printf("error ioctl streamon\n");
    		return 0;
    	}
    
    	printf("start to get pic %d\n",(int)fmt.fmt.pix.sizeimage);
    	while(1){
    		n=read(v4l2_fd, buf, Picture_Width*Picture_Height*2);
    		printf("display %d\n",n);
    		show_rgb565_img(fb_buf,buf);
    	}
    
    }
    

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值