Barcode Recognition with Borland Delphi

Barcode Recognition Software

The Softek Barcode Toolkit is an application programmers toolkit which reads barcodes from image files and bitmaps - with a choice of 3 types of interface: DLL, OCX and COM .

The following code was kindly provided by Marian Aldenhövel of MBA Software-Consulting.

 

unit SoftekBarCode;

{ (c)2003 Marian Aldenhövel MBA Software-Consulting
     Rosenhain 23
     53123 Bonn

     marian@mba-software.de

  This unit dynamically imports the functions from the Softek Barcode Library.

  Usage:

  - Call InitLibrary from somewhere in your program before calling any of
    the functions.
  - See the documentation from Softek for the use of the actual functions.

}

interface

uses Windows,SysUtils;

type short=shortint;
     long=integer;
     PLong=^long;

var stGetBitmapResolution:function():integer; stdcall;
    stSetBitmapResolution:function(newValue:integer):integer; stdcall;
    stGetDebugTraceFile:function():PChar; stdcall;
    stSetDebugTraceFile:function(lpszNewValue:PChar):PChar; stdcall;
    stGetLineJump:function():integer; stdcall;
    stSetLineJump:function(nNewValue:integer):integer; stdcall;
    stGetLowerRatio:function():double; stdcall;
    stSetLowerRatio:function(newValue:double):double; stdcall;
    stGetMinOccurrence:function():short; stdcall;    
    stSetMinOccurrence:function(nNewValue:short):short; stdcall;    
    stGetMultipleRead:function():bool; stdcall;    
    stSetMultipleRead:function(bNewValue:bool):bool; stdcall;
    stGetNoiseReduction:function():short; stdcall;
    stSetNoiseReduction:function(nNewValue:short):short; stdcall;    
    stGetPageNo:function():short; stdcall;    
    stSetPageNo:function(nNewValue:short):short; stdcall;    
    stGetPrefOccurrence:function():short; stdcall;    
    stSetPrefOccurrence:function(nNewValue:short):short; stdcall;    
    stGetQuietZoneSize:function():short; stdcall;    
    stSetQuietZoneSize:function(nNewValue:short):short; stdcall;    
    stGetRotation:function():short; stdcall;    
    stSetRotation:function(nNewValue:short):short; stdcall;    
    stGetUpperRatio:function():double; stdcall;    
    stSetUpperRatio:function(newValue:double):double; stdcall;    
    stGetReadCode39:function():bool; stdcall;
    stSetReadCode39:function(bNewValue:bool):bool; stdcall;
    stGetReadEAN13:function():bool; stdcall;    
    stSetReadEAN13:function(bNewValue:bool):bool; stdcall;    
    stGetReadEAN8:function():bool; stdcall;
    stSetReadEAN8:function(bNewValue:bool):bool; stdcall;    
    stGetReadUPCA:function():bool; stdcall;    
    stSetReadUPCA:function(bNewValue:bool):bool; stdcall;    
    stGetReadUPCE:function():bool; stdcall;    
    stSetReadUPCE:function(bNewValue:bool):bool; stdcall;    
    stGetShowCheckDigit:function():bool; stdcall;    
    stSetShowCheckDigit:function(bNewValue:bool):bool; stdcall;    
    stGetReadCode128:function():bool; stdcall;    
    stSetReadCode128:function(bNewValue:bool):bool; stdcall;    
    stGetCode39NeedStartStop:function():bool; stdcall;    
    stSetCode39NeedStartStop:function(bNewValue:bool):bool; stdcall;    
    stGetReadCode25:function():bool; stdcall;    
    stSetReadCode25:function(bNewValue:bool):bool; stdcall;    
    stGetColorThreshold:function():short; stdcall;    
    stSetColorThreshold:function(nNewValue:short):short; stdcall;    
    stGetBarString:function(index:short):PChar; stdcall;    
    stScanBarCode:function(FileName:PChar):short; stdcall;    
    stScanBarCodeFromBitmap:function(hBitmap:long):short; stdcall;    
    stGetBarStringType:function(index:short):PChar; stdcall;    
    stGetBarStringPos:function(nBarCode:short;var pTopLeftX,pTopLeftY,pBotRightX,pBotRightY:Long):short; stdcall;
    stSetScanRect:function(TopLeftX,TopLeftY,BottomRightX,BottomRightY:Long;MappingMode:short):bool; stdcall;
    stGetReadPatchCodes:function():bool; stdcall;
    stSetReadPatchCodes:function(bNewValue:bool):bool; stdcall;
    stSaveBitmap:function(hBitmap:Long;strFilePath:PChar):bool; stdcall;

