关于在lcd上显示一张jpg图片的程序~~编译通过了,但启动程序后没有任何显示~~求解!!

polar_1995 2018-01-29 07:53:14
头文件,myhead.h
========================
#ifndef __MYHEAD_H
#define __MYHEAD_H

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <strings.h>
#include <pthread.h>
#include <semaphore.h>

#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/mman.h> // for mmap
#include <linux/fb.h>

#include "jpeglib.h"

struct RGB
{
int width;
int height;
int bpp; // bits per pixel

char *rgb_data;
};

bool jpg2rgb(struct RGB *, char *, struct stat *);

#endif

==================================
jpg_demo.c
===================================


#include "myhead.h"



bool jpg2rgb(struct RGB *rgb, char *jpg_data,

struct stat *jpg_info)

{

// [固定步骤]声明JPG信息结构体,以及错误管理结构体

struct jpeg_decompress_struct cinfo;

struct jpeg_error_mgr jerr;



// [固定步骤]使用缺省的出错处理来初始化cinfo

cinfo.err = jpeg_std_error(&jerr);

jpeg_create_decompress(&cinfo);



// [固定步骤]配置该cinfo,使其从jpg_data中读取st_size个字节

jpeg_mem_src(&cinfo, jpg_data, jpg_info->st_size);



// [固定步骤]读取JPG数据的头,并判断其格式是否合法

if(jpeg_read_header(&cinfo, true) != 1)

{

perror("这不是JPG图片");

return false;

}



// [固定步骤]开始解码

jpeg_start_decompress(&cinfo);





rgb->width = cinfo.output_width;

rgb->height= cinfo.output_height;

rgb->bpp = cinfo.output_components*8;



// 根据图片的尺寸大小,分配一块相应的内存rgb_data

// 用来存放从jpg_data解码出来的RGB数据

unsigned long rgb_size = cinfo.output_width *

cinfo.output_height *

cinfo.output_components;

rgb->rgb_data = calloc(1, rgb_size);





int row_stride = cinfo.output_width * cinfo.output_components;

while(cinfo.output_scanline < cinfo.output_height)

{

unsigned char *tmp[1];

tmp[0] = rgb->rgb_data + (cinfo.output_scanline) * row_stride;

jpeg_read_scanlines(&cinfo, tmp, 1);

}





jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);



return true;

}
=======================================
main.c
========================================
#include "myhead.h"


void read_jpg_image(int fd, char *jpg_data, int size)
{
int n;
char *tmp = jpg_data;
while(size > 0)
{
// n <= size
n = read(fd, tmp, size);
if(n == -1)
{
perror("read failed");
exit(0);
}

if(n == 0)
break;

size -= n;
tmp += n;
}
}

int main(int argc, char **argv) // ./showjpg xx.jpg
{
// 读取指定的JPG图片数据
int fd = open(argv[1], O_RDONLY);

struct stat *jpg_info
= calloc(1, sizeof(struct stat));
stat(argv[1], jpg_info);

char *jpg_data = calloc(1, jpg_info->st_size);
read_jpg_image(fd, jpg_data, jpg_info->st_size);

// 调用JPG标准的解码过程
// 将JPG数据变成RGB数据
struct RGB rgb;
jpg2rgb(&rgb, jpg_data, jpg_info);

// 准备好LCD,并将rgb_data妥善放置到显存上
int lcd = open("/dev/fb0", O_RDWR);
if(lcd == -1)
{
perror("打开LCD出错");
exit(0);
}

char *fbmem = mmap(NULL,
800*480*4,
PROT_READ|PROT_WRITE,
MAP_SHARED,
lcd,
0);
if(fbmem == MAP_FAILED)
{
perror("映射内存失败");
exit(0);
}

int rgb_size = rgb.width * rgb.height * rgb.bpp/8;
memcpy(fbmem, rgb.rgb_data, rgb.width);

close(fd);


close(lcd);

return 0;
}

...全文
440 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
老马何以识途 2018-01-31
  • 打赏
  • 举报
回复
沒錯,先分模塊測試,一點點來。
FoolCarpe 2018-01-30
  • 打赏
  • 举报
回复
看你代码的逻辑,可以分开测试一下 先读取一个rgb数据的文件,写入lcd设备,是否能够正常显示 如果可以正常显示,读取jpg文件转成rgb数据,在写入lcd设备,是否能够正常显示
晨星 2018-01-30
  • 打赏
  • 举报
回复
程序这么长,总得自己慢慢调试啊。别人拿去一般也只能通过调试发现问题。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