本节主要实现的功能是在lcd上显示两行文字。

主要实现了两种

1、靠左侧上方显示

2、居中显示

两种的实现方法大同小异,主要的不同实在确定描字符时的源点。

一、靠左显示时,确定源点的方法

显示第一行文字的方法很简单,因为我们使用了FT_Set_Pixel_Sizes();函数,设置的字体的大小为24个像素,这样我们就可以得到第一行的源点是(0,24),因为这个坐标系的源点在左上角,需要转化一下于是就得到源点为(0,y方向像素 - 24)。

第二行的显示就稍显麻烦,我们需要在打印第一行文字时,通过bbox内的参数记录其长度最大的字符的长度,然后在第一行的基础上增加此长度,即可。

主要的源码实现就是这样

/* 确定座标:* lcd_x = 0* lcd_y = 24* 笛卡尔座标系:* x = lcd_x = 0* y = var.yres - lcd_y = var.yres - 24*/pen.x = 0 * 64;pen.y = (var.yres - 24) * 64;for (i = 0; i < wcslen(wstr1); i++) //wcslen(wstr1) 得到wchar字符串长度{/* set transformation */FT_Set_Transform( face, 0, &pen);/* load glyph image into the slot (erase previous one) */error = FT_Load_Char( face, wstr1[i], FT_LOAD_RENDER );if (error){printf("FT_Load_Char error\n");return -1;}error = FT_Get_Glyph( face->glyph, &glyph );if (error){printf("FT_Get_Glyph error!\n");return -1;}FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); //此处得到字符的最大最小坐标 然后得到整个字符中长度最大的那个字符的大小if (line_box_ymin > bbox.yMin)line_box_ymin = bbox.yMin;if (line_box_ymax < bbox.yMax)line_box_ymax = bbox.yMax;draw_bitmap( &slot->bitmap,slot->bitmap_left,var.yres - slot->bitmap_top);/* increment pen position */pen.x += slot->advance.x;pen.y += slot->advance.y;}/* 确定座标:* lcd_x = 0* lcd_y = line_box_ymax - line_box_ymin + 24* 笛卡尔座标系:* x = lcd_x = 0* y = var.yres - lcd_y = var.yres - (line_box_ymax - line_box_ymin + 24)*/pen.x = 0 * 64;pen.y = (var.yres - (line_box_ymax - line_box_ymin + 24)) * 64;for (i = 0; i < wcslen(wstr2); i++){/* set transformation */FT_Set_Transform( face, 0, &pen);/* load glyph image into the slot (erase previous one) */error = FT_Load_Char( face, wstr2[i], FT_LOAD_RENDER );if (error){printf("FT_Load_Char error\n");return -1;}error = FT_Get_Glyph( face->glyph, &glyph );if (error){printf("FT_Get_Glyph error!\n");return -1;}/*FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox );if (line_box_ymin > bbox.yMin)line_box_ymin = bbox.yMin;if (line_box_ymax < bbox.yMax)line_box_ymax = bbox.yMax;*/draw_bitmap( &slot->bitmap,slot->bitmap_left,var.yres - slot->bitmap_top);/* increment pen position */pen.x += slot->advance.x;//pen.y += slot->advance.y;}

二、居中显示