function stGetErrorString(ErrorCode:short):string;

procedure InitLibrary;
procedure DoneLibrary;

type ESoftekBarCodeError=class(Exception);

implementation

function stGetErrorString(ErrorCode:short):string;
begin
  case ErrorCode of
    -1:Result:='Error opening file';
    -2:Result:='File is Multi Plane';
    -3:Result:='Invalid number of bits per sample';
    -4:Result:='Memory allocation error';
    -5:Result:='Invalid TIF photometric value';
    else Result:=Format('Unknown error %d',[ErrorCode]);
  end;
end;

var LibraryHandle:THandle=0;

procedure InitLibrary;

  function DoGetProcAdress(aName:string):pointer;
  begin
    Result:=GetProcAddress(LibraryHandle,PChar(aName));
    if Result=NIL then RaiseLastOSError;
  end;

begin
  if LibraryHandle<>0 then exit;

  LibraryHandle:=LoadLibrary('SoftekBarCode.dll');
  if LibraryHandle=0 then RaiseLastOSError;

  @stGetBitmapResolution:=DoGetProcAdress('stGetBitmapResolution');
  @stSetBitmapResolution:=DoGetProcAdress('stSetBitmapResolution');
  @stGetDebugTraceFile:=DoGetProcAdress('stGetDebugTraceFile');
  @stSetDebugTraceFile:=DoGetProcAdress('stSetDebugTraceFile');
  @stGetLineJump:=DoGetProcAdress('stGetLineJump');
  @stSetLineJump:=DoGetProcAdress('stSetLineJump');
  @stGetLowerRatio:=DoGetProcAdress('stGetLowerRatio');
  @stSetLowerRatio:=DoGetProcAdress('stSetLowerRatio');
  @stGetMinOccurrence:=DoGetProcAdress('stGetMinOccurrence');
  @stSetMinOccurrence:=DoGetProcAdress('stSetMinOccurrence');
  @stGetMultipleRead:=DoGetProcAdress('stGetMultipleRead');
  @stSetMultipleRead:=DoGetProcAdress('stSetMultipleRead');
  @stGetNoiseReduction:=DoGetProcAdress('stGetNoiseReduction');
  @stSetNoiseReduction:=DoGetProcAdress('stSetNoiseReduction');
  @stGetPageNo:=DoGetProcAdress('stGetPageNo');
  @stSetPageNo:=DoGetProcAdress('stSetPageNo');
  @stGetPrefOccurrence:=DoGetProcAdress('stGetPrefOccurrence');
  @stSetPrefOccurrence:=DoGetProcAdress('stSetPrefOccurrence');
  @stGetQuietZoneSize:=DoGetProcAdress('stGetQuietZoneSize');
  @stSetQuietZoneSize:=DoGetProcAdress('stSetQuietZoneSize');
  @stGetRotation:=DoGetProcAdress('stGetRotation');
  @stSetRotation:=DoGetProcAdress('stSetRotation');
  @stGetUpperRatio:=DoGetProcAdress('stGetUpperRatio');
  @stSetUpperRatio:=DoGetProcAdress('stSetUpperRatio');
  @stGetReadCode39:=DoGetProcAdress('stGetReadCode39');
  @stSetReadCode39:=DoGetProcAdress('stSetReadCode39');
  @stGetReadEAN13:=DoGetProcAdress('stGetReadEAN13');
  @stSetReadEAN13:=DoGetProcAdress('stSetReadEAN13');
  @stGetReadEAN8:=DoGetProcAdress('stGetReadEAN8');
  @stSetReadEAN8:=DoGetProcAdress('stSetReadEAN8');
  @stGetReadUPCA:=DoGetProcAdress('stGetReadUPCA');
  @stSetReadUPCA:=DoGetProcAdress('stSetReadUPCA');
  @stGetReadUPCE:=DoGetProcAdress('stGetReadUPCE');
  @stSetReadUPCE:=DoGetProcAdress('stSetReadUPCE');
  @stGetShowCheckDigit:=DoGetProcAdress('stGetShowCheckDigit');
  @stSetShowCheckDigit:=DoGetProcAdress('stSetShowCheckDigit');
  @stGetReadCode128:=DoGetProcAdress('stGetReadCode128');
  @stSetReadCode128:=DoGetProcAdress('stSetReadCode128');
  @stGetCode39NeedStartStop:=DoGetProcAdress('stGetCode39NeedStartStop');
  @stSetCode39NeedStartStop:=DoGetProcAdress('stSetCode39NeedStartStop');
  @stGetReadCode25:=DoGetProcAdress('stGetReadCode25');
  @stSetReadCode25:=DoGetProcAdress('stSetReadCode25');
  @stGetColorThreshold:=DoGetProcAdress('stGetColorThreshold');
  @stSetColorThreshold:=DoGetProcAdress('stSetColorThreshold');
  @stGetBarString:=DoGetProcAdress('stGetBarString');
  @stScanBarCode:=DoGetProcAdress('stScanBarCode');
  @stScanBarCodeFromBitmap:=DoGetProcAdress('stScanBarCodeFromBitmap');
  @stGetBarStringType:=DoGetProcAdress('stGetBarStringType');
  @stGetBarStringPos:=DoGetProcAdress('stGetBarStringPos');
  @stSetScanRect:=DoGetProcAdress('stSetScanRect');
  @stGetReadPatchCodes:=DoGetProcAdress('stGetReadPatchCodes');
  @stSetReadPatchCodes:=DoGetProcAdress('stSetReadPatchCodes');
  @stSaveBitmap:=DoGetProcAdress('stSaveBitmap');
end;

procedure DoneLibrary;
begin
  if LibraryHandle=0 then exit;

  if not FreeLibrary(LibraryHandle) then RaiseLastOSError;

  stGetBitmapResolution:=NIL;
  stSetBitmapResolution:=NIL;
  stGetDebugTraceFile:=NIL;
  stSetDebugTraceFile:=NIL;
  stGetLineJump:=NIL;
  stSetLineJump:=NIL;
  stGetLowerRatio:=NIL;
  stSetLowerRatio:=NIL;
  stGetMinOccurrence:=NIL;
  stSetMinOccurrence:=NIL;
  stGetMultipleRead:=NIL;
  stSetMultipleRead:=NIL;
  stGetNoiseReduction:=NIL;
  stSetNoiseReduction:=NIL;
  stGetPageNo:=NIL;
  stSetPageNo:=NIL;
  stGetPrefOccurrence:=NIL;
  stSetPrefOccurrence:=NIL;
  stGetQuietZoneSize:=NIL;
  stSetQuietZoneSize:=NIL;
  stGetRotation:=NIL;
  stSetRotation:=NIL;
  stGetUpperRatio:=NIL;
  stSetUpperRatio:=NIL;
  stGetReadCode39:=NIL;
  stSetReadCode39:=NIL;
  stGetReadEAN13:=NIL;
  stSetReadEAN13:=NIL;
  stGetReadEAN8:=NIL;
  stSetReadEAN8:=NIL;
  stGetReadUPCA:=NIL;
  stSetReadUPCA:=NIL;
  stGetReadUPCE:=NIL;
  stSetReadUPCE:=NIL;
  stGetShowCheckDigit:=NIL;
  stSetShowCheckDigit:=NIL;
  stGetReadCode128:=NIL;
  stSetReadCode128:=NIL;
  stGetCode39NeedStartStop:=NIL;
  stSetCode39NeedStartStop:=NIL;
  stGetReadCode25:=NIL;
  stSetReadCode25:=NIL;
  stGetColorThreshold:=NIL;
  stSetColorThreshold:=NIL;
  stGetBarString:=NIL;
  stScanBarCode:=NIL;
  stScanBarCodeFromBitmap:=NIL;
  stGetBarStringType:=NIL;
  stGetBarStringPos:=NIL;
  stSetScanRect:=NIL;
  stGetReadPatchCodes:=NIL;
  stSetReadPatchCodes:=NIL;
  stSaveBitmap:=NIL;
end;

initialization
finalization
  DoneLibrary;
end.