else s:=s+abackground;
end;
for p:=7 downto 0 do
begin
if getbit(ord(cw[i*2+1]),p)=1 then s:=s+aforeground
else s:=s+abackground;
end;
s:=s+#13#10;
end;
end;
end;
finally
closefile(file16);
end;
result:=s;
end;
第二个关键技术:使用系统字库进行转换
其实使用系统字库是极为自由的方式,因为这样我们完全不必关心字库的技术,这一切都交给系统好了,让我们充分利用系统资源。
如果我们定义一个设备,然后设定好设备的各种属性,包括宽度、高度、字体、颜色等,然后在上面绘制文本就可以了,要转换为字符画,只需要把设备上的点阵信息转换为文本即可。
配合 createfontindirect 函数,使用 drawtext 可以绘制丰富的文本效果。实现完整的字符画效果
下面是十二号宋体的转换结果
█████ ██████
█ █
████████ █
██ ███
██████ █████
█████ ██████
█
█████ ██████
█████ ██████
█████ ██████
███ ██████
████████████
下面是九号@黑体的转换结果
████████████
██ ███ ████
██ ████ ████
██ █ ██ ████
██ █ █ ████
█ █ █
█ ██ ██ █
██ █ ██ ██ █
██ █ ██ ████
██ ████ ████
██ ███ ████
████████████
第三个关键技术:图片转换为文本
要把图像转换为文本,这其中有一个很大的困难,就是文本没有颜色,所以我们特别引进了一个概念:文本灰度,就是把不同字母在屏幕上显示的大小排序,得到一张灰度表,用这个灰度表来转换图片,可以达到比较好的效果。
下面的函数可以把一个位图转换成文本,abit 是位图,agray 是灰度
function imagetotext(abit:tbitmap;const agray: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);
for y:=0 to abit.height-1 do
begin
for x:=0 to abit.width-1 do
begin
pcolor:=abit.canvas.pixels[x,y];
r:=pcolor and $ff;
g:=(pcolor shr 8) and $ff;
b:=(pcolor shr 16) and $ff;
igray:=hibyte(r*77+g*151+b*28);
iindex:=(igray*igraylen div 255);
if iindex<1 then iindex:=1;
if iindex>igraylen then iindex:=igraylen;
s:=s+sgrayper[iindex];