粤嵌开发板ARM电子相册

代码实现映射的方式实现图片的随机切换

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#define UP    1
#define DOWN  2
#define LEFT  3
#define RIGHT 4

#define RGB_SIZE 800*480*3
#define LCD_SIZE 800*480
int *p =NULL;//全局变量
void  draw_point(int x, int y, int color)
{
    if(x>=0 && x<800 && y>=0 && y<480)
    {
        *(p + y*800 + x) = color ;
    }
}
int lcd_fd;
int main()
{
    lcdinit();
    //定义一个数组存储图片
    char *c[9]= {"/home/czz/00.bmp","/home/czz/1.bmp","/home/czz/2.bmp","/home/czz/3.bmp","/home/czz/4.bmp","/home/czz/5.bmp","/home/czz/6.bmp",
                 "/home/czz/7.bmp","/home/czz/123.bmp"
                };
    int i;     //初始化图片张数
    //生成随机数实现随机图片切换,需要引入时间库,做随机数种子#include <time.h>
    i=0;
    show_bmp(c[i],0,0);   //显示第一张图片
    int len = sizeof(c)/sizeof(c[0]);
    while(1)
    {
        int r;
        int str[10]= {0,1,2,3,4,5,6,7,8,9};
        r=rand()%10;
        i=str[r];
        int distance=Get_Move_Direction();

        if(distance==1) //up
        {
            i=(i+len)%(len+1);     //一共9张图片,从0开始,i=1,i=(i+8)%9 i=0,显示上一张图片,下面同理
        }
        else if(distance==2)  //down
        {
            i=(i+1)%(len+1);
        }
        else if(distance==3)  //left
        {
            i=(i+len)%(len+1);
        }
        else
        {
            i=(i+len)%(len+1);
        }
        show_bmp(c[i],0,0);   //映射新的图片
    }
    lcdclose();
    return 0;
}
int lcdinit()    //打开帧缓冲
{
    lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd == -1)
    {
        printf("Open lcd failed!!\n");
        return -1;
    }
}
int lcdclose()
{
    close(lcd_fd);
}
//显示图片函数
void show_bmp(const char * pathname, int x, int y )
{
    int fd = open(pathname,O_RDWR);
    int fd1 = open("/dev/fb0",O_RDWR);    //打开帧缓冲

    p =  mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,fd1, 0);

    int width,height;
    short depth;
    unsigned char buf[4]= {0};
    //读取宽度
    lseek(fd,0x12,SEEK_SET);
    read(fd,buf,4);
    width = buf[3] << 24 | buf[2] << 16 | buf[1] <<8 | buf[0];

    //读取高度
    lseek(fd,0x16,SEEK_SET);
    read(fd,buf,4);
    height = buf[3] << 24 | buf[2] << 16 | buf[1] <<8 | buf[0];

    //读取色深
    lseek(fd,0x1c,SEEK_SET);
    read(fd,buf,2);
    depth = buf[1]<< 8 | buf[0];

    //读取像素数组 显示图片
    int line_valid_bytes = abs(width) * depth / 8 ;
    int line_total_bytes ;//定义 一行的总字节数 = 一行有效字节数+ 赖子
    //如果有赖子 就求赖子数
    int laizi=0;
    if(line_valid_bytes %4 != 0)
    {
        laizi = 4 - line_valid_bytes % 4;
    }
    line_total_bytes = line_valid_bytes +  laizi ;

    //整个像素数组的大小
    int total_bytes = line_total_bytes * abs(height);

    //申请一段空间 来保存 从图片中读取到的像素数组
    unsigned char *c   = malloc(total_bytes); //返回你申请的这段空间的首地址
    //读取像素数组
    lseek(fd,54,SEEK_SET);
    read(fd,c,total_bytes);

    //把保存的像素数组的颜色分量 写进帧缓冲  调用画点函数
    unsigned char a,r,g,b ;
    int color;
    int j=0;
    int x0,y0;
    for(y0=0; y0< abs(height) ; y0++)
    {
        for(x0=0; x0<abs(width); x0++)
        {
            b = c[j++];
            g = c[j++];
            r = c[j++];
            if(depth == 32)
            {
                a = c[j++];
            }
            else //24位只有RGB
            {
                a = 0;
            }
            color = a<<24 | r<<16 | g<<8 | b ;
            draw_point( width>0? x+x0 : abs(width)+x-1-x0,
                        height>0? y+height-1-y0 : y+y0,
                        color
                      );
        }
        //跳过赖子数
        j = j+laizi;
    }


    close(fd);
    munmap(p,800*480*4);


}
int Get_Move_Direction(void)
{

    struct input_event event0;
    int res = 0;
    int x_start = -1;
    int y_start = -1;
    int x_end = -1;
    int y_end = -1;
    int fd = open("/dev/input/event0",O_RDWR);
    if(fd == -1)
    {
        printf("Open Error!\n");
        return -1;
    }
    //不停的从触摸屏读取输入事件的属性
    while(1)
    {
        res = read(fd,&event0,sizeof(event0));
        if(res == -1)
        {
            perror("read input error\n");
            return -2;
        }
        //读取按键的坐标  一个点的X轴的坐标 eveny0.value就是坐标值
        if(event0.type == EV_ABS)
        {
            //当手指离开屏幕
            if(event0.code == ABS_PRESSURE && event0.value == 0)
            {
                break;
            }
            //读取按键的坐标  一个点的X轴的坐标 ev.value就是坐标值
            if(event0.code == ABS_X&&event0.code==ABS_X)
            {
                if(x_start == -1)
                {
                    x_start = event0.value;
                }
                x_end = event0.value;
            }
            //y轴坐标值同理
            if(event0.code == ABS_Y&&event0.code==ABS_Y)
            {
                if(y_start == -1)
                {
                    y_start = event0.value;
                }
                y_end = event0.value;
            }
        }
    }
    //根据方向轴上的变化量的大小 以及数值的比较 就可以确定发生了哪个方向的滑动
    if(abs(x_end - x_start) > abs(y_end - y_start))
    {
        if(x_end - x_start > 0)
        {
            return RIGHT;
        }
        else
            return LEFT;
    }
    if(abs(x_end - x_start) < abs(y_end - y_start))
    {
        if(y_end - y_start > 0)
        {
            return DOWN;
        }
        else
            return UP;
    }
}

  • 9
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值