數碼相框---在LCD上顯示一個向量字型(5)
數碼相框---在LCD上顯示一個向量字型(5)
回顧freetye官方給出的示例example1:
FT_Init_Freetype(&library):初始化Freetype
FT_New_Face(library, argv[1], 0, &face):開啟一個字型檔案,得到一個face平面
FT_Set_Pixel_Sizes(face,24,0):設定字型大小24 X 24
FT_Set_Transform(face,&matrix,&pen):設定轉換矩陣,轉換引數,旋轉角度matrix,指定它的位置pen
FT_Load_Char(face, chinese_str[n], FT_LOAD_RENDER):
draw_bitmap():列印,把點陣存到全域性陣列image裡,最後使用show_image()打印出來
現在需要在LCD上顯示,修改draw_bitmap(),以前把點陣存到image裡,直接把它寫到LCD的frambuffer裡面。
使用的韌體:
交叉編譯器:arm-linux-gcc-4.3.2
Kernel核心:linux-3.4.2
bootloader:u-boot-2012.04.01
把之前的交叉編譯器arm-linux-gcc-3.4.5改為交叉編譯器arm-linux-gcc-4.3.2。(使用新的工具鏈編譯u-boot-2012.04.01)
(首先要明確,第三期視訊用的交叉編譯器是4.3.2,核心是linux-3.4.2,二者必須匹配,否則編譯出錯。二期視訊用的編譯器3.4.5,核心Linux-2.6.22.6,二者必須匹配,否則編譯出錯。)
(1)先把arm-linux-gcc-4.3.2.tar.bz2上傳到伺服器
(2)使用命令tar xjf arm-linux-gcc-4.3.2.tar.bz2 解壓到當前目錄
(3)echo $PATH 列印當前環境變數,把交叉編譯器改為arm-linux-gcc-4.3.2。輸入命令:export PATH=/home/book/bin:/home/book/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/work/tools/arm-linux-gcc-4.3.2/usr/local/arm/4.3.2/bin:/snap/bin
(4)檢視是否可行:arm-linux-gcc -v,顯示4.3.2
(5)不想每次手工設定,可以修改 sudo vi /etc/environment這個檔案,重啟虛擬機器才有效
在LCD上顯示一個向量字型
安裝freetype到交叉編譯目錄裡(供arm-linux-gcc使用)
1.1 首先檢視,需要安裝到哪個lib和include目錄裡
1)通過$PATH找到arm-linux-gcc交叉編譯位於:
/usr/local/arm/4.3.2/bin
然後進入.../arm/4.3.2/ 目錄,通過find查詢stdio.h檔案,找到:
所以編譯出來的標頭檔案應該放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include
2)通過find查詢lib,找到:find -name lib
由於ARM9屬於ARMv4T架構,所以編譯出來的庫檔案應該放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib
交叉編譯freetype庫,並把freetype安裝到交叉編譯工具鏈
1、tar xjf freetype-2.4.10.tar.bz2(解壓)
2、cd freetype-2.4.10(進入目錄)
3、./configure --host=arm-linux(配置它為交叉編譯,表示編譯出來的程式在arm-linux開發板)
4、make(編譯)
5、make DESTDIR=$PWD/tmp install(把它安裝在當前目錄的tmp臨時目錄下)
6、sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib -d -rf(把tmp/usr/local/lib/* 複製到交叉編譯工具鏈裡面去/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib )
7、sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include -rf(把tmp/usr/localinclude/* 複製到交叉編譯工具鏈裡面去/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include )
8、編譯官方提供測試程式example1
顯示編譯錯誤: 56:38: error: freetype/config/ftheader.h: No such file or directory
通過find找到ftheader./的真正位置位於:../include/freetype2/freetype/config/ftheader.h
輸入cd ./arm-none-linux-gnueabi/libc/usr/include/freetype2
mv freetype/ ../freetype (將freetype2下的freetype移到include目錄下)
9、編譯:
arm-linux-gcc -finput-charset=GBK -o example1 example1.c -lfreetype -lm
(-lfreetype:加上動態庫,-lm:加上數學庫,-finput-charset=GBK:指定輸入的字符集)
10、到arm開發執行程式時,顯示缺少動態庫*so檔案,
cd freetype-2.4.10/tmp/usr/local/lib
cp *so* /work/nfsroor/fs_mini_mdev_new/lib -d(把所有的so檔案拷貝到根檔案系統)
11、拷貝字型檔案:cp simsun.ttc /work/nfs_root/fs_mini_mdev_new
12、拷貝測試程式:cp example1 /work/nfs_root/fs_mini_mdev_new
12、執行./example1 ./simsun.ttc
在LCD上顯示輸出
程式碼:
#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_H
#define FONTDATAMAX 4096
static const unsigned char fontdata_8x16[FONTDATAMAX] = {
....ASCII碼點陣太多了,省略
};
int 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;
int fd_hzk16;
struct stat hzk_stat;
unsigned char *hzkmem;
/* 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;
}
}
}
void lcd_put_ascii(int x, int y, unsigned char c)
{
unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
int i, b;
unsigned char byte;
for (i = 0; i < 16; i++)
{
byte = dots[i];
for (b = 7; b >= 0; b--)
{
if (byte & (1<<b))
{
/* show */
lcd_put_pixel(x+7-b, y+i, 0xffffff); /* 白 */
}
else
{
/* hide */
lcd_put_pixel(x+7-b, y+i, 0); /* 黑 */
}
}
}
}
void lcd_put_chinese(int x, int y, unsigned char *str)
{
unsigned int area = str[0] - 0xA1;
unsigned int where = str[1] - 0xA1;
unsigned char *dots = hzkmem + (area * 94 + where)*32;
unsigned char byte;
int i, j, b;
for (i = 0; i < 16; i++)
for (j = 0; j < 2; j++)
{
byte = dots[i*2 + j];
for (b = 7; b >=0; b--)
{
if (byte & (1<<b))
{
/* show */
lcd_put_pixel(x+j*8+7-b, y+i, 0xffffff); /* 白 */
}
else
{
/* hide */
lcd_put_pixel(x+j*8+7-b, y+i, 0); /* 黑 */
}
}
}
}
/* 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];
//輸出函式,因為buffer是一個位元組,而lcd_put_pixel()是0x00RRGGBB,所以打印出來的是藍色字型
lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);//引數:x座標,y座標,顏色值
}
}
}
int main(int argc, char **argv)
{
unsigned char str[] = "中";
wchar_t *chinese_str = L"繁";
FT_Library library;
FT_Face face;
int error;
FT_Vector pen; /* untransformed origin */
FT_GlyphSlot slot;
FT_Matrix matrix; /* transformation matrix */
double angle;
if (argc != 3)//打印出用法
{
printf("Usage : %s <font_file> <angle>\n", argv[0]);
return -1;
}
fd_fb = open("/dev/fb0", O_RDWR);//開啟LCD
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;
}
fd_hzk16 = open("HZK16", O_RDONLY);
if (fd_hzk16 < 0)
{
printf("can't open HZK16\n");
return -1;
}
if(fstat(fd_hzk16, &hzk_stat))
{
printf("can't get fstat\n");
return -1;
}
hzkmem = (unsigned char *)mmap(NULL , hzk_stat.st_size, PROT_READ, MAP_SHARED, fd_hzk16, 0);
if (hzkmem == (unsigned char *)-1)
{
printf("can't mmap for hzk16\n");
return -1;
}
/* 清屏: 全部設為黑色 */
memset(fbmem, 0, screen_size);
lcd_put_ascii(var.xres/2, var.yres/2, 'A');
printf("chinese code: %02x %02x\n", str[0], str[1]);
lcd_put_chinese(var.xres/2 + 8, var.yres/2, str);
/* 顯示向量字型,把show_font和example2兩個合在一起 */
error = FT_Init_FreeType( &library ); /* 初始化freetype庫 */
/* error handling omitted */
//裝載字型檔案
error = FT_New_Face( library, argv[1], 0, &face ); /* 開啟一個字型檔案 */
/* error handling omitted */
//每得到一個face,都會有一個slot插槽,插槽就是裝載一個字型裡面的資料
//每載入一個文字,插槽slot就會變換一下,裡面的資料時最新的
slot = face->glyph;
//設定字型的大小24X24
FT_Set_Pixel_Sizes(face, 24, 0);
/* 確定座標: A中\向量字型\
* lcd_x = var.xres/2 + 8 + 16 //8是A的座標,16是中的座標
* lcd_y = var.yres/2 + 16
* 轉換為笛卡爾座標系:
* x = lcd_x = var.xres/2 + 8 + 16
* y = var.yres/2 - lcd_y = var.yres/2 - 16
*/
//pen就是向量字型的位置
pen.x = (var.xres/2 + 8 + 16) * 64;//單位是64分之一畫素
pen.y = (var.yres/2 - 16) * 64;
//旋轉角度
//strtoul:把字串轉換為unsigned long
angle = ( 1.0 *strtoul(argv[2], NULL, 0) / 360 ) * 3.14159 * 2; /* use 25 degrees */
/* 設定矩陣 */
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 );
/* 設定轉換方法 */
FT_Set_Transform( face, &matrix, &pen );//設定轉換矩陣matrix,轉換引數,0表示不旋轉
/* load glyph image into the slot (erase previous one) */
//FT_LOAD_RENDER引數會把glyph向量檔案轉換為點陣圖
//根據chinese_str(字元unicode碼),在字型檔案face得到glyph,載入到slot(face->glyph)裡面
error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER );
if (error)
{
printf("FT_Load_Char error\n");
return -1;
}
/* now, draw to our target surface (convert position) */
//描繪出來,slot就是face裡面的glyph
draw_bitmap( &slot->bitmap,
slot->bitmap_left,//bitmap_left這是笛卡爾座標系
var.yres - slot->bitmap_top );//slot->bitmap_top是笛卡爾座標系
return 0;
}
編譯與執行:
arm-linux-gcc -finput-charset=GBK -fexec-charset=GBK -o show_font show_font.c-lfreetype -lm
./show_font ./simsun.ttc 0
./show_font ./simsun.ttc 45
效果圖: