//将图片转换成BMP格式,以便比较,主要是因为数码相机采集的图片基本上是JPEG 格式的。 //所以在装入时需要将其转换成BMP图片,便于比较 Function Pic2Bmp(PicFileName:String):TBitmap; var Str:String; Icon: TIcon; Jpeg1:TJPEGIMAGE; MetaFile:TMetafile; begin result:=TBitMap.create; result.PixelFormat:=pf24bit; Str := ExtractFileExt(PicFileName); Str :=Uppercase(Copy(Str,2,3)); if (Str='BMP') then result.LoadFromFile(PicFileName) else //Jpeg转换成BMP if (Str='JPG') or (Str='JPEG') then begin Jpeg1 := TJPEGIMAGE.Create; Jpeg1.LoadFromFile(PicFileName); result.Assign(Jpeg1); Jpeg1.free; end else //ICO 转换成BMP if (Str='ICO') or (Str='ico') then begin Icon := TIcon.Create; Icon.LoadFromFile(PicFileName); result := TBitmap.Create; result.Width := Icon.Width; result.Height := Icon.Height; result.Canvas.Draw(0, 0, Icon); Icon.Free;
end else //WMF转换成BMP if Str='WMF' then begin Metafile:=TMetaFile.create;
MetaFile.LoadFromFile(PicFileName); with result do begin Height:=Metafile.Height; Width:=Metafile.Width; Canvas.Draw(0,0,MetaFile);
end; MetaFile.Free; end;
end;
constructor TBmp2Number.Create; begin inherited Create; Ftempl_index:=1; Ftempl_Path:='template'; Fdiscern_rate:=100; end;
//设置模板索引 procedure TBmp2Number.SetTempl_index(value:integer); begin FtempL_index:=Value; initTemplate; end;
procedure TBmp2Number.SetTempl_path(value:String); begin FTempl_path:=value; initTemplate; end;
//初始化运行模板库 procedure TBmp2Number.InitTemplate; begin if tempL_Index=0 then exit; if Not DirectoryExists(Templ_Path) then exit;
end;
//逐行扫描的方式识别图片中的数字 function TBmp2Number.GetNumber:string;
var tmpleBmp,midbmp:TBitMap; row,Col,mRow,mCol,numberNo:integer; NewRow,NewCol:integer; PSource, Ptemple: pRGBTripleArray; mR, mG, mB, tR, tG, tB: Integer; mP, tP: pRGBTripleArray; succ:Boolean;//对比是否成功 begin //数据的初步检查 if sourcebitmap=nil then begin showmessage('请装入目标图片!'); exit; end; FNumberList:=Tlist.Create; try if assigned(pb_Count) then pb_Count.Max:= SourceBitMap.width; if assigned(pb_deta) then pb_deta.Max:= SourceBitMap.Height;
for Col:=0 to SourceBitMap.width-1 do begin if stop then exit; if ShowDemo then memo1.Lines.Add('本列是:'+inttostr(col)); if assigned(pb_Count) then begin pb_Count.Position:=col; Application.ProcessMessages; end; for Row:=0 to SourceBitMap.Height-1 do begin if assigned(pb_deta) then begin pb_deta.Max:= SourceBitMap.Height; pb_deta.Position:=Row; Application.ProcessMessages; end; if stop then exit; for numberNo:=0 to 9 do //0至9的数字分别装入模板 begin if not fileexists(Templ_Path+inttostr(numberNo)+'_'+inttostr(Templ_index)+'.bmp') then continue;
if stop then exit; tmpleBmp:=TBitMap.create; tmpleBmp:=Pic2Bmp(Templ_Path+inttostr(numberNo)+'_'+inttostr(Templ_index)+'.bmp'); // tmpleBmp.LoadFromFile(Templ_Path+inttostr(numberNo)+'_'+inttostr(Templ_index)+'.bmp'); if ShowDemo then image1.Picture.bitmap:=tmplebmp;
midbmp.Canvas.CopyRect(Rect(0,0,midbmp.width,midbmp.height), sourcebitmap.canvas, Rect(Col,row,Col+tmpleBmp.width,row+tmpleBmp.height)); if ShowDemo then image2.picture.bitmap:=midbmp; Assert(midbmp.PixelFormat = tmpleBmp.PixelFormat); //然后逐行取值比较 for mRow:=0 to MidBmp.Height-1 do begin mp:= midBmp.ScanLine[mRow]; tp:= tmplebmp.ScanLine[mRow]; if stop then exit; for mcol:=0 to MidBmp.Width -1 do begin if stop then exit; mR := mp[mcol].rgbtRed; mG := mP[mcol].rgbtGreen; mB := mP[mcol].rgbtBlue; tR := tP[mcol].rgbtRed; tG:= tP[mcol].rgbtGreen; tB:=tP[mcol].rgbtBlue; if ShowDemo then memo1.Lines.Add('Mrow:'+Inttostr(mrow)+'MCol:'+inttostr(Mcol)+'mr:' +inttostr(mr)+'mg:'+inttostr(mg)+'mb:'+ inttostr(mb)+'tr:'+inttostr(tr)+'tg:'+inttostr(tg)+'tb:'+ inttostr(tb)); if (mR <> tR) or (mG <> tG) or (mB <> tB) then//如果像素不等 begin succ:=false; break;
end else succ:=true;
end; // end mcol:=0 to midbmp.width -1 do
end; //end mrow:=0 to midbmp.height-1 do freeandnil(midbmp); if succ then begin//对比成功 new(tmeplate); tmeplate.number:= numberNo; tmePlate.top := Row; tmePlate.left:=col; FNumberList.Add(tmeplate); end ; // else // break; freeandnil(tmpleBmp); end; //end for number
end; //end for row end; // end for col except showmessage('对比失败!'); end;
for row:=0 to FNumberList.Count -1 do Result:=result+inttostr(Ptemplate(FNumberlist.Items[row]).number); for row:=FNumberList.count-1 downto 0 do begin Dispose(Ptemplate(FNumberList[Row])); FNumberlist.Delete(row); end; FNumberlist.free; end;