居中显示麻烦一点,代码的实现基本上是定义一个TGlyph_的结构体数组,先将需要显示的字符全部进行加载,然后通过一个函数计算出整串字符的长宽,最后在指定位置显示。

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_Htypedef struct TGlyph_ { FT_UInt index; /* glyph index */ FT_Vector pos; /* glyph origin on the baseline */ FT_Glyph image; /* glyph image */
} TGlyph, *PGlyph; #define MAX_GLYPHS  100int fd_fb;
struct fb_var_screeninfo var;   /* Current var */
struct fb_fix_screeninfo fix;   /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;unsigned short *pen_16; unsigned int *pen_32;   unsigned int red, green, blue;  pen_16 = (unsigned short *)pen_8;pen_32 = (unsigned int *)pen_8;switch (var.bits_per_pixel){case 8:{*pen_8 = color;break;}case 16:{/* 565 */red   = (color >> 16) & 0xff;green = (color >> 8) & 0xff;blue  = (color >> 0) & 0xff;color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);*pen_16 = color;break;}case 32:{*pen_32 = color;break;}default:{printf("can't surport %dbpp\n", var.bits_per_pixel);break;}}
}/* Replace this function with something useful. */void
draw_bitmap( FT_Bitmap*  bitmap,FT_Int      x,FT_Int      y)
{FT_Int  i, j, p, q;FT_Int  x_max = x + bitmap->width;FT_Int  y_max = y + bitmap->rows;//printf("x = %d, y = %d\n", x, y);for ( i = x, p = 0; i < x_max; i++, p++ ){for ( j = y, q = 0; j < y_max; j++, q++ ){if ( i < 0      || j < 0       ||i >= var.xres || j >= var.yres )continue;//image[j][i] |= bitmap->buffer[q * bitmap->width + p];lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);}}
}int Get_Glyphs_Frm_Wstr(FT_Face face, wchar_t * wstr, TGlyph glyphs[])
{int n;PGlyph glyph = glyphs;int pen_x = 0;int pen_y = 0;int error;FT_GlyphSlot  slot = face->glyph;;for (n = 0; n < wcslen(wstr); n++){glyph->index = FT_Get_Char_Index( face, wstr[n]); //根据Unicode码查找 得到字符的图像索引 保存到glyph->index/* store current pen position */ glyph->pos.x = pen_x;  //这里保存的是字符之间的相对位置glyph->pos.y = pen_y;        /* load时是把glyph放入插槽face->glyph */error = FT_Load_Glyph(face, glyph->index, FT_LOAD_DEFAULT); //根据glyph->index索引 将图像加载到face->glyph中if ( error ) continue;error = FT_Get_Glyph(face->glyph, &glyph->image ); //保存face->glyph到glyph->image中if ( error ) continue;/* translate the glyph image now */ /* 这使得glyph->image里包含有位置信息 */FT_Glyph_Transform(glyph->image, 0, &glyph->pos );pen_x += slot->advance.x;  /* 下一个字符的位置 单位是 1/64 point *//* increment number of glyphs */ glyph++;//指针自加 指向下一个数组项     }/* count number of glyphs loaded */ return (glyph - glyphs);  //指针相减得到加载了几个字符
}void compute_string_bbox(TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox )
{FT_BBox bbox; int n;bbox.xMin = bbox.yMin = 32000; bbox.xMax = bbox.yMax = -32000;for ( n = 0; n < num_glyphs; n++ ){FT_BBox glyph_bbox;FT_Glyph_Get_CBox(glyphs[n].image, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox ); //以像素为单位取出字体的最大最小//取出这整串字符长宽if (glyph_bbox.xMin < bbox.xMin)bbox.xMin = glyph_bbox.xMin;if (glyph_bbox.yMin < bbox.yMin)bbox.yMin = glyph_bbox.yMin;if (glyph_bbox.xMax > bbox.xMax)bbox.xMax = glyph_bbox.xMax;if (glyph_bbox.yMax > bbox.yMax)bbox.yMax = glyph_bbox.yMax;}*abbox = bbox;
}void Draw_Glyphs(TGlyph glyphs[], FT_UInt num_glyphs, FT_Vector pen)
{int n;int error;for (n = 0; n < num_glyphs; n++){FT_Glyph_Transform(glyphs[n].image, 0, &pen); //第三个参数代表 描绘的位置/* convert glyph image to bitmap (destroy the glyph copy!) */ //把Glyph转化为位图error = FT_Glyph_To_Bitmap(&glyphs[n].image, FT_RENDER_MODE_NORMAL, 0, /* no additional translation */  1 );       /* destroy copy in "image" */if ( !error ) { FT_BitmapGlyph bit = (FT_BitmapGlyph)glyphs[n].image; draw_bitmap(&bit->bitmap, bit->left, var.yres - bit->top); FT_Done_Glyph(glyphs[n].image ); //释放空间}}
}int main(int argc, char **argv)
{wchar_t *wstr1 = L"百问网gif";wchar_t *wstr2 = L"www.100ask.net";FT_Library   library;FT_Face     face;int error;FT_Vector     pen;FT_GlyphSlot  slot;int i;FT_BBox bbox;int line_box_ymin = 10000;int line_box_ymax = 0;int line_box_width;int line_box_height;TGlyph glyphs[MAX_GLYPHS]; /* glyphs table */ FT_UInt num_glyphs;if (argc != 2){printf("Usage : %s <font_file>\n", argv[0]);return -1;}fd_fb = open("/dev/fb0", O_RDWR);if (fd_fb < 0){printf("can't open /dev/fb0\n");return -1;}if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)){printf("can't get var\n");return -1;}if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)){printf("can't get fix\n");return -1;}line_width  = var.xres * var.bits_per_pixel / 8;pixel_width = var.bits_per_pixel / 8;screen_size = var.xres * var.yres * var.bits_per_pixel / 8;fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);if (fbmem == (unsigned char *)-1){printf("can't mmap\n");return -1;}/* 清屏: 全部设为黑色 */memset(fbmem, 0, screen_size);/* 显示矢量字体 */error = FT_Init_FreeType( &library );              /* initialize library *//* error handling omitted */error = FT_New_Face( library, argv[1], 0, &face ); /* create face object *//* error handling omitted */ slot = face->glyph;FT_Set_Pixel_Sizes(face, 24, 0);/* wstr1 */num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr1, glyphs);compute_string_bbox(glyphs, num_glyphs, &bbox);//计算宽度line_box_width  = bbox.xMax - bbox.xMin;//得到一行字体的宽度line_box_height = bbox.yMax - bbox.yMin;//得到一行字体的高度pen.x = (var.xres - line_box_width)/2 * 64;//pen.x的单位是64分之一个点pen.y = (var.yres - line_box_height)/2 * 64;Draw_Glyphs(glyphs, num_glyphs, pen);/* wstr2 */num_glyphs = Get_Glyphs_Frm_Wstr(face, wstr2, glyphs);compute_string_bbox(glyphs, num_glyphs, &bbox); line_box_width  = bbox.xMax - bbox.xMin; line_box_height = bbox.yMax - bbox.yMin;//X值不变 Y方向在上一行显示的往下24个pen.x = (var.xres - line_box_width)/2 * 64; pen.y = pen.y - 24 * 64;Draw_Glyphs(glyphs, num_glyphs, pen); //描绘return 0;
}

