利用fd子系统实现图案与图片显示方法

简介: 利用fd子系统实现图案与图片显示方法

//第一:利用fb子系统画圆的方法与实现

//1、头文件信息
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define COLOR 0x0000ffff
//定义lcd屏幕固定参数的变量
static struct fb_fix_screeninfo fix_info;
//定义lcd屏幕可变参数的变量
static struct fb_var_screeninfo var_info;
int main(int argc, char *argv[])
{
    int fd,ret,x,y,m,n;
  int *ptr;
  int *temp;
  //1、打开对应的设备节点
    fd=open("/dev/fb0",O_RDWR);
  //2、利用ioclt函数传输对应的命令-固定参数
    ret = ioctl(fd,FBIOGET_FSCREENINFO, &fix_info);
  if(ret < 0)
  {
      printf("error\r\n");
    return -1;
  }
  //3、利用ioctl函数传递可变参数的命令
   ret = ioctl(fd,FBIOGET_VSCREENINFO, &var_info);
  if(ret < 0)
  {
      printf("error\r\n");
    return -1;
  }
  //4、输出获取到的参数信息--保存各自对应的结构体里面
  printf("fix_info.smem_len = %d, fix_info.line_length = %d, var_info.xres = %d, var_info.yres = %d, var_info.bits_per_pixel = %d\n", \
              fix_info.smem_len, fix_info.line_length, var_info.xres, var_info.yres, var_info.bits_per_pixel);
  ptr = (int *)mmap(NULL,fix_info.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    temp = ptr;
  for(x = 0;x < 800;x++)
  {
    for(y = 0;y < 1280;y++)
    {
      *temp = COLOR;
      temp++;
      //usleep(100000);
    }
  }
  temp = ptr;
  for(x = 0;x < 1280;x++)//400,640
  {
    for(y = 0;y < 800;y++)
    {
      m = abs(x-640)*abs(x-640);
      n = abs(y-400)*abs(y-400);
      if((m+n)<201*201 && (m+n >199*199))
      {
        *(temp+x*800+y) = 0x00ff0000;
      };
    }
  }
  return 0;
}

//第二个:图片循环显示的方法

#include<stdio.h>
#include<setjmp.h>
#include<jpeglib.h>
#include<stdio.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<linux/fb.h>
#include<string.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
  int fd_fb;
  int ret;
  int x,y;
  int* pfile;
  int color=0;
  struct fb_fix_screeninfo fix_info;
  struct fb_var_screeninfo var_info;
  fd_fb=open("/dev/fb0",O_RDWR);
  ioctl(fd_fb,FBIOGET_FSCREENINFO,&fix_info);
  ioctl(fd_fb,FBIOGET_VSCREENINFO,&var_info);
  pfile=(int*)mmap(NULL,fix_info.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd_fb,0);
  system("ls ./pic > temp.txt");
  FILE *temp_fb=fopen("temp.txt","rb");
  int num;
  char temp[100][30]={0};
  char *EO;
  num=0;
  do
  {
  char pwd[20]={0};
  EO=fgets(pwd,sizeof(pwd)-1,temp_fb);
    if(EO!=NULL)
    {
    strncat(temp[num],"./pic/",strlen("./pic/"));
    strncat(temp[num],pwd,strlen(pwd)-1);
    num++;
    }
  }while(EO!=NULL);
  fclose(temp_fb);
  
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  cinfo.err=jpeg_std_error(&jerr);//一旦出错,会将错误信息放到这里
  jpeg_create_decompress(&cinfo);//初始化核心结构体
  for(y=0;y<num;y++)
  {
    FILE *infile=NULL;
    printf("%d\t%s\r\n",y,temp[y]);
    infile=fopen((char *)temp[y],"rb");
    if(infile==NULL)
    {
      perror("error");
      break;
    }
    jpeg_stdio_src(&cinfo,infile);//绑定核心结构体和要展示的图片
    jpeg_read_header(&cinfo,TRUE);
    cinfo.scale_num=1;
    cinfo.scale_denom=1;
    cinfo.out_color_space=JCS_RGB;
    //5.解压缩图片
    jpeg_start_decompress(&cinfo);
    //6.读取数据并且上屏操作
    unsigned char*buffer;//这个变量是读取jpeg图片的数据
    buffer=(unsigned char*)malloc(cinfo.output_width*cinfo.output_components);//变量使用空间的大小--800*3行的大小2400
    while(cinfo.output_scanline<cinfo.output_height)
    {
      memset(buffer,0,sizeof(buffer));
      jpeg_read_scanlines(&cinfo,&buffer,1);
      for(x=0;x<cinfo.output_width;x++)
      {
          switch(cinfo.output_components)
          {
            case 1:
              color=buffer[0+x*1]<<16|buffer[1+x*1]<<8|buffer[2+x*1]<<0;
              break;
            case 3:
              color=buffer[0+x*3]<<16|buffer[1+x*3]<<8|buffer[2+x*3]<<0;
              break;
            case 4:
              color=buffer[0+x*4]<<24|buffer[1+x*4]<<16|buffer[2+x*4]<<8|buffer[3+x*4]<<0;
              break;
          } 
        *(pfile+x+800*(cinfo.output_scanline-1))=color; 
        color=0;
      } 
    }
  //7.完成解码
  jpeg_finish_decompress(&cinfo);   
  free(buffer);
  fclose(infile);//文件流指针,需要收回一下 
  sleep(5);
  } 
  //8.释放空间
  jpeg_destroy_decompress(&cinfo);  
  munmap(pfile,fix_info.smem_len);
  close(fd_fb);
  return 0;
}

第三:图片循环显示的方法(二)

#include <stdio.h>
#include <setjmp.h>
#include <jpeglib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <string.h>
#include <stdlib.h>
char *file;
int num = 1;
int main(int argc, char *argv[])
{
int fd;
int ret;
int x;
int *pfile;
int color = 0;
char file[20];
struct fb_fix_screeninfo fix_info;
struct fb_var_screeninfo var_info;
//打开lcd设备
fd = open("/dev/fb0", O_RDWR);
if(fd < 0)
{
perror("open");
return -1;
}
//获取lcd的基础信息
ret = ioctl(fd, FBIOGET_FSCREENINFO, &fix_info);
if(ret < 0)
{
perror("ioctl_fix");
return -1;
}
ret = ioctl(fd, FBIOGET_VSCREENINFO, &var_info);
if(ret < 0)
{
perror("ioctl_fix");
return -1;
}
printf("fix_info.smem_len = %d, fix_info.line_length = %d, var_info.xres = %d, var_info.yres = %d, var_info.bits_per_pixel = %d\n", \
fix_info.smem_len, fix_info.line_length, var_info.xres, var_info.yres, var_info.bits_per_pixel);
pp:
//映射显存空间
pfile = (int *)mmap(NULL, fix_info.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(pfile == MAP_FAILED)
{
perror("mmap");
return -1;
}
     //jpeg源码处理jpeg图片步骤
     //1.定义核心结构体并初始化
struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr); //一旦出错,会将错误信息放到这里
jpeg_create_decompress(&cinfo);//初始化核心结构体
FILE* infile;
sprintf(file,"%d.jpg",num);
//2.绑定图片资源
    if ((infile = fopen(file, "rb")) == NULL) {   // file指向自己的jpg文件的绝对路径
        fprintf(stderr, "open  failed\n");
        return -1;
    }
jpeg_stdio_src(&cinfo, infile);//绑定核心结构体和要展示的图片
//3.读取图片的基础信息
jpeg_read_header(&cinfo, TRUE);
//注意::cinfo.image_width和cinfo.output_width是两个数据
//注意::cinfo.image_width和cinfo.output_width是两个数据
//注意::cinfo.image_width和cinfo.output_width是两个数据
printf("cinfo.image_width = %d, cinfo.image_height = %d, cinfo.num_components = %d\n", \
cinfo.image_width, cinfo.image_height, cinfo.num_components);
//4.设置解压缩的参数---注意:如果使用了参数,则图片信息会发生变化,要使用cinfo.output*这类的数据
    //开始的这两参数是决定图片的大小可以等比例缩放  ----JCS_GRAYSCALE--灰度显示命令
cinfo.scale_num=1;
cinfo.scale_denom=1;
//cinfo.out_color_space=JCS_GRAYSCALE;
cinfo.out_color_space=JCS_RGB;
    //5.解压缩图片
jpeg_start_decompress(&cinfo);
printf("cinfo.output_width = %d, cinfo.output_height = %d, cinfo.output_components = %d,cinfo.output_scanline=%d\n", \
cinfo.output_width, cinfo.output_height, cinfo.output_components,cinfo.output_scanline);
    //6.读取数据并且上屏操作
    //6.读取解压缩后的数据并上屏
//官方的操作
// JSAMPARRAY buffer;
// unsigned int row_stride = cinfo.output_width * cinfo.output_components;
// buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
//常规操作
unsigned char *buffer;  //这个变量是读取jpeg图片的数据
buffer = (unsigned char *)malloc(cinf o.output_width*cinfo.output_components);  //变量使用空间的大小--800*3 行的大小2400
if(cinfo.output_components == 4) //如果图片为4通道时--跟随颜色分量的 RGB8888
{
while(cinfo.output_scanline < cinfo.output_height)//cinfo.output_scanline会随着jpeg_read_scanlines的触发而增1
{
memset(buffer, 0, sizeof(buffer));
jpeg_read_scanlines(&cinfo, &buffer, 1);//如果是官方操作,buffer是一个二维指针变量,这里不用取址符
for(x = 0; x < cinfo.output_width; x++)
{
    //进行数据整合
color = buffer[0+x*4] << 24 | buffer[1+x*4] << 16 | buffer[2+x*4] << 8 | buffer[3+x*4] << 0;
*(pfile + x + 800*(cinfo.output_scanline-1)) = color;
color = 0;
}
}
}else if(cinfo.output_components == 3) { //如果图片为3通道时--屏幕
while(cinfo.output_scanline < cinfo.output_height)//cinfo.output_scanline会随着jpeg_read_scanlines的触发而增1
{
memset(buffer, 0, sizeof(buffer));
jpeg_read_scanlines(&cinfo, &buffer, 1);//如果是官方操作,buffer是一个二维指针变量,这里不用取址符
for(x = 0; x < cinfo.output_width; x++)
{
    
color = buffer[0+x*3] << 16 | buffer[1+x*3] << 8 | buffer[2+x*3] << 0;  //RGB888
*(pfile + x + 800*(cinfo.output_scanline-1)) = color;
                //清楚一下,color里面原来的图像数据
color = 0;
}
//printf("cinfo.output_scanline = %d\n", cinfo.output_scanline);
}
}else if(cinfo.output_components == 1) { //如果图片为1通道,也就是选择灰白色时
while(cinfo.output_scanline < cinfo.output_height)//cinfo.output_scanline会随着jpeg_read_scanlines的触发而增1
{
memset(buffer, 0, sizeof(buffer));
jpeg_read_scanlines(&cinfo, &buffer, 1);//如果是官方操作,buffer是一个二维指针变量,这里不用取址符
for(x = 0; x < cinfo.output_width; x++)
{
    //灰度图像 buffer[0]---高位   数据整合 buffer[0]  buffer[1]buffer[2]   ----buffer[1]buffer[2]buffer[3]
color = buffer[0+x*1] << 16 | buffer[1+x*1] << 8 | buffer[2+x*1] << 0;
*(pfile + x + 800*(cinfo.output_scanline-1)) = color;
color = 0;
}
//printf("cinfo.output_scanline = %d\n", cinfo.output_scanline);
}
}
sleep(3);
num++;
if(num == 3)
{
num = 1;
}
//7.完成解码
jpeg_finish_decompress(&cinfo); 
//8.释放空间
free(buffer);
fclose(infile); //文件流指针,需要收回一下
jpeg_destroy_decompress(&cinfo);
munmap(pfile, fix_info.smem_len);
goto pp;
close(fd);
return 0;
}
//aarch64-linux-gnu-gcc a.c -o main -I/home/fang/work/armlib/include -L/home/fang/work/armlib/lib -ljpeg

目录
相关文章
|
存储 安全 数据管理
python如何批量创建文件与文件夹
python如何批量创建文件与文件夹
893 0
|
6月前
|
人工智能 弹性计算 测试技术
2026年阿里云618大促活动云服务器优惠价格表
2026阿里云618大促以“AI加速季,智惠生产力”为主题,推出轻量服务器(低至38元/年)、ECS云服务器(99元起/年)及GPU实例(T4 4折+补贴),覆盖个人、企业与AI开发全场景,限时特惠,按需选购。
893 0
|
JSON 文字识别 BI
如何开发车辆管理系统中的加油管理板块(附架构图+流程图+代码参考)
本文针对中小企业在车辆加油管理中常见的单据混乱、油卡管理困难、对账困难等问题,提出了一套完整的系统化解决方案。内容涵盖车辆管理系统(VMS)的核心功能、加油管理模块的设计要点、数据库模型、系统架构、关键业务流程、API设计与实现示例、前端展示参考(React + Antd)、开发技巧与工程化建议等。通过构建加油管理系统,企业可实现燃油费用的透明化、自动化对账、异常检测与数据分析,从而降低运营成本、提升管理效率。适合希望通过技术手段优化车辆管理的企业技术人员与管理者参考。
|
7月前
|
人工智能 运维 Serverless
【保姆级教程】零成本冷启动:基于函数计算一键部署 Flux 文生图与 GPT-Sovits 语音合成实战
本文介绍低成本玩转AI的Serverless方案:无需4090显卡,借助阿里云函数计算(FC)+ Serverless GPU,零代码一键部署Flux毛绒风文生图与GPT-Sovits语音克隆。免运维、按量付费(单图0.02元起,语音0.017元起),弹性扩缩、冷启快、成本极低,学生党/开发者轻松上车。
|
8月前
|
人工智能 数据挖掘 数据安全/隐私保护
AI拆解小红书和公众号笔记,4个步骤让你看懂爆款底层密码(附AI工作流实操步骤和AI提示词)
用AI深度拆解155篇万赞笔记,提炼出爆款共性:选题抓痛点、开头造冲突、结构强逻辑、表达重共鸣。本文分享实操四步法、40+可复用模板及原创提示词,助你告别凭感觉创作,科学提升内容效率
1818 4
|
消息中间件 存储 Java
手动实现 Spring Boot 日志链路追踪:提升调试效率的利器
【8月更文挑战第8天】在复杂的分布式系统中,日志是诊断问题、追踪系统行为的重要工具。然而,随着微服务架构的普及,服务间的调用链路错综复杂,传统的日志记录方式往往难以快速定位问题源头。今天,我们将探讨如何在不依赖外部组件(如Zipkin、Sleuth等)的情况下,手动实现Spring Boot应用的日志链路追踪,让日志定位更加便捷高效。
1104 1
|
运维 监控 数据可视化
小白也能部署应用,3个免费的容器化部署工具测评
本文对比了三款容器化部署工具:Docker Compose、Portainer 和 Websoft9。Docker Compose 适合开发者编排多容器应用,Portainer 提供图形化管理界面,而 Websoft9 则面向中小企业和非技术人员,提供一键部署与全流程运维支持,真正实现“开箱即用”。三款工具各有定位,Websoft9 更贴近大众用户需求。
小白也能部署应用,3个免费的容器化部署工具测评
|
9月前
|
缓存 人工智能 安全
阿里云服务器8核16G、8核32G、8核64G最低价格:计算型c9i、通用型g9i、内存型r9i实例活动价格参考
阿里云第九代云服务器8核16G、8核32G、8核64G价格,包括计算型c9i实例8核16G、通用型g9i实例8核32G、内存型r9i实例8核64G,分别适合高性能计算、企业级应用和大数据分析等场景。活动价格方面,c9i实例年付5958.52元起,g9i实例7551.94元起,r9i实例9937.12元起。用户可用7.5折优惠券,学生、企业用户还有额外优惠,实际价格更低。阿里云提供全场景高性能云服务器解决方案,满足不同业务需求。
900 2
|
9月前
|
人工智能 安全 关系型数据库
阿里云企业上云第一站:提供迁云及出海补贴,域名与云服务器等云产品优惠
阿里云企业上云第一站平台提供企业迁云和出海补贴,以及域名注册和云服务器等云产品。完成企业认证可享5亿算力补贴和最高10万元出海补贴。平台提供多样化域名及云产品服务,包括经济型云服务器和AI解决方案,支持企业建站、业务上云、AI创新及协同办公。
|
11月前
|
人工智能 自然语言处理 算法
炎鹊行业AI,技术解读
炎鹊AI推出Nexus Agent v1.0,构建“通用大模型+垂直增强”四层架构,融合AIGA决策大脑、行业知识图谱与专属业务模型,实现AI从“能对话”到“能做事”的跨越。通过低代码平台,企业可快速打造高精准、可迭代的专属AI应用,大幅降本提效规模化落地。(239字)
炎鹊行业AI,技术解读