打印

V4L2摄像头驱动及程序代码 GEC6818开发板

[复制链接]
132|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
棋王高手|  楼主 | 2018-10-6 12:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这个代码实测可用于GEC6818的开发板



单片机源程序如下:

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/mman.h>

#include <linux/input.h>

#include <strings.h>

#include "jpeglib.h"

#include "api_v4l2.h"

#include <stdlib.h>

#include <pthread.h>



#define LCD_WIDTH 800

#define LCD_HEIGHT 480



int *p = NULL;

int lcd_fd = -1;

int is_open_camera = 0;

int is_video_play = 0;

int is_vedio_record = 0;

int video_count=1;

FrameBuffer camera_buf;

int video_fd;

char bmp[3][10] =

{

        "1.bmp",

        "2.bmp",

        "girl.bmp"

};



int lcd_open(void)

{

        lcd_fd = open("/dev/fb0",O_RDWR);

        if(lcd_fd < 0)

        {

                perror("lcd open failed");

                return 0;

        }

        p = mmap(NULL, LCD_WIDTH*LCD_HEIGHT*4,

                        PROT_READ | PROT_WRITE,

                        MAP_SHARED,

                        lcd_fd,

                        0);

        if (NULL == p)

        {

                perror("lcd mmap error: ");

                return -1;

        }

        return 0;

}



int lcd_draw_point(int x, int y, int color)

{

        if (x > 800)

        {

                printf("value of X-coordinates is over: %d\n", LCD_WIDTH);

                return -1;

        }



        if (y > 480)

        {

                printf("value of Y-coordinates is over: %d\n", LCD_HEIGHT);

                return -1;

        }

        *(p + x + y*800) = color;

        return 0;

}



void lcd_draw_bmp(const char *file_name, int x0, int y0)

{

        int fd;

        int bmp_width;

        int bmp_height;

        char buf[54];

        fd = open(file_name, O_RDONLY);

        if (fd < 0)

        {

                perror("open file error: ");

                return ;

        }

                /* 读取位图头部信息 */

        read(fd,buf,54);

        

        /* 宽度  */

        bmp_width =buf[18];

        bmp_width|=buf[19]<<8;

        //printf("bmp_width=%d\r\n",bmp_width);

        

        /* 高度  */

        bmp_height =buf[22];

        bmp_height|=buf[23]<<8;

        //printf("bmp_height=%d\r\n",bmp_height);        



        

        int len;

        len = bmp_width*bmp_height*3;

        

        //跳过 前边54个字节,这54个字节是用来存储bmp图片的相关信息的

        lseek(fd, 54, SEEK_SET);

        

        //从第55个字节开始,读取bmp图片的像素数组

        char bmp_buf[len];

        read(fd, bmp_buf, len);//因为这个bmp图片是24位色

        close(fd);

        int color, x, y, i = 0;

        unsigned char r, g, b;

        for (y = 0; y < bmp_height; y++)

        {

                for (x = 0; x < bmp_width; x++)

                {        

                        //将一个24bit //bmp图片的像素点转换为LCD的一个像素点

                        b = bmp_buf[i++];

                        g = bmp_buf[i++];

                        r = bmp_buf[i++];

                        color = (r << 16) | (g << 8) | b;

                        lcd_draw_point(x + x0, bmp_height-y+y0-1, color);

                }

        }

}



int get_pos(int *pos_x,int *pos_y)

{

        // 打开触摸屏设备

        struct input_event buf;

        bzero(&buf, sizeof(buf));

        int ts = open("/dev/input/event0", O_RDONLY);

        if(ts < 0)

        {

                perror("open touch srceen failed");

                return -1;

        }

        while(1)

        {

                // 循环地读取触摸屏信息

                read(ts, &buf, sizeof(buf));

                // 遇到 X轴 坐标事件

                if(buf.type == EV_ABS && buf.code == ABS_X)

                {

                        *pos_x = buf.value;

                        printf("(%d, ", *pos_x);

                }



                // 遇到 Y轴 坐标事件

                if(buf.type == EV_ABS && buf.code == ABS_Y)

                {

                        *pos_y = buf.value;

                        printf("%d)\n", *pos_y);

                }

                //判断手指松开

                if (buf.value == 0)

                        break;

        }

}





/****************************************************

*函数名称:file_size_get

*输入参数:pfile_path        -文件路径

*返 回 值:-1                -失败

                   其他值        -文件大小

*说        明:获取文件大小

****************************************************/

unsigned long file_size_get(const char *pfile_path)

{

        unsigned long filesize = -1;        

        struct stat statbuff;

        

        if(stat(pfile_path, &statbuff) < 0)

        {

                return filesize;

        }

        else

        {

                filesize = statbuff.st_size;

        }

        

        return filesize;

}



/*



x                        :起点

y                        :起点

pjpg_path        :图片路径

pjpg_buf    :摄像头获取的图像数据,如果你没用摄像头一般设为NULL

jpg_buf_size:摄像头数据的大小,默认设为0

*/

int lcd_draw_jpg(unsigned int x,unsigned int y,const char *pjpg_path,char *pjpg_buf,unsigned int jpg_buf_size)  

{

        /*定义解码对象,错误处理对象*/

        struct         jpeg_decompress_struct         cinfo;

        struct         jpeg_error_mgr                         jerr;        

        char         g_color_buf[800*480*4];

        char         *pcolor_buf = g_color_buf;

        char         *pjpg;

        

        unsigned int         i=0;

        unsigned int        color =0;

        unsigned int        count =0;

        

        unsigned int         x_s = x;

        unsigned int         x_e ;        

        unsigned int         y_e ;

        

                         int        jpg_fd;

        unsigned int         jpg_size;

        

        unsigned int         jpg_width;

        unsigned int         jpg_height;

        



        if(pjpg_path!=NULL)

        {

                /* 申请jpg资源,权限可读可写 */        

                jpg_fd=open(pjpg_path,O_RDWR);

               

                if(jpg_fd == -1)

                {

                   printf("open %s error\n",pjpg_path);

                  

                   return -1;        

                }        

……………………

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

378

主题

378

帖子

0

粉丝