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格式图片并显示汉字的更多相关文章

  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语言解析并显示.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++ 加载其他格式图片,并显示半透明化

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

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

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

  8. [自制操作系统] BMP格式文件读取&图形界面系统框架/应用接口设计

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

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

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

随机推荐

  1. iOS 4s-6Plus屏幕自动适配及颜色转换为十六进制

    iOS各种屏幕自动适配及颜色转换为十六进制 ★★★XLJMatchScreen自动适配屏幕★★★ 支持pod导入 pod 'XLJScreenMatching', '~> 1.0.3' 如果发现 ...

  2. salesforce 零基础学习(五十八)通过sObject的field返回其对应的基础类型

    项目中有时候会要求通过sObject的Field的type类型返回其对应的基本类型,然后对其进行相关的处理,创建sObject的field可以选择的type类型是固定多的. 上述类型可以转换成几种基本 ...

  3. salesforce 零基础学习(五十五)java通过SOAP方式定时访问某个文件然后插入到sObject中

    项目源码:https://github.com/zhangyueqidlmu/SOAP-Access-SFDC.git 项目背景:salesforce端相关数据需要其他系统提供,其他系统可以提供相关数 ...

  4. 编写Windows服务疑问1:操作过程

    Windows 服务开发平时不太受人关注,毕竟那是高大上的项目类型,平常需求也用不上,很多老掉牙的家伙也只知有WinForm,仍不知有WPF,更别说Windows 服务了,正如陶渊明所写的,“不知有汉 ...

  5. 《JavaScript高级程序设计》读书笔记 2

    1,动态模型模式 function Person (name,age,job) { this.name=name; this.age=age; this.job=job; if(typeof this ...

  6. TextView使用大全

    最近打算写一个系列的android初级开发教程,预计40篇以上的文章,结合我实际工作中的经验,写一些工作中经常用到的技术,让初学者可以少走弯路,写一个系列的话,大家学习起来也有头有尾. 今天就从我们每 ...

  7. 劈荆斩棘:Gitlab 部署 CI 持续集成

    阅读目录: install configue gitlab-ci-multi-runner restore nuget packages bulid .sln run unit tests confi ...

  8. 挑子学习笔记:对数似然距离(Log-Likelihood Distance)

    转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/log-likelihood_distance.html 本文是“挑子”在学习对数似然距离过程中的笔记摘录,文 ...

  9. 基于HTML5 Canvas 实现矢量工控风机叶轮旋转

    之前在拓扑上的应用都是些静态的图元,今天我们将在拓扑上设计一个会动的图元--叶轮旋转. 先看看最后我们实现的效果:http://www.hightopo.com/demo/fan/index.html ...

  10. iOS 相机

    本章节主要为之前项目 JXHomepwner 添加照片功能(项目地址).具体任务就是显示一个 UIImagePickerController 对象,使用户能够为 JXItem 对象拍照并保存.拍摄的照 ...