主题 : CMOS摄像头程序代码 复制链接 | 浏览器收藏 | 打印
级别: 侠客
UID: 47708
精华: 0
发帖: 108
金钱: 565 两
威望: 113 点
贡献值: 0 点
综合积分: 216 分
注册时间: 2011-05-24
最后登录: 2019-03-17
楼主  发表于: 2012-05-02 23:00

 CMOS摄像头程序代码

#include <sys/time.h>
#include <sys/types.h>
#include <asm/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include "videodev.h"
#include <linux/fb.h>

#define PIXFMT_NUM 5
#define INPUT_NUM 5
#define CTRL_NUM 100
#define ScreenWidth_43 480 //4.3inch lcd width
#define ScreenHigh_43     272 //4.3inch lcd width

#define COLS 320//640           //·Ö±æÂÊ
#define ROWS 240//480
//#define JPEG_TEST

//#define V4L2_DEV_NODE "/dev/video/preview"
#define V4L2_DEV_NODE "/dev/video0"
#define FB_DEV_NODE "/dev/fb0"

static int fb_xres = ScreenWidth_43;
static int fb_yres = ScreenHigh_43;


typedef struct v4l2_input    V_INPUT;
typedef struct v4l2_format   V_FORMAT;
typedef struct v4l2_fmtdesc  V_FMTDESC;
typedef struct v4l2_queryctrl V_QRYCTRL;

typedef struct fb_var_screeninfo F_VINFO;

int main(void)
{
    __u16 x,y;
    int v4l2_fd = -1;
    int fb_fd = -1;
    int n=0;

    unsigned char *fb_buf;
    unsigned char *buf;
    unsigned char *fb_buf_addr;
    unsigned char *buf_addr;


    __u32 screensize;
    //__u16 rgb565[COLS*ROWS];
    V_FORMAT fmt;
    V_INPUT inp;
    V_QRYCTRL qc;
    V_FMTDESC pix;
    struct v4l2_capability caminfo;
    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 ERROR\n");
        return 0;
    }
    screensize = ScreenWidth_43 * ScreenHigh_43 * 2;  // 16bpp

    fb_buf = (unsigned char *)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fb_fd, 0);


    fb_buf_addr = fb_buf;
    if ((int)fb_buf == -1) {
        printf("Error: failed to map framebuffer device to memory.\n");
        close(fb_fd);
             return -1;
    }



    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    fmt.fmt.pix.width = COLS;
    fmt.fmt.pix.height = ROWS;
    fmt.fmt.pix.depth = 16;
    //fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
    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(COLS*ROWS*4);
    buf_addr = buf;    

        
    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)
    {

        fb_buf = fb_buf_addr;
        buf = buf_addr;

        n=read(v4l2_fd, buf, COLS*ROWS*4);
        //printf("display %d\n",n);
    //    show_rgb565_img(fb_buf,buf);

    //    fb_buf += (ScreenWidth_43*14); //Æ«ÒÆ14ÐÐ
        
        
//            for( y=0; y<ROWS; y++)
            for( y=0; y<240; y++)
            {
                for( x=0; x<COLS*2; x++)
                {    //calculate 2 time
                        fb_buf[x]   = *buf++ ; //Æ«ÒÆ10ÁÐ
        //                usleep(100);
                }
                fb_buf += ScreenWidth_43*2 ;
                buf = buf + 320*2;
        //        sleep(2);
            }
    }
}




级别: 新手上路
UID: 64930
精华: 0
发帖: 21
金钱: 110 两
威望: 22 点
贡献值: 0 点
综合积分: 42 分
注册时间: 2012-03-12
最后登录: 2012-09-26
1楼  发表于: 2012-05-03 17:12
哇~!!!顶起啊!!好好学习下