unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,MAKEPDF417Lib_TLB, ExtCtrls, StdCtrls,ComObj,ComCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Memo1: TMemo;
    Panel1: TPanel;
    Button2: TButton;
    Button3: TButton;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Label5: TLabel;
    Label6: TLabel;
    Edit4: TEdit;
    Edit5: TEdit;
    GroupBox2: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;

    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    barcode:IBarCodePDF417 ;
    CurrentDir:string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  CurrentDir := GetCurrentDir();
  //barcode:=CoBarCode2.Create;
  barcode:=CreateComObject(CLASS_BarCodePDF417) as  IBarCodePDF417;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  bmpFile:WideString;
  vData:Variant;
  vLen:string;
  vHeight:string;

  vszText:string;
  vFontSize:string;
  vTxtHeight:string;
  vHmargin:string;
  vVmargin:string;
  vTxtCntOneLine:string;

begin
  //VarDataInit(&v);
  //TVarData(v).VType := varByte;
  vData := VarArrayCreate([0, (Length(Memo1.Text)-1)],varByte);
  //v:=VarArrayCreate([0,1024],varByte);
  bmpFile := 'temp.bmp';
  vLen := Edit1.Text;
  vHeight :=Edit4.Text;

  vszText :=Edit7.Text;
  vFontSize :=Edit8.Text;
  vTxtHeight :=Edit9.Text;
   vHmargin :=Edit10.Text;
  vVmargin :=Edit11.Text;
  vTxtCntOneLine :=Edit6.Text;
  //s:= '123456';
  //for i:=0 to Length(s)-1 do
  //begin
   // m:= Byte(s[i+1]);
  //end;
  barcode.MakeBinary(vData,bmpFile,StrToInt(vLen),1,3,StrToInt(vHeight));
  barcode.MixText(bmpFile,bmpFile,vszText,StrToInt(vFontSize),StrToInt(vTxtHeight),StrToInt(vHmargin),StrToInt(vVmargin),StrToInt(vTxtCntOneLine));
  Image1.Picture.LoadFromFile('temp.bmp');

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   barcode:=nil;
end;

//procedure Make(varData: OleVariant; const strBmpFileName: WideString; nClumn: Integer; nErr: Integer; nHLRatio: Integer);


procedure TForm1.Button2Click(Sender: TObject);
var
  vData:Variant;
  vLen:string;
  bmpFile:WideString;
  //CurrentDir:string;

begin
   //CurrentDir := GetCurrentDir();
   OpenDialog1.InitialDir := CurrentDir;
   //CreateDir(CurrentDir+'\\bmp\\');
   if OpenDialog1.Execute then
      Memo1.Lines.LoadFromFile(OpenDialog1.FileName)
   else
      Memo1.Lines.Clear;
   vData := VarArrayCreate([0, (Length(Memo1.Text)-1)],varByte);
   bmpFile := 'temp.bmp';
   vLen := Edit1.Text;
   barcode.MakeBinary(vData,bmpFile,StrToInt(vLen),1,3,50);

   Image1.Picture.LoadFromFile('temp.bmp');
end;

procedure TForm1.Button3Click(Sender: TObject);
var
   //CurrentDir:string;
   NewFile: TFileStream;
   OldFile: TFileStream;

begin
   //CurrentDir := GetCurrentDir();
   CreateDir(CurrentDir+'\\bmp\\');
   SaveDialog1.InitialDir := CurrentDir + '\\bmp\\';
   SaveDialog1.FileName := 'barcode.bmp';
   SaveDialog1.Filter := '位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||';
   SaveDialog1.Title := '另存为';
   if (SaveDialog1.Execute()) then
   begin
      OldFile := TFileStream.Create(CurrentDir+'\\temp.bmp', fmOpenRead);
      try
         NewFile := TFileStream.Create(SaveDialog1.FileName, fmCreate);
         try
            NewFile.CopyFrom(OldFile, OldFile.Size);
         finally
            FreeAndNil(NewFile);
         end;
      finally
         FreeAndNil(OldFile);
      end;
   end;
end;

end.