LINUX学习-在LCD上显示多行文字相关推荐

  1. 数码相框 在LCD上显示多行文字(6)

    数码相框 在LCD上显示多行文字(6) 目的: 1.从左边起显示几行文字 2.居中显示几行文字 在LCD上显示下列两行文字: 我是程序员gif Hello World 分析: 1.从左边起显示几行文字 ...

  2. STM32如何在LCD上显示单词、文字、图片等

    本文将介绍STM32F103R6如何在LCD12864上显示文字图片 注:本文中用到的字模软件,我放在了文章末尾,点击链接即可下载,是绿色版软件,解压即可使用! 点击下载LCD12864上显示文字图片 ...

  3. linux开发板lcd上显示jpg图片,关于在嵌入式开发板上显示一张jpeg图片

    #include "lcdjpg.h" static char g_color_buf[FB_SIZE]={0}; static int  g_fb_fd; static int ...

  4. 【正点原子I.MX6U-MINI应用篇】5、嵌入式Linux在LCD上显示BMP、JPG、PNG图片

    一.BMP图像介绍与显示 我们常用的图片格式有很多,一般最常用的有三种:JPEG(或 JPG).PNG.BMP和GIF.其中 JPEG(或JPG).PNG以及 BMP 都是静态图片,而 GIF 则可以 ...

  5. 数码相框(五、使用freetype库在LCD显示几行文字)

    注:本人已购买韦东山第三期项目视频,内容来源<数码相框项目视频>,只用于学习记录,如有侵权,请联系删除. 1.在LCD显示几行文字 (1) 在LCD显示几行文字,我们分为两种显示方法: ① ...

  6. 数码相框_在LCD上显示英文字母、汉字的点阵显示(2)

    数码相框_在LCD上显示英文字母.汉字的点阵显示 主要内容: 写应用程序,使LCD显示汉字和字符 原理: 在SDRAM内存里划出一块空间为FrameBuffer显存,LCD控制器会从FrameBuff ...

  7. 在 LCD 上显示 jpeg 图像

    1.图片格式有很多,一般最常用的有三种: JPEG(或 JPG). PNG. BMP. 在 LCD 上显示 BMP 图片格式: BMP 图像虽然没有失真.并且解析简单,但是由于图像数据没有进行任何压缩 ...

  8. 使用libjpeg库在LCD上显示图片

    背景: 网上已经有很多关于利用libjpeg显示图片的文章了,因此本文的技术含量不算高.本文是使用libjpeg的v8版本,在开发板的LCD上显示jpg格式图片,关于libjpeg,可到其官方网站下载 ...

  9. (原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。

    (原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间.显示格式为"时时:分分:秒秒".设有 4 个功能键k1-k4,功能如下: (1)k1--进入时间修 ...

最新文章

  1. BZOJ 1503 郁闷的出纳员(splay)
  2. 加载dict_PyTorch 7.保存和加载pytorch模型的两种方法
  3. 【图像分割模型】快速道路场景分割—ENet
  4. 每天学习flash一点(3) flash外部读取xml
  5. 《the way to go》一处关于go匿名函数的“勘误”
  6. oracle ldap 配置,Ubuntu安装OpenLDAP之配置LDAP
  7. o型圈沟槽设计软件_265 电机壳体上轴承室和轴承外圈增加的O型圈工艺对轴承外圈(防蠕动)作用有多大?...
  8. Android基础(三) 数据库SQLite
  9. android应用调试之如何保存logcat日志到本地
  10. 【源码】手把手教你用Python实现Vivado和ModelSim仿真自动化
  11. 2017年IT类公司世界500强企业及其简评
  12. 刘铁猛-深入浅出WPF-系列资源汇总
  13. Java角度制向弧度制转化
  14. Python高级第2课——飞机大战(只读课堂)
  15. 4K高清屏幕保护工具Aquarium 4K for Mac
  16. a标签中herf的用法
  17. php 微信小程序签到打卡,微信小程序实现打卡日历功能
  18. 企业数字化最核心的数据智能,它的价值到底在哪?
  19. python列表元组字典集合实验心得_python学习小总结(列表、元组、字典、集合、字符串)...
  20. 字体设计中的表现手法(一)

热门文章

  1. kali中rarcrack爆破rar压缩包密码
  2. 系统架构设计师(截图+做题思路+目录)
  3. python代理ip怎么写_python代理ip怎么写
  4. python批量检索文献pubmed_如何使用PubMed高效检索文献
  5. 计算机开机最快设置,怎样让电脑开机更快
  6. verilog中的signed用法
  7. FPGA之VGA转HDMI之编码模块的编写
  8. 反正切函数的应用解题报告
  9. 计算机到路由器用交叉线的好处,路由器与交换机连接-路由器和交换机之间是用交叉线还是用直通线联 – 手机爱问...
  10. 5G学习(四):PRACH专题