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

发布时间:2024年01月15日

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

文章来源:https://blog.csdn.net/weixin_41114301/article/details/135610746
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。