嵌入式Linux-LCD显示多行文字
显示文字这里我用了freetype库。
以左上角显示两行文字:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h> #include "show_font.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/ftglyph.h> unsigned char *hzkmem;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width; struct fb_var_screeninfo var; 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 short *pen_32;
unsigned char red,green,blue; pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned short *)pen_8; switch( pixel_width*8 )
{
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 support %ddpp\n",pixel_width*8 );
break;
}
} 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; 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 main( int argc, char **argv )
{
int hzk_fd;
int fd_fb;
struct fb_fix_screeninfo fix; int screen_size; FT_Library library;
FT_Error error;
FT_Face face;
FT_Matrix matrix; /* transformation matrix */
FT_Vector pen; /* untransformed origin */
FT_BBox bbox;
FT_Glyph glyph; int i; double angle; wchar_t *chinese_char = L"周zhou";
wchar_t *chinese_char2 = L"嵌入式Linux"; int line_box_ymin=800;
int line_box_ymax=0; struct stat hzk_stat; fd_fb = open("/dev/fb0", O_RDWR );
if( fd_fb<0 )
{
perror("oepn failed");
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("mmap is failed\n");
return -1;
} memset( fbmem, 0, screen_size ); if( argc != 2 )
{
printf("Usage: %s <font_file>\n", argv[0]);
return -1;
} error = FT_Init_FreeType( &library ); /* initialize library */
/* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* use 50pt at 100dpi */
error = FT_Set_Pixel_Sizes( face, 30, 0 ); /* set character size */ angle = ( 0.0 / 360 ) * 3.14159 * 2; /* use 25 degrees */
/* set up matrix */
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); /* the pen position in 26.6 cartesian space coordinates; */
/* start at (0,24) relative to the upper left corner */
pen.x = (0) * 64;
pen.y = ( var.yres - 24 ) * 64;
for( i=0; i<wcslen(chinese_char); i++ )
{
/* set transformation */
FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */
error = FT_Load_Char( face, chinese_char[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( &face->glyph->bitmap,
face->glyph->bitmap_left,
var.yres - face->glyph->bitmap_top ); /* increment pen position */
pen.x += face->glyph->advance.x;
pen.y += face->glyph->advance.y;
} /* the pen position in 26.6 cartesian space coordinates; */
/* start at (0,24) relative to the upper left corner */
pen.x = (0) * 64;
pen.y = ( var.yres-( line_box_ymax-line_box_ymin+24) ) * 64;
for( i=0; i<wcslen(chinese_char2); i++ )
{
/* set transformation */
FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */
error = FT_Load_Char( face, chinese_char2[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( &face->glyph->bitmap,
face->glyph->bitmap_left,
var.yres - face->glyph->bitmap_top ); /* increment pen position */
pen.x += face->glyph->advance.x;
pen.y += face->glyph->advance.y;
} return 0; }
在屏幕的中间左右上下对称显示:
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h> #include "show_font.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/ftglyph.h> typedef 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 100 unsigned char *hzkmem;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width; struct fb_var_screeninfo var; 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 short *pen_32;
unsigned char red,green,blue; pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned short *)pen_8; switch( pixel_width*8 )
{
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 support %ddpp\n",pixel_width*8 );
break;
}
} 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; 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_From_Wstr( FT_Face face, wchar_t* wstr, TGlyph glyphs[] )
{
int n;
PGlyph glyph = glyphs;
int pen_x=0;
int pen_y=0;
int error; for( n=0; n<wcslen( wstr ); n++ )
{
glyph->index = FT_Get_Char_Index( face, wstr[n] );
/* store current pen position */
glyph->pos.x = pen_x;
glyph->pos.y = pen_y; error = FT_Load_Glyph( face, glyph->index, FT_LOAD_DEFAULT );
if ( error )
continue; error = FT_Get_Glyph( face->glyph, &glyph->image );
if ( error )
continue; /* translate the glyph image now */
FT_Glyph_Transform( glyph->image, 0, &glyph->pos ); pen_x += face->glyph->advance.x ; /* 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!) */
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 )
{
int hzk_fd;
int fd_fb;
struct fb_fix_screeninfo fix; int screen_size; FT_Library library;
FT_Error error;
FT_Face face;
FT_Matrix matrix; /* transformation matrix */
FT_Vector pen; /* untransformed origin */
FT_BBox bbox; TGlyph glyphs[MAX_GLYPHS]; /* glyphs table */
PGlyph glyph; /* current glyph in table */
FT_UInt num_glyphs; int i; double angle; wchar_t *chinese_char = L"周zhou";
wchar_t *chinese_char2 = L"嵌入式Linux"; int line_box_ymin=800;
int line_box_ymax=0; int line_box_width;
int line_box_hight; struct stat hzk_stat; fd_fb = open("/dev/fb0", O_RDWR );
if( fd_fb<0 )
{
perror("oepn failed");
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("mmap is failed\n");
return -1;
} memset( fbmem, 0, screen_size ); if( argc != 2 )
{
printf("Usage: %s <font_file>\n", argv[0]);
return -1;
} error = FT_Init_FreeType( &library ); /* initialize library */
/* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* use 50pt at 100dpi */
error = FT_Set_Pixel_Sizes( face, 24, 0 ); /* set character size */ num_glyphs = Get_Glyphs_From_Wstr( face, chinese_char, glyphs);
compute_string_bbox( glyphs, num_glyphs, &bbox );
line_box_width = bbox.xMax - bbox.xMin;
line_box_hight = bbox.yMax - bbox.yMin;
pen.x = (var.xres - line_box_width)/2*64;
pen.y = (var.yres - line_box_hight)/2*64;
Draw_Glyphs( glyphs, num_glyphs, pen ); num_glyphs = Get_Glyphs_From_Wstr( face, chinese_char2, glyphs);
compute_string_bbox( glyphs, num_glyphs, &bbox );
line_box_width = bbox.xMax - bbox.xMin;
line_box_hight = bbox.yMax - bbox.yMin;
pen.x = (var.xres - line_box_width)/2*64;
pen.y = pen.y-24*64;
Draw_Glyphs( glyphs, num_glyphs, pen ); return 0; }
sd
嵌入式Linux-LCD显示多行文字的更多相关文章
- iOS开发遇到的错误 -- Label显示多行文字导致宽度和高度的问题
Label的宽度问题 注意:UILabel下面需要设置preferredMaxLayoutWidth ,设置了autolayout和numberofline的UIlabel才显示多行 label宽度的 ...
- linux cat显示若干行
[一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1000 [二]显示1000行到3000行 cat ...
- Linux LCD 显示图片【转】
转自:https://blog.csdn.net/niepangu/article/details/50528190 BMP和JPEG图形显示程序1) 在LCD上显示BMP或JPEG图片的主流程图首 ...
- 修改Linux可显示的行数
在/boot/grub/menu.lst中,找到kernel开头的那一行,在后面加上参数vga=791 下面是vga可以取的值 # +----------------------------- ...
- bat显示多行文字,逐个显示哦!不同的颜色!
最近想修改bat文件输出提示的时候能有不同的颜色提示,在网上找了下,发现这个文章,实现的不错,先记录下来,留着后面研究. 这是曾经写的,又稍微改进了一下. @echo off set str=青天有月 ...
- CSS实现多行文字限制显示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- css实现多行文字限制显示&编译失效解决方案
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- label正确显示多行数据
label显示多行文字时会遇到文字中包含换行符“\n”,这时需要对字符进行全部替换, if(labelContent.indexOf('\\n')>0){labelContent=labelCo ...
- 嵌入式Linux 修改启动LOGO
1.嵌入式 Linux LOGO显示原理 嵌入式Linux是直接在FrameBuffer的基础上.直接显示一个ppm格式的图象. 它 kernel/drivers/video/fbc ...
随机推荐
- 冲刺随笔——Day_Ten
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...
- 色相偏移 HueShift ASE
色相偏移可以改变颜色色调,unity ASE没有参考UE4写个,原理很简单,将颜色向量绕(1,1,1)旋转,就可以得到不同色调的颜色. https://zhuanlan.zhihu.com/p/677 ...
- python MD5加密和flask-generate_password_hash
实际开发过程中,有些数据是需要加密保存或者处理的,为了就是为了保证源数据的安全性.那么MD5加密作为一种简单有效的非对称加密方式在日常开发过程中也经常的被使用到.下面就来介绍下MD5算法: 1. * ...
- 第7.17节 Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析
第7.17节 Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析 静态方法也是通过类定义的一种方法,一般将不需要访问类属性但是类需要具有的一些能力可以静态方法提供. 一 ...
- PyQt(Python+Qt)学习随笔:QTreeView树形视图的indentation属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的indentation属性用于控制视图中每级数据项之间的缩进,对于顶级项 ...
- PyQt学习随笔:Model/View中诸如DisplayRole的数据角色及含义
在PyQt中,模型可以针对不同的组件(或者组件的不同部分,比如存储数据.界面展示数据.按钮的提示等)提供不同的数据.例如,Qt.DisplayRole用于视图的文本显示.通常来说,模型中的数据项包含一 ...
- 关于select下拉框选择触发事件
最开始使用onclick设置下拉框触发事件发现会有一些问题: <select> <option value="0" onclick="func0()&q ...
- Day1-7【Scrum 冲刺博客集合】
Day1-Day7博客链接 Day1[Scrum 冲刺博客] Day2[Scrum 冲刺博客] Day3[Scrum 冲刺博客] Day4[Scrum 冲刺博客] Day5[Scrum 冲刺博客] D ...
- 冲刺Day5
每天举行站立式会议照片: 前后端交互: 昨天已完成的工作: 1.确认搜索栏界面 2.订单模块的大部分代码 3.用户模块的大部分代码 4.测试登录注册功能 燃尽图: 今天计划完成的工作: 成员 任务 高 ...
- 题解-NOI2003 智破连环阵
题面 NOI2003 智破连环阵 有 \(m\) 个靶子 \((ax_j,ay_j)\) 和 \(n\) 个箭塔 \((bx_i,by_i)\).每个箭塔可以射中距离在 \(k\) 以内的靶子.第 \ ...