unit untQrcodetest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ActnList, ExtDlgs, jpeg, pngimage;

type
  TOpenLCD = function(sComm: PChar;  iBaudRate: Integer): Byte; stdcall;

  TCloseLCD = function: byte; stdcall;

  TShowQrCode = function(sQRCodeJPG: PChar; AlignmentX, AliagnmentY: Integer): Byte; stdcall;

  TfrmQrcodeTest = class(TForm)
    btn_open: TButton;
    cbbPort: TComboBox;
    Label1: TLabel;
    btn_close: TButton;
    btn_show: TButton;
    btn_exit: TButton;
    Label3: TLabel;
    cbbBaudRate: TComboBox;
    OpenPic1: TOpenPictureDialog;
    cbbAlignX: TComboBox;
    cbbAlignY: TComboBox;
    Label2: TLabel;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btn_openClick(Sender: TObject);
    procedure btn_closeClick(Sender: TObject);
    procedure btn_showClick(Sender: TObject);
    procedure btn_exitClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmQrcodeTest: TfrmQrcodeTest;

  dllhandle: THandle;
  OpenLCD: TOpenLCD;
  CloseLCD: TCloseLCD;
  ShowQrCode: TShowQrCode;

implementation

{$R *.dfm}

procedure TfrmQrcodeTest.FormCreate(Sender: TObject);
begin
  cbbPort.ItemIndex := 0;
  cbbBaudRate.ItemIndex := 0;
  cbbAlignX.ItemIndex := 0;
  cbbAlignY.ItemIndex := 0;
  
  dllhandle := SafeLoadLibrary('TLCDQRCODE.dll');
  //有效端口
  if (dllhandle <> 0) then
  begin //初始化函数
    @OpenLCD := GetProcAddress(dllhandle, 'OpenLCD');
    @CloseLCD := GetProcAddress(dllhandle, 'CloseLCD');
    @ShowQrCode := GetProcAddress(dllhandle, 'ShowQrCode');
  end;
end;

procedure TfrmQrcodeTest.btn_openClick(Sender: TObject);
var
  iError: Integer;
begin
  iError:= OpenLCD(PChar(cbbPort.Text), StrToInt(cbbBaudRate.Text));
  if iError <> 0 then
  begin
    ShowMessage('Error：' + IntToStr(iError));
    Exit;
  end;
  cbbPort.Enabled := False;
  cbbBaudRate.Enabled := False;
  btn_open.Enabled := False;
  btn_close.Enabled := True;
  btn_show.Enabled := True;
end;

procedure TfrmQrcodeTest.btn_closeClick(Sender: TObject);
begin
  CloseLCD;
  cbbPort.Enabled := True;
  cbbBaudRate.Enabled := True;
  btn_open.Enabled := True;
  btn_close.Enabled := False;
  btn_show.Enabled := False;
end;

procedure TfrmQrcodeTest.btn_showClick(Sender: TObject);
var
  iError: Integer;
  FileName: string;
  AlignmentX, AlignmentY: Integer;
begin
  AlignmentX := cbbAlignX.ItemIndex + 1;
  AlignmentY := cbbAlignY.ItemIndex + 1;
  if AlignmentY = 2 then
    AlignmentY := 3;

  if not OpenPic1.Execute then
    Exit;
  FileName := OpenPic1.FileName;
  iError:= ShowQrCode(PChar(FileName), AlignmentX, AlignmentY);
  if iError <> 0 then
      ShowMessage('Error：' + inttostr(iError));
end;

procedure TfrmQrcodeTest.btn_exitClick(Sender: TObject);
begin
  Close;
end;

end.

