⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 TRY2LPR-1.0开源的车牌识别核心
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, LPR_h, ExtCtrls, StdCtrls, ijl, ComCtrls;

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Button1: TButton;
    StatusBar1: TStatusBar;
    Edit1: TEdit;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    procedure LoadFile(FileName:string);
    procedure LoadJPG(FileName : string);
    procedure LoadBMP(FileName : string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.LoadFile(FileName: string);
begin
  if '.BMP' = UpperCase(ExtractFileExt(FileName)) then
  begin
    LoadBMP(FileName);
  end else
  begin
    LoadJPG(FileName);
  end;
end;

procedure TForm1.LoadJPG(FileName: string);
var
   jcprops : TJPEG_CORE_PROPERTIES;
   iStatus : Integer;
   iWidth, iHeight, iNChannels : Integer;
   iLineWidth : Integer;
   iPadBytes : Integer;
   iImageSize : Integer;
   DIB : TDIBSection;
begin
   //初始化jpg库
   iStatus := ijlinit(@jcprops);
   if iStatus<0 then
       raise Exception.Create('Own: Error in IJL function.');
   //设置TJPEG_CORE_PROPERTIES;
   jcprops.JPGFile := PChar(FileName);
   //读取jpg文件的参数,如:图象的宽高等
   iStatus := ijlRead(@jcprops,IJL_JFILE_READPARAMS);
   if iStatus<0 then
       raise Exception.Create('Own: Error in IJL function.');
   //图象的宽高位数
   iWidth := jcprops.JPGWidth;
   iHeight := jcprops.JPGHeight;
   iNChannels := jcprops.JPGChannels;
   //设置Image控件
   with Image1.Picture.Bitmap do
   begin
      Width := iWidth;
      Height := iHeight;
      if iNChannels = 4 then
        PixelFormat := pf32bit
      else
        PixelFormat := pf24bit;
   end;
   Image1.Width:=iWidth;
   Image1.Height:=iHeight;
   //将DIB和Image关联
   FillChar(DIB,SizeOf(DIB),0);
   GetObject(Image1.Picture.Bitmap.Handle,SizeOf(DIB),@DIB);
   //设置DIB
   jcprops.DIBWidth := iWidth;
   jcprops.DIBHeight := -iHeight;
   jcprops.DIBChannels := iNChannels;
   if iNChannels = 4 then
     jcprops.DIBColor := IJL_RGBA_FPX
   else
     jcprops.DIBColor := IJL_BGR;
   jcprops.DIBPadBytes := ((((iWidth*iNChannels)+3) div 4)*4)-(iWidth*iNChannels);
   jcprops.DIBBytes := PByte(DIB.dsBm.bmBits);

   if (jcprops.JPGChannels = 3) then
      jcprops.JPGColor := IJL_YCBCR
   else if (jcprops.JPGChannels = 4) then
      jcprops.JPGColor := IJL_YCBCRA_FPX
   else if (jcprops.JPGChannels = 1) then
      jcprops.JPGColor := IJL_G
   else
   begin
      // This catches everything else, but no
      // color twist will be performed by the IJL.
      jcprops.DIBColor := TIJL_COLOR (IJL_OTHER);
      jcprops.JPGColor := TIJL_COLOR (IJL_OTHER);
   end;

   //读取并解码图象
   iStatus := ijlRead(@jcprops,IJL_JFILE_READWHOLEIMAGE);
   if iStatus<0 then
       raise Exception.Create('Own: Error in IJL function.');
   Image1.Repaint;
   ijlFree(@jcprops);
end;

procedure TForm1.LoadBMP(FileName: string);
begin
  Image1.Picture.LoadFromFile(FileName);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  version: WORD;
  LPInfo: TLPInfo;

  MS:TMemoryStream;
  FileName:string;
  T:Integer;

  LPRect : TRect;
  iRet : Integer;
  pImg : PChar;
  Bi: BITMAPINFOHEADER ;
  Bf: BITMAPFILEHEADER;
  res : integer;
begin
   InitLPRDll(Form1.Handle, version);
   if OpenDialog1.Execute then
   begin
       FileName:=OpenDialog1.FileName;
       LoadFile(FileName);
       MS:=TMemoryStream.Create;
       Image1.Picture.Bitmap.SaveToStream(MS);
       MS.Position := 0;
       MS.ReadBuffer(Bf, sizeof(BITMAPFILEHEADER));

       MS.Position := sizeof(BITMAPFILEHEADER);
       MS.ReadBuffer(Bi, sizeof(BITMAPINFOHEADER));
       pImg := AllocMem(Bf.bfSize-sizeof(BITMAPFILEHEADER));
       if (pImg = nil) then
         Exit;
       MS.Position := sizeof(BITMAPFILEHEADER);
       MS.ReadBuffer(pImg^, Bf.bfSize-sizeof(BITMAPFILEHEADER));
////////////////////////////////调用识别引擎///////////////////////////////////
       T:=GetTickCount;
       res := GetLPR(LPInfo, pImg);
       T := GetTickCount - T;
       if res = 1 then
       begin
         Statusbar1.Panels[0].Text := '车牌号码: ' + LPInfo.LPNum;
         case LPInfo.LPColor of
           LP_BLACK:
             Statusbar1.Panels[1].Text := '车牌颜色: ' + '黑色';
           LP_BLUE:
             Statusbar1.Panels[1].Text := '车牌颜色: ' + '蓝色';
           LP_YELLOW:
             Statusbar1.Panels[1].Text := '车牌颜色: ' + '黄色';
           LP_WHITE:
             Statusbar1.Panels[1].Text := '车牌颜色: ' + '白色';
         end;
         Statusbar1.Panels[2].Text := '识别时间:' + IntToStr(T)+'ms';
         Edit1.Text := LPInfo.LPNum;
         LPRect.Left := LPInfo.LPRect.left;
         LPRect.Right := LPInfo.LPRect.right;
         LPRect.Top := LPInfo.LPRect.top;
         LPRect.Bottom := LPInfo.LPRect.bottom;

         // 画定位框
         Image1.Canvas.Brush.Color := clRed;
         Image1.Canvas.FrameRect(LPRect);
       end
       else
         ShowMessage('识别失败');
/////////////////////////////////////////////////////////////////////////
       if (pImg <> nil) then
         FreeMem(pImg);
       MS.Position := 0;
       MS.Free;
   end;

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -