複製鏈接
請複製以下鏈接發送給好友

字符畫

鎖定
字符畫,一種由字母、標點、漢字或其他字符組成的圖畫。簡單的字符畫是利用字符的形狀代替圖畫的線條來構成簡單的人物、事物等形象,它一般由人工製作而成;複雜的字符畫通常利用佔用不同數量像素的字符代替圖畫上不同明暗的點,它一般由程序製作而成。字符畫是互聯網時代的產物,通常應用於即時聊天中。
中文名
字符畫
所屬學科
IT
所屬領域
技術
應用領域
繪畫

字符畫關鍵技術

第一個關鍵技術:漢字庫讀取技術
使用漢字庫技術可以做到和操作系統無關性,先了解一下點陣
字符畫 字符畫
字庫的基本原理。如下所示,下面是一個“字”的點陣圖,在16點陣字庫中一個漢字為16x16點,每一行使用兩個字節表示,如下面示例第一行的十六進制為:0x02和0x00,所以,一個漢字在16點陣字庫中需要佔用2x16個字節,24點陣字庫需要3x24個字節,下面僅以16點陣字庫為例,其他點陣類似。 ██████ █████████
███████ ████████
██ ██
██ ██████████ ██
█ ██████████ ███
███ █████
█████████ ██████
████████ ███████
███████ █████ ██
███████ ████████
███████ ████████
███████ ████████
███████ ████████
█████ █ ████████
██████ █████████
下面的函數返回指定字符串的字符畫文本
functionGet16(constAWord,AForeground,ABackground:string):string;
functionGetBit(constc,n:byte):integer;
begin
result:=(cshrn)and1;
end;
var
iLen:integer;
iFileSize:integer;
s:string;
字符畫 字符畫
k,l,i,p:integer;
cw:array[0..31]ofchar;
qu_ma,wei_ma:integer;
File16:file;
begin
iLen:=length(AWord);
AssignFile(File16,piProgramInfo.Path+HZK16);
FileMode:=fmOpenRead;
try
Reset(File16,1);
finally
FileMode:=fmOpenReadWrite;
end;
iFileSize:=FileSize(File16);
try
forl:=1toiLendiv2do
begin
k:=l*2-1;
//如果不是漢字,往前進一位
whilekiLenthenbreak;
if((ord(AWord[k])and$80)0)then
begin
qu_ma:=ord(AWord[k])-161;
wei_ma:=ord(AWord[k+1])-161;
if(94*qu_ma+wei_ma)*32+32>iFileSizethencontinue;
try
seek(File16,(94*qu_ma+wei_ma)*32);
except
myMessageBox(fseekcallfail!);
exit;
字符畫 字符畫
end;BlockRead(File16,cw,32); fori:=0to15do
begin
forp:=7downto0do
begin
ifGetBit(ord(cw[i*2]),p)=1thens:=s+AForeground
elses:=s+ABackground;
end;
forp:=7downto0do
begin
ifGetBit(ord(cw[i*2+1]),p)=1thens:=s+AForeground
elses:=s+ABackground;
end;
s:=s+#13#10;
end;
end;
end;
finally
CloseFile(File16);
end;
result:=s;
end;
第二個關鍵技術:使用系統字庫進行轉換
其實使用系統字庫是極為自由的方式,因為這樣完全不必關心字庫的技術,這一切都交給系統好了,充分利用系統資源。
如果定義一個設備,然後設定好設備的各種屬性,包括寬度、高度、字體、顏色等,然後在上面繪製文本就可以了,要轉換為字符畫,只需要把設備上的點陣信息轉換為文本即可。配合CreateFontIndirect函數,使用DrawText可以繪製豐富的文本效果。實現完整的字符畫效果 下面是十二號宋體的轉換結果:
字符畫 字符畫
█████ ██████
█ █
████████ █
██ ███
██████ █████
█████ ██████
█████ ██████
█████ ██████
█████ ██████
███ ██████
████████████ 下面是九號@黑體的轉換結果:
████████████
██ ███ ████
██ ████ ████
██ █ ██ ████
██ █ █ ████
█ █ █
█ ██ ██ █
██ █ ██ ██ █
██ █ ██ ████
██ ████ ████
██ ███ ████
████████████
第三個關鍵技術:圖片轉換為文本
要把圖像轉換為文本,這其中有一個很大的困難,就是文本沒有顏色,所以特別引進了一個概念:文本灰度,就是把不同字母在屏幕上顯示的大小排序,得到一張灰度表,用這個灰度表來轉換圖片,可以達到比較好的效果。
下面的函數可以把一個位圖轉換成文本,ABit是位圖,AGray是灰度
functionImageToText(ABit:TBitmap;constAGray:string):string;
var
x,y:integer;
s:string;
pColor:Longint;
R,G,B:byte;
字符畫 字符畫
iGray:integer;sGrayPer:string;
iGrayLen:integer;
iIndex:integer;
begin
s:=;
sGrayPer:=AGray;
iGrayLen:=Length(sGrayPer);
fory:=0toABit.Height-1do
begin
forx:=0toABit.Width-1do
begin
pColor:=ABit.Canvas.Pixels[x,y];
R:=pColorand$FF;
G:=(pColorshr8)and$FF;
B:=(pColorshr16)and$FF; iGray:=HiByte(R*77+G*151+B*28);
iIndex:=(iGray*iGrayLendiv255);
ifiIndexiGrayLentheniIndex:=iGrayLen;
s:=s+sGrayPer[iIndex];
end;
s:=s+Crlf;
end;
result:=s;
end;
這是一個常用且效果比較好的灰度:“MNHQ$OC?7>!:-;.”
第四個關鍵技術:把文本轉換為圖像
字符畫 字符畫
要把文本轉換為圖片,必須獲取兩個重要參數:轉換後的寬和高,要取得這兩個參數,我們可以使用GetTextExtentPoint32函數,該函數的定義如下:
functionGetTextExtentPoint32(DC:HDC;Str:PChar;Count:Integer;varSize:TSize):BOOL;
DC傳入設備句柄
Str為文本內容
Count為文本的長度(字節)
Size返回寬和高
在實際應用中,往往被轉換的文本有多行,且每一行的長度不定,
所以我們還需要在生成圖像前進行一遍預掃,以便獲得完整的圖像大小 下面演示了文本轉換為圖像的代碼
////////////////////////////////////////////////////////////////////////////////
//功能:把文本轉換為位圖
//AOwner:窗體參數
//AText:要轉換的文本
//AFont:文本的字體
//ABitmap:轉換後的位圖對象
//日期:2003.12.15
////////////////////////////////////////////////////////////////////////////////
procedureTextToBitmap(AOwner:TObject;constAText:TStrings;AFont:TFont;ABitmap:TBitmap);
var
i:integer;
iWidth,iHeight:integer;
iCharHeight:integer;
s:string;
r:TRect;
size:TSize;
lblTemp:TLabel;
begin
iWidth:=0;
iHeight:=0;
lblTemp:=TLabel.Create(nil);
r.Top:=0;
try
lblTemp.Visible:=false;
lblTemp.Parent:=TWinControl(AOwner);
lblTemp.Font.Assign(AFont);
ABitmap.Canvas.Brush.Style:=bsClear;
ABitmap.Canvas.Pen.Color:=rgb(0,0,0);
ABitmap.Canvas.Brush.Color:=RGB(255,255,255);
ABitmap.Canvas.Font.Assign(AFont);
//下面代碼用户獲得文本的最大寬度和高度
fori:=0toAText.Count-1do
begin
s:=AText.Strings[i];
ifs=thens:=;
lblTemp.Caption:=s;
GetTextExtentPoint32(lblTemp.Canvas.Handle,pchar(lblTemp.Caption),lblTemp.GetTextLen,size);
ifiWidth<size. cxtheniWidth:=Size. cx;
iHeight:=iHeight+Size. cy;
end;
//獲得一個字符的高度
GetTextExtentPoint32(lblTemp.Canvas.Handle,pchar(),length(),size);
iCharHeight:=size. cy;
ABitmap.Width:=iWidth;
ABitmap.Height:=iHeight;
fori:=0toAText.Count-1do
begin
s:=AText.Strings[i];
r.Left:=0;
r.Right:=ABitmap.Width;
r.Bottom:=r.Bottom+iCharHeight;
DrawText(ABitmap.Canvas.Handle,PChar(s),length(s),r,0);
r.Top:=r.Top+iCharHeight;
end;
finally
lblTemp.Free;
end;
end;

字符畫轉換方法

圖片轉換成字符畫,一幅SHE的字符畫,由密密麻麻的字符組成,粉絲們真是瘋狂得可以,一個字母一個字母地拼,不怕被整崩潰啊沒想到,原來有一種專門將圖片轉換為字符的程序。 下面要介紹的就是一個專門的圖片轉換網站。找一張合適的圖片,點擊“瀏覽”,然後點Submit,這樣就得到字符畫了。如果想保存下來,接下來為這張圖取一個名字(不支持中文),然後點Submit,進入另一個頁面後點DownloadNow,你會得到一個txt格式的文件,剛才的字符畫就保存其中了。另外,此頁面還可以調整字符畫的明暗並選擇不同的字符(字符畫上方)。點GotoMyPage,就會得到一個專門保存剛才字符畫的頁面。