Linux C语言解析.bmp格式图片并显示汉字

时间:2023-02-05 09:48:45

bmp.h 文件

 #ifndef __BMP_H__
#define __BMP_H__ #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> //文件头
typedef struct
{
unsigned char bfType[]; //文件类型
unsigned long bfSize; //位图大小
unsigned short bfReserved1; //位0
unsigned short bfReserved2; //位0
unsigned long bfOffBits; //到数据偏移量
} __attribute__((packed)) BitMapFileHeader; //信息头
typedef struct
{
unsigned long biSize; // BitMapFileHeader 字节数
long biWidth; //位图宽度
long biHeight; //位图高度,正位正向,反之为倒图
unsigned short biPlanes; //为目标设备说明位面数,其值将总是被设为1
unsigned short biBitCount; //说明比特数/象素,为1、4、8、16、24、或32。
unsigned long biCompression; //图象数据压缩的类型没有压缩的类型:BI_RGB
unsigned long biSizeImage; //说明图象的大小,以字节为单位
long biXPelsPerMeter; //说明水平分辨率
long biYPelsPerMeter; //说明垂直分辨率
unsigned long biClrUsed; //说明位图实际使用的彩色表中的颜色索引数
unsigned long biClrImportant; //对图象显示有重要影响的索引数,0都重要。
} __attribute__((packed)) BitMapInfoHeader; //位图颜色分量
typedef struct
{
unsigned char Blue; //该颜色的蓝色分量
unsigned char Green; //该颜色的绿色分量
unsigned char Red; //该颜色的红色分量
unsigned char Reserved; //保留值
} __attribute__((packed)) RgbQuad; typedef struct DealInfo
{
unsigned char tmp;
unsigned char hum;
unsigned char light;
unsigned char fan;
unsigned char buzz;
}DealInfo; typedef struct QNode
{
DealInfo buf;
struct QNode *pNext;
}QNode; void plan();
int show_photo(const char *fbp, struct fb_var_screeninfo *scrinfo); //显示图片 #endif //__BMP_H__

bmp.c 文件

 #include "bmp.h"
#include "word.h" extern char photo_name[];
extern QNode heard; //汉字字模
unsigned char word[WORD_DATA_HEIGTH][WORD_DATA_WIDTH] = { 0x08,0x40,0x08,0x40,0x0B,0xFC,0x10,0x40,0x10,0x40,0x30,0x40,0x3F,0xFE,0x50,0x00,
0x90,0x40,0x10,0x40,0x13,0xFC,0x10,0x40,0x10,0x40,0x10,0x40,0x1F,0xFE,0x10,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x80,
0x04,0x40,0x04,0x40,0x08,0x20,0x08,0x20,0x10,0x10,0x20,0x08,0x40,0x04,0x80,0x02, 0x08,0x20,0x04,0x40,0x7F,0xFC,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,
0xFF,0xFE,0x01,0x00,0x01,0x00,0x7F,0xFC,0x02,0x80,0x04,0x40,0x18,0x30,0xE0,0x0E, 0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x7C,0xFE,0x44,0x22,0x44,0x22,0x44,0x22,0x44,
0x42,0x44,0x24,0x44,0x14,0x44,0x08,0x44,0x14,0x44,0x22,0x7C,0x42,0x44,0x80,0x00, 0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x4F,0xE4,0x49,0x24,0x49,0x24,0x4F,0xE4,
0x49,0x24,0x49,0x24,0x49,0x24,0x4F,0xE4,0x40,0x04,0x40,0x04,0x7F,0xFC,0x00,0x04, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,
0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x00,0x08,0x00,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x08,0x01,0x08,0x7D,0x90,0x05,0xA0,0x09,0x40,
0x09,0x40,0x11,0x20,0x11,0x10,0x21,0x08,0x41,0x06,0x81,0x00,0x05,0x00,0x02,0x00, 0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x4F,0xE4,0x49,0x24,0x49,0x24,0x4F,0xE4,
0x49,0x24,0x49,0x24,0x49,0x24,0x4F,0xE4,0x40,0x04,0x40,0x04,0x7F,0xFC,0x00,0x04, 0x10,0x40,0x10,0x40,0x10,0x40,0x10,0x40,0x3E,0x40,0x22,0x60,0x42,0x50,0x42,0x48,
0xA4,0x44,0x14,0x44,0x08,0x40,0x08,0x40,0x10,0x40,0x20,0x40,0x40,0x40,0x80,0x40, 0x00,0x00,0x20,0x80,0x13,0x3C,0x12,0x24,0x02,0x24,0x02,0x24,0xF2,0x24,0x12,0x24,
0x12,0x24,0x12,0xB4,0x13,0x28,0x12,0x20,0x10,0x20,0x28,0x20,0x47,0xFE,0x00,0x00 }; void plan(void)
{
//映射内存地址,屏幕信息
char *fbp = NULL;
struct fb_var_screeninfo scrinfo; //设备文件的操作
int devfb = open("/dev/fb0", O_RDWR);
if(!devfb)
{
printf("devfb open error!\r\n");
exit();
} //屏幕信息
if(ioctl(devfb, FBIOGET_VSCREENINFO, &scrinfo))
{
printf("get screen infomation error!\r\n");
exit();
}
//设置参数(图片大小为1280*168)
scrinfo.xres = ;
scrinfo.yres = ; if(ioctl(devfb, FBIOPUT_VSCREENINFO, &scrinfo))
{
printf("set screen infomation error!\r\n");
exit();
} //内存映射
unsigned long screensize = scrinfo.xres_virtual * scrinfo.yres_virtual * scrinfo.bits_per_pixel / ;
fbp = (char *)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, devfb, );
if(- == (int)fbp)
{
printf("mmap error!\r\n");
exit();
} //字形处理和视频显示
//update_word(&heard);
show_photo(fbp, &scrinfo); //结束
munmap(fbp, screensize);
close(devfb); return ;
} int show_photo(const char *fbp, struct fb_var_screeninfo *scrinfo)
{
if(NULL == fbp || NULL == scrinfo)
return -; int xres = scrinfo->xres_virtual;
int bits_per_pixel = scrinfo->bits_per_pixel;
BitMapFileHeader FileHead;
BitMapInfoHeader InfoHead;
RgbQuad rgb;
unsigned long location = ; //打开.bmp文件
FILE *fb = fopen(photo_name, "rb");
if (fb == NULL)
{
printf("fopen bmp error\r\n");
return -;
} //读文件头,并判断是否为.bmp文件
if ( != fread( &FileHead, sizeof(BitMapFileHeader),, fb))
{
printf("read BitMapFileHeader error!\n");
fclose(fb);
return -;
}
if (memcmp(FileHead.bfType, "BM", ) != )
{
printf("it's not a BMP file\n");
fclose(fb);
return -;
} //获取.bmp文件信息
if ( != fread( (char *)&InfoHead, sizeof(BitMapInfoHeader),, fb))
{
printf("read BitMapInfoHeader error!\n");
fclose(fb);
return -;
} //移动至数据段,准备读数据
fseek(fb, FileHead.bfOffBits, SEEK_SET); //图片行,列,像素信息
int line_x = , line_y = ;
unsigned long tmp = ; int len = InfoHead.biBitCount / ; //原图像素点所占字节数,24/8=3
int bits_len = bits_per_pixel / ; //屏幕像素点所占字节数,32/8=4 //右上角文字显示区相关信息
int word_line_y = WORD_DATA_HEIGTH - ; //三行文字最底端的行
int word_line_x; //三行文字最左端的列
int k; //字模移动位数
unsigned char data = , one = ; while(!feof(fb))
{
tmp = ;
rgb.Reserved = 0xFF; //对应的内存相对映射首地址的偏移量
location = line_x * bits_len + (InfoHead.biHeight - - line_y ) * xres * bits_len; tmp = rgb.Reserved << | rgb.Red << | rgb.Green << | rgb.Blue; //文字显示区
if(line_x > WORD_WIDTH && line_x < InfoHead.biWidth - &&
line_y > WORD_HEIGTH && line_y < InfoHead.biHeight - && word_line_y >= )
{
tmp = ; for(word_line_x = ; word_line_x < WORD_DATA_WIDTH; word_line_x++)
{ //读字模
data = word[word_line_y][word_line_x]; one = 0x80; for(k = ; k < BIT_WIDTH; k++)
{ fread((char *)&rgb, , len, fb); //确定有效位
if(data & one)
tmp = ; //黑色字体 else
tmp = rgb.Reserved << | rgb.Red << | rgb.Green << | rgb.Blue; location = line_x * bits_len + (InfoHead.biHeight - - line_y ) * xres * bits_len;
*((unsigned long *)(fbp + location)) = tmp; one >>= ; //自增列
line_x++;
if (line_x == InfoHead.biWidth )
{
line_x = ; //自增行
line_y++;
if(line_y == InfoHead.biHeight)
break;
}
}
} //字模从下往上读和显示
word_line_y--;
continue; } //读.bmp文件
if (len != fread((char *)&rgb, , len, fb))
break; //显示非文字区的图片
*((unsigned long *)(fbp + location)) = tmp; //自增列
line_x++;
if (line_x == InfoHead.biWidth )
{
line_x = ; //自增行
line_y++;
if(line_y == InfoHead.biHeight)
break;
}
} fclose(fb); return ;
}

main函数

 int main()
{ plan(); return ;
}

效果图(我不是给这游戏打广告)

说明:1.图片是24位或32位bmp图

     2.屏幕是32位屏幕

     3.不同的设备,可能设备文件不同

     4.需要在root用户下执行

  Linux C语言解析.bmp格式图片并显示汉字

Linux C语言解析.bmp格式图片并显示汉字的更多相关文章

  1. C语言解析Ini格式文件

    引用别人的博文: http://www.open-open.com/lib/view/open1402278076447.html 可以解析 INI 格式的字符串.解析文件.保存到文件. 下面是头文件 ...

  2. Bmp格式图片与16进制的互相转换简解 Python

    BMP TO HEX 首先介绍Github上一个简单的Bmp转成16进制的py: https://github.com/robertgallup/bmp2hex 网上这种例子很多.思路也简单:将bmp ...

  3. Linux C语言解析并显示&period;bmp格式图片

    /************************* *bmp.h文件 *************************/ #ifndef __BMP_H__ #define __BMP_H__ # ...

  4. bmp格式图片文件读取

    C++读取bmp图片 #include <windows.h> #include <stdio.h> #include <stdlib.h> #include &l ...

  5. 微信小程序 base64格式图片的显示及保存

    当我们拿到如下base64格式的图片(如下图)时, base64格式的图片数据: 如何显示 ? 使用image标签,src属性添加data:image/png;base64, (注意:若imgData ...

  6. 006--VS2013 C&plus;&plus; 加载其他格式图片&comma;并显示半透明化

    //--------------------------------------------MyPaint() 函数------------------------------------------ ...

  7. PNG&comma;JPEG&comma;BMP&comma;JIF图片格式详解及其对比

    原文地址:http://blog.csdn.net/u012611878/article/details/52215985 图片格式详解 不知道大家有没有注意过网页里,手机里,平板里的图片,事实上,图 ...

  8. &lbrack;自制操作系统&rsqb; BMP格式文件读取&amp&semi;图形界面系统框架&sol;应用接口设计

    本文将介绍在本人JOS中实现的简单图形界面应用程序接口,应用程序启动器,以及一些利用了图形界面的示例应用程序. 本文主要涉及以下部分: 内核/用户RW/RW调色板framebuffer共享区域 8bi ...

  9. 图片bmp格式转换为jpg格式

    一下代码经过个人测试,可用 注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的 import java.awt.Image; impo ...

随机推荐

  1. 业务安全通用解决方案——WAF数据风控

    业务安全通用解决方案——WAF数据风控 作者:南浔@阿里云安全 “你们安全不要阻碍业务发展”.“这个安全策略降低用户体验,影响转化率”——这是甲方企业安全部门经常听到合作团队抱怨.但安全从业者加入公司 ...

  2. c&num; 图片路径转byte&lbrack;&rsqb; 插到数据库BLOB 图片长宽自定义

    //根据图片路径读取图片并且转byte[]   类型 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] byData = ...

  3. hlsl 和cg 涉及 mul 左乘 右乘

    error: 1. mul' implicit truncation of vector type 2. matrixXXX: array dimensions of(unknown scope en ...

  4. GDB 的MI接口

    背景介绍: libgdb过时了,目前的GDB调试前端都不用libgdb 目前有两种比较流行:- MI接口,现在应该是MI II接口,是Eclipse CDT所采用的方式- emac输出接口,这个似乎有 ...

  5. IIS出现Server Application Unavailable的解决办法

    在XP中IIS5.1服务器,运行asp.net出现Server Application Unavailable,经过多次查证,才发现是因为先安装了.net Framework 2.0然后安装IIS,导 ...

  6. MySQL的一些基本查询,创建存储过程等

    常用的查询条件有1.比较:=,<,>,<=,>=,!=,<>,!>,!<              2.确定范围:between and,not bet ...

  7. mvc约定

    mvc是围绕一些基于约定的默认项,这写默认项在必要时可以覆盖,这个概念通常成为"约定优于配置". 比如没必要在web.config中设置views,models等文件夹的名称,已经 ...

  8. netty解码器详解(小白也能看懂!)

    什么是编解码器? 首先,我们回顾一下netty的组件设计:Netty的主要组件有Channel.EventLoop.ChannelFuture.ChannelHandler.ChannelPipe等. ...

  9. plsql 工具怎样导出 oracle 表结构

    一.双击 plsql 工具,输入登陆用户.登陆密码以及登陆数据库名称,如下图: 二.菜单 Tools --> Export User Objects...,如下图: 三.进入导出界面后,可以选择 ...

  10. ElasticSearch 2 &lpar;8&rpar; - 概览与简介

    ElasticSearch 2 (8) - 概览与简介 摘要 分布式集群架构,具有高扩充性,可随时增加或移除节点,并保证数据正确. 使用Apache Lucene储存JSON文件,提供全文搜索功能 所 ...