www.pudn.com > PKEncodeDemo.zip > Unit1.pas


unit Unit1; 

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons;

type
  TForm1 = class(TForm)
    BitBtnEncode: TBitBtn;
    Panel1: TPanel;
    Image: TImage;
  procedure FormCreate(Sender: TObject);
  procedure BitBtnEncodeClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
end;


Type
    pPTPDF417ENCODESTRUCT=^PTPDF417ENCODESTRUCT;
    PTPDF417ENCODESTRUCT = record
        pData :              PChar ;    //Pointer to the data to be encoded.
        nDataLength :        Integer	; //Length of the data in bytes.
        bisTruncated :       Integer	; //Writes truncated PDF417 symbols.
        wEccPercent :        Smallint	; //Determines the error correction level by percentage.
        wEccLevel  :         Smallint	; //Determines the ECC level for encoding a PDF417 symbol.
        wCols  :             Smallint	; //Number of columns in the symbol.
        wRows :              Smallint	; //Number of rows in the symbol.
        wAspectHeigh :       Smallint	; //The height part of the aspect ratio of the symbol.
        wAspectWidth :       Smallint	; //The width part of the aspect ratio of the symbol.
        wXModule :           Smallint	; //The smallest element width in pixels.
        wModuleAspectRatio : Smallint	; //The ratio of a row height to XModule .
        wLeftSpace :         Smallint	; //The left   space of the symbol in pixels while generating Image.
        wRightSpace :        Smallint	; //The right  space of the symbol in pixels while generating Image.
        wTopSpace :          Smallint	; //The top    space of the symbol in pixels while generating Image.
        wBottomSpace :       Smallint	; //The bottom space of the symbol in pixels while generating Image.
End;


Type
    pPTIMAGESTRUCT=^PTIMAGESTRUCT  ;
    PTIMAGESTRUCT = record
        dwWidth  :     DWORD;     //The width of the image in pixels.
        dwHeight :     DWORD;     //The height of the image in pixels.
        pBits :        PByte ;    //Pointer to the image data.
        pPalette:      PByte;     //Pointer to the palette data (RGBQUAD)for 1,4,8 bit image.
        wBitsPerPixel: Smallint   //Number of bits per pixel.
End;


const
  PT_IMAGERW_FAIL                   =$00000000; //An error occured in an operation.
  PT_IMAGERW_SUCCESS                =$00000001; //An operation is successful. 
  PT_IMAGERW_ALLOC_ERROR            =$00000100; //Error while allocating memory. 
  PT_IMAGERW_FORMAT_UNSUPPORTED     =$00000101; //The format of image is unsupported. 

  PT_PDF417ENCODE_FAIL              =$00000000; //An operation is Failed.
  PT_PDF417ENCODE_SUCCESS           =$00000001; //An operation is successful. 
  PT_PDF417ENCODE_ALLOC_ERROR       =$00000200; //Error while allocating the memory. 
  PT_PDF417ENCODE_DATA_BIG          =$00000201; //Data to be encoded is too big. 
  PT_PDF417ENCODE_SIZE_SMALL        =$00000202; //The size of image to be pasted the symbol is too small. 
  PT_PDF417ENCODE_IMAGE_INVALID     =$00000203; //The image to be pasted is invalid.

  //PDF417 ECC level constants
  PT_PDF417_ECCLEVEL_0 = 0;       //Use ECC level 0. This uses 2   codewords for error correction.
  PT_PDF417_ECCLEVEL_1 = 1;       //Use ECC level 1. This uses 4   codewords for error correction.
  PT_PDF417_ECCLEVEL_2 = 2;       //Use ECC level 2. This uses 8   codewords for error correction.
  PT_PDF417_ECCLEVEL_3 = 3;       //Use ECC level 3. This uses 16  codewords for error correction.
  PT_PDF417_ECCLEVEL_4 = 4;       //Use ECC level 4. This uses 32  codewords for error correction.
  PT_PDF417_ECCLEVEL_5 = 5;       //Use ECC level 5. This uses 64  codewords for error correction.
  PT_PDF417_ECCLEVEL_6 = 6;       //Use ECC level 6. This uses 128 codewords for error correction.
  PT_PDF417_ECCLEVEL_7 = 7;       //Use ECC level 7. This uses 256 codewords for error correction.
  PT_PDF417_ECCLEVEL_8 = 8;       //Use ECC level 8. This uses 512 codewords for error correction.
  PT_PDF417_ECC_PERCENT= 9;       //Use the percentage to determine the ECC level.


  Procedure PtInitImage(pImage : pPTIMAGESTRUCT);
        stdcall; far; external 'PtImageRW.dll' name 'PtInitImage';
  Function PtLoadImage(fileName : String; pImage : pPTIMAGESTRUCT) : Integer;
      stdcall; far; external 'PtImageRW.dll' name 'PtLoadImage';
  Function PtSaveImage( fileName : String; pImage : pPTIMAGESTRUCT) : Integer;
      stdcall; far; external 'PtImageRW.dll' name 'PtSaveImage';
  Function PtCreateImage( pImage : pPTIMAGESTRUCT; ImageSize: DWORD; PaletteSize:DWORD ) : Integer;
      stdcall; far; external 'PtImageRW.dll' name 'PtCreateImage';
  Procedure PtFreeImage(pImage : pPTIMAGESTRUCT);
      stdcall; far; external 'PtImageRW.dll' name 'PtFreeImage';

  Procedure PTPDF417EncodeInit(pEncode : pPTPDF417ENCODESTRUCT) ;
        stdcall; far; external 'PtPDF417Encode.dll' name 'PtPDF417EncodeInit';
  Function PtPDF417Encode(pEncode : pPTPDF417ENCODESTRUCT; pImage : pPTIMAGESTRUCT) : Integer;
        stdcall; far; external 'PtPDF417Encode.dll' name 'PtPDF417Encode';
  Function PtPDF417EncodeToImage(pEncode : pPTPDF417ENCODESTRUCT; pImage : pPTIMAGESTRUCT;
                                 StartX:Integer; StartY : Integer) : Integer;
        stdcall; far; external 'PtPDF417Encode.dll' name 'PtPDF417EncodeToImage';
var
  Form1:    TForm1;
  m_image : PTIMAGESTRUCT;




implementation

{$R *.dfm}

procedure TForm1.BitBtnEncodeClick(Sender: TObject);
var
  ret : integer;
  m_encode : PTPDF417ENCODESTRUCT;
  str : String;//encode a string
  data: array [0..9] of byte;//encode binary data
begin
    PtPDF417EncodeInit(@m_encode);
    str := 'Hello world!';
    m_encode.pData := pChar(str);
    m_encode.nDataLength :=lstrlen( m_encode.pData);
    //If want to encode binary data, use following code
    //m_encode.pData := @data[0];
    //m_encode.nDataLength := 10;

    ret := PtPDF417Encode(@m_encode, @m_image);
    If ret <> PT_PDF417ENCODE_SUCCESS Then
    begin
        ShowMessage('Encode Error');
        Exit;
    End;

    ret := PtSaveImage( 'tempPDF417.bmp', @m_image);
    If ret <> PT_IMAGERW_SUCCESS Then
        ShowMessage('save bmp file  Error')
    Else
      Image.Picture.LoadFromFile('tempPDF417.bmp');

    PtFreeImage(@m_image);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  ret : integer;
begin
  PtInitImage(@m_image);
end;

end.