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

news2024/9/28 23:23:35

//第一:利用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1386818.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

欧姆龙plc学习NJ系列CJ系列资料Sysmac Studio编程软件视频教程

Sysmac Studio是欧姆龙公司开发的一款集成开发环境&#xff08;IDE&#xff09;&#xff0c;用于编程和配置欧姆龙PLC&#xff08;可编程逻辑控制器&#xff09;和其他自动化设备。Sysmac Studio支持以下型号的欧姆龙PLC&#xff1a;1. NJ系列&#xff1a;NJ501、NJ301、NJ101、…

循环冗余校验(Cyclic Redundancy Check, CRC)计算

若信息码字为111000110&#xff0c;生成多项式G(x)x^5x^3x1&#xff0c;则计算出的CRC校验码为&#xff08; &#xff09;。 A.01101 B.11001 C.001101 D.011001 循环冗余校验(Cyclic Redundancy Check, CRC)是一种根据网络数据包或电脑文件等数据产生简短固定位数校验码的…

【数据库和表的管理】

数据库和表的管理 一、实验目的 了解MySQL数据库的逻辑结构和物理结构的特点。学会使用SQL语句创建、选择、删除数据库。学会使用SQL语句创建、修改、删除表。学会使用SQL语句对表进行插入、修改和删除数据操作。了解MySQL的常用数据类型。 二、实验内容SQL语句创建、选择、删…

多维时序 | Matlab实现GRO-CNN-LSTM-Attention淘金算法优化卷积神经网络-长短期记忆网络结合注意力机制多变量时间序列预测

多维时序 | Matlab实现GRO-CNN-LSTM-Attention淘金算法优化卷积神经网络-长短期记忆网络结合注意力机制多变量时间序列预测 目录 多维时序 | Matlab实现GRO-CNN-LSTM-Attention淘金算法优化卷积神经网络-长短期记忆网络结合注意力机制多变量时间序列预测效果一览基本介绍程序设…

vivado 使用Vitis HLS源、使用模型生成器源、使用系统生成器源

使用Vitis HLS源 AMD Vitis™ 高级综合&#xff08;HLS&#xff09;工具将C规范转换为寄存器传输级&#xff08;RTL&#xff09;实现&#xff0c;您可以将其合成到AMD设备中。你可以写CC、C或SystemC中的规范&#xff0c;AMD设备提供了大规模并行与传统处理器相比&#xff0c;…

Android的setContentView流程

一.Activity里面的mWindow是啥 在ActivityThread的performLaunchActivity方法里面&#xff1a; private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {ActivityInfo aInfo r.activityInfo;if (r.packageInfo null) {r.packageInfo getP…

11 # 类:抽象类与多态

抽象类 抽象类是不允许被实例化的&#xff0c;只能被继承 abstract class Animal {}let animal new Animal(); // 错误: 不能创建一个抽象类的实例 class Dog {constructor(name: string) {this.name name;}name: string;run() {} }let dog new Dog(Tom);抽象类的好处就…

【野火i.MX6NULL开发板】ARM-GCC 和开发板的 HelloWorld(ubuntu主机和野火开发板debian交叉编译)、开发板的/mnt里没文件

0、前言 参考资料&#xff1a; 《野火 Linux 基础与应用开发实战指南基于 i.MX6ULL 系列》PDF 第24章 参考视频&#xff1a; https://www.bilibili.com/video/BV1JK4y1t7io?p26&vd_sourcefb8dcae0aee3f1aab700c21099045395 注意&#xff0c;一定要记得把虚拟机的网络适配…

【扩散模型】10、ControlNet | 用图像控制图像的生成(ICCV2023)

论文&#xff1a;Adding Conditional Control to Text-to-Image Diffusion Models 代码&#xff1a;https://github.com/lllyasviel/ControlNet 出处&#xff1a;ICCV2023 Best Paper | 斯坦福 时间&#xff1a;2023.02 一、背景 文本到图像的生成尽管已经有很好的效果&…

拦截器的配置

1、什么是拦截器&#xff1f; Spring MVC中的拦截器&#xff08;Interceptor&#xff09;类似于Servlet中的过滤器&#xff08;Filter&#xff09;&#xff0c;它主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、判断用户是否登录等。 拦截器依赖于web框…

Git仓库管理笔记

问题&#xff1a; hint: the same ref. If you want to integrate the remote changes, use Done 解决&#xff1a; 解决方法&#xff1a; 1、先使用pull命令&#xff1a; git pull --rebase origin master 2、再使用push命令&#xff1a; git push -u origin master

关于Python —— Python教程

开始 Python 是一个易于学习、使用和高效阅读的编程语言。它具有简洁的英文语法&#xff0c;编写更少的代码&#xff0c;让程序员专注于业务逻辑而不是语言本身。 本教程将从深度、专注细节上去理解 Python 这门语言。初学者可以参考此教程理解相应的内容&#xff0c;本教程将…

服务异步通讯——RabbitMQ

文章目录 RabbitMQ异步通讯技术对比 安装RabbitMQ下载镜像 安装MQRabbitMQ消息模型入门案例publisher实现consumer实现总结 SpringAMQPBasic Queue 简单队列模型消息发送消息接收 WorkQueue消息发送消息接收测试总结 发布/订阅Fanout声明队列和交换机消息发送消息接收 Direct基…

数据结构学习 jz66 构建乘积数组

关键词&#xff1a;数学 双指针 方法一&#xff1a;这个题目我一开始做不知道不能用除法。我做的&#xff1a;[ 用时: 12 m 12 s ] 用了除法 分类讨论 方法二&#xff1a;后来看了提示&#xff0c;双指针&#xff0c;两边各开始乘。 方法三&#xff1a;然后又看了答案可以节…

特征工程-特征处理(二)

特征处理 二、时间特征处理 将原本的具体时间拆分为年月日等多个特征变量&#xff0c;同时可以引入在一天的某个时间段&#xff0c;或者是当天是否为节假日等其他条件&#xff0c;还可以进一步结合其他特征&#xff0c;进行前后一个时间段或是多个时间段时间的特征差值。 dt.…

常见的反爬虫风控 | 验证码风控

一.前言 在当今信息技术迅速发展的背景下&#xff0c;网站和在线服务面临着日益增长的自动化访问威胁&#xff0c;这些大多来自于各类爬虫程序。这种大量的自动化访问不仅对网站的正常运行构成压力&#xff0c;还可能导致敏感数据的泄露&#xff0c;甚至被用于不正当竞争和恶意…

【华为OD机试真题2023CD卷 JAVAJS】查找一个有向网络的头节点和尾节点

华为OD2023(C&D卷)机试题库全覆盖,刷题指南点这里 查找一个有向网络的头节点和尾节点 知识点图DFS搜索 时间限制:1s 空间限制:256MB 限定语言:不限 题目描述: 给定一个有向图,图中可能包含有环,图使用二维矩阵表示,每一行的第一列表示起始节点,第二列表示终止节…

ubuntu22: nvtop no gpu to monitor.

解决方法&#xff1a; 重新下载nvtop sudo apt update sudo apt -y install nvtop真是逆天 &#xff0c;ubuntu系统的nvidia driver突然坏了&#xff0c;然后我重装了nvidia driver, 之后用nvtop就出现这个问题了&#xff0c;但是逆天的是我竟然没有搜到一篇中文的帖子讲这个问…

自己动手写一个 Arthas 在线诊断工具系列说明

相关文章&#xff1a; 自己动手写一个分库分表中间件&#xff08;十&#xff09;线上优化之数据库连接超时优化自己动手写分布式任务调度框架自己动手写 Java 虚拟机&#xff08;二&#xff09;-查找 Class 文件自己动手调试 JDK&#xff08;CLion&#xff09;Java Agent 的简…

Linux网络文件共享服务之FTP协议

目录 一、存储类型 1、直连式存储&#xff08;DAS&#xff09; 2、存储区域网络&#xff08;SAN&#xff09; 3、网络附加存储&#xff08;NAS&#xff09; 二、 FTP文件传输协议 1、FTP协议的工作原理 1.1 FTP协议的工作流程 1.2 FTP协议的两种工作模式 1.2.1 主动模…