利用c语言GEC210开发板Linux系统的/picture目录下,有若干文件 利用Linux系统目录操作相关知识,编程获取/picture目录下所有后缀为bmp、jpg、jpeg的图片文件名 这些图片的宽、高事先未知,需要编程获取 每隔500毫秒,清白屏,显示下一幅图片,无论图片是否超出LCD屏幕大小,都从屏幕左上角(0,0)处显示图片全部或局部 能够循环显示这些图片
时间: 2023-07-16 16:14:00 浏览: 133
以下是一个参考代码,可以实现上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define FB_DEVICE "/dev/fb0"
#define FB_SIZE (800*480*4)
typedef struct {
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int offset;
unsigned int header_size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bit_count;
unsigned int compression;
unsigned int image_size;
unsigned int x_px_per_meter;
unsigned int y_px_per_meter;
unsigned int colors_used;
unsigned int colors_important;
} __attribute__((packed)) BMP_HEADER;
int main(int argc, char *argv[]) {
// 打开帧缓存设备
int fb_fd = open(FB_DEVICE, O_RDWR);
if (fb_fd < 0) {
perror("open framebuffer device");
exit(EXIT_FAILURE);
}
// 映射帧缓存内存
void *fb = mmap(NULL, FB_SIZE, PROT_WRITE, MAP_SHARED, fb_fd, 0);
if (fb == MAP_FAILED) {
perror("mmap framebuffer");
close(fb_fd);
exit(EXIT_FAILURE);
}
// 获取/picture目录下的所有bmp、jpg、jpeg文件
DIR *dir = opendir("/picture");
if (dir == NULL) {
perror("open directory");
munmap(fb, FB_SIZE);
close(fb_fd);
exit(EXIT_FAILURE);
}
struct dirent *entry;
char *filename;
while ((entry = readdir(dir)) != NULL) {
filename = entry->d_name;
int len = strlen(filename);
if (len > 4 && strcmp(filename+len-4, ".bmp") == 0
|| len > 4 && strcmp(filename+len-4, ".jpg") == 0
|| len > 5 && strcmp(filename+len-5, ".jpeg") == 0) {
// 打开图片文件
char filepath[128];
sprintf(filepath, "/picture/%s", filename);
int fd = open(filepath, O_RDONLY);
if (fd < 0) {
perror("open image file");
continue;
}
// 读取BMP文件头信息,获取图片宽、高
BMP_HEADER header;
read(fd, &header, sizeof(header));
int width = header.width;
int height = header.height;
// 映射图片内存
void *image = mmap(NULL, width*height*3, PROT_READ, MAP_SHARED, fd, 0);
if (image == MAP_FAILED) {
perror("mmap image");
close(fd);
continue;
}
// 清屏
memset(fb, 0, FB_SIZE);
// 显示图片
int x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
unsigned char *pixel = (unsigned char *)image + (y*width + x)*3;
// 将BGR颜色值转换为ARGB格式
unsigned int color = (pixel[0]<<16) | (pixel[1]<<8) | pixel[2];
color |= 0xff000000;
// 在屏幕上显示像素点
*(unsigned int *)(fb + ((y+90)*800 + x+140)*4) = color;
}
}
// 解除图片内存映射
munmap(image, width*height*3);
close(fd);
// 等待500毫秒
usleep(500000);
}
}
// 关闭目录和帧缓存设备,解除内存映射
closedir(dir);
munmap(fb, FB_SIZE);
close(fb_fd);
return 0;
}
```
注意,这是一个简单的示例代码,可能需要根据具体的需求进行修改。另外,该代码假设LCD屏幕分辨率为800x480,图片文件的宽度和高度都不超过这个范围。如果实际情况不同,需要根据需要进行修改。
阅读全文