ARM板显示图形颜色和图片

显示颜色

VS code编译代码

找出需求的点,涂上想要的颜色,达到心中所想的彩图。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <linux/fb.h>
#include <sys/ioctl.h>

int main()
{
    //打开屏幕
    int fd = open("/dev/fb0",O_RDWR);
    if(fd<0)
    {
        perror("open fd error");
        exit(-1);
    }
    //获取屏幕参数并储存
    struct fb_var_screeninfo fb_var;
    struct fb_fix_screeninfo fb_fix;
    unsigned int size;

    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);
    size = fb_fix.line_length*fb_var.yres;          //像素大小
    int width = fb_var.xres;                        //宽
    int height = fb_var.yres;                       //高

    //内存映射
    int *screen = NULL;//(int32位,色深只有24,放得下)
    screen = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
    if(screen == NULL)
    {
        perror("mmap error");
        exit(-1);
    }
    //画图填色
    int x,y;

    for(x = 0; x < 1024 ; x++)//圆
    {
        for(y = 0; y < 600; y++)
        {
            if(((x-512)*(x-512)+(y-300)*(y-300)) <=40000 )//中心圆
                screen[y*1024+x] = 0x00FFFF; 
            else if(x<200 & y<200)//起点正方形
            {
                screen[y*1024+x] = 0x00FF00;
            }
            else//底色
            {
                screen[y*1024+x] = 0xFF0000;  
            }
        }
    }
    
    //取消映射
    munmap(screen,size);

    //关闭屏幕
    close(fd);
}

下图在乌邦图编辑(命令1经过修饰原为arm-linux-gnueabihf-gcc)

下图是修改文件权限

图片显示

BMP

24位色深

//ARGB 8888
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>

int main()//1024*600(色深24bmp)
{
    //1.打开图片
    int bmp=open("./2.bmp",O_RDWR);
    if(bmp==-1)
    {
        perror("打开图片失败");
        exit(-1);
    }
    //2.定义一个数组用以存储图片信息,lcd ARGB:1024*600*4   bmp  RGB:1024*600*3
    unsigned char bmpbuf[1024*600*3];  //原始RBG = 1024*600*3 

    //3.光标偏移,跳过文件头信息
    lseek(bmp,54,SEEK_SET);

    //4.读取图片上的像素点
    read(bmp,bmpbuf,1024*600*3);

    //5.打开显示屏
    int lcd = open("/dev/fb0",O_RDWR);
    if(lcd==-1)
    {
        perror("打开显示屏失败");
        exit(-1);
    }

    //6.将图片的RGB转换为适合lcd显示的ARGB

        //定义一个数组用以存放转换后的像素点数据ARGB
        unsigned int lcdbuf[1024*600];       //转换后的ARBG 1024*600*4
    int i;
   
    for(i=0;i<1024*600;i++)
    {
        //将每一个像素点进行转换
       //ARBG            B             G           R            A
       lcdbuf[i] = bmpbuf[3*i] | bmpbuf[3*i+1]<<8 | bmpbuf[3*i+2]<<16 | 0<<24 ;
    }

    //7.将得到的ARGB映射到lcd显存里
    int * p =mmap(NULL,1024*600*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);

    //8.写入数据
    int x , y ;
    for(y=0;y<600;y++)
    {
        for(x=0;x<1024;x++)
        {
            *(p+x+y*1024)=lcdbuf[(599-y)*1024+x]; //*(p+x+y*1024)=lcdbuf[y*1024+x];
        }
    }

    //9.解除映射
    munmap(p,1024*600*4);
    close(lcd);
    return 0;

}

32位色深

//图片大小不同修改全局1024*600和599(8.写入数据)(600-1)
//色深不同修改存储(2.bmpbuf)和(6.argb)
int main()//1024*600(色深32bmp)
{
    //1.打开图片
    int bmp=open("./0.bmp",O_RDWR);
    if(bmp==-1)
    {
        perror("打开图片失败");
        exit(-1);
    }
    //2.定义一个数组用以存储图片信息,
    unsigned char bmpbuf[1024*600*4]; //数组优先使用char型,方便后面像素点转化

    //3.光标偏移,跳过文件头信息
    lseek(bmp,54,SEEK_SET);

    //4.读取图片上的像素点
    read(bmp,bmpbuf,1024*600*4);

    //5.打开显示屏
    int lcd = open("/dev/fb0",O_RDWR);
    if(lcd==-1)
    {
        perror("打开显示屏失败");
        exit(-1);
    }

    //6.将图片的RGB转换为适合lcd显示的ARGB

        //定义一个数组用以存放转换后的像素点数据ARGB
        unsigned int lcdbuf[1024*600];       //转换后的ARBG 1024*600*4
    int i;
   
    for(i=0;i<1024*600;i++)
    {
        //将每一个像素点进行转换
       //ARBG            B               G                   R                  A
       lcdbuf[i] = bmpbuf[4*i] | bmpbuf[4*i+1]<<8 | bmpbuf[4*i+2]<<16 | bmpbuf[4*i+3]<<24 ;
    }

    //7.将得到的ARGB映射到lcd显存里
    int * p =mmap(NULL,1024*600*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);

    //8.写入数据
    int x , y ;
    for(y=0;y<600;y++)
    {
        for(x=0;x<1024;x++)
        {
            *(p+x+y*1024)=lcdbuf[(599-y)*1024+x]; //*(p+x+y*1024)=lcdbuf[y*1024+x];
        }
    }

    //9.解除映射
    munmap(p,1024*600*4);
    close(lcd);
    return 0;

}

如颜色显示一样编译、下载和赋予权限,再下载bmp图片(默认格式1024*600,32位教程有修改指导),执行可执行文件即可在ARM板上显示图片。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值