从回调函数的参数中获取值

时间:2016-12-08 17:41:22

标签: delphi dll biometrics

我正在使用biometrics SDK。我将标题转换为delphi以使用dll。

它看起来像这样:

const
{VrBio_EventType}
  VRBIO_CAPTURE_EVENT_UNPLUG               = $001; {Fingerprint scanner unplugged from the computer.}
  VRBIO_CAPTURE_EVENT_PLUG                 = $002; {Fingerprint scanner plugged on the computer.}
  VRBIO_CAPTURE_EVENT_REMOVED              = $004; {Finger removed from the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_PLACED               = $008; {Finger placed on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_IMAGE_FRAME          = $10;  {A fingerprint frame was captured on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_IMAGE_CAPTURED       = $020; {A fingerprint image was captured on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_FAKE_FINGER_DETECTED = $400; {A false finger has been detected on the sensor}
  VRBIO_CAPTURE_EVENT_FAKE_FINGER_REMOVED  = $800; {A false finger has been removed from the sensor}

type

(* Stores the parameters of the ISO 19794-4 image format. @see VGetReaderProperties @see VrBio_ReaderProperty 
typedef struct
{
    /** @see VrBio_ISO197944CompressionMode*/
    int compressionMode;
    /** @see VrBio_ISO197944ImpressionType*/
    int impressionType;
    /** @see VrBio_ISO197944FingerPosition*/    
    int fingerPosition;

}VrBio_ISO197944Parameters;
*)

  PISO197944Parameters = ^TISO197944Parameters;
  TISO197944Parameters = record
      compressionMode: Integer; { @see VrBio_ISO197944CompressionMode}
      impressionType: Integer;  { @see VrBio_ISO197944ImpressionType}
      fingerPosition: Integer;  { @see VrBio_ISO197944FingerPosition}
  end;

(* Represents a biometric image. @see VrBio_CaptureEventCallback \ref VSyncCapture 
struct VrBio_BiometricImage
{
  /** Image width.*/
  int width;
  /**Image height*/
  int height;
  /**Image resolution in dpi. For the obsolete functions, use pixels/cm.*/
  int resolution;
  /**Number of channels in the image. Fingerprint images should always be grayscale, so this value is always 1.*/
  int channels;
  /**Biometric modality. 
  * Always use VRBIO_BIOMETRIC_MODALITY_FINGERPRINT. 
  * \ref VrBio_BiometricModality.
  */
  int biometricModality;
  /**Scanner type. 
  * \ref VrBio_ScannerType.
  */
  int scannerType;
  /**Formato de imagem: Formato da imagem.
  *\ ref VrBio_ImageFormat.*/
  int imageFormat;
  /**Size of the buffer*/
  int bufferSize;
  /**Compression rate. Valid for images that allow compression.
  * \ref VrBio_CompressionRate
  */
  int compressionRate; 
 /**Quality of the fingerprint image. 
  * \ref VrBio_FingerQuality
  */
  int fingerQuality;

  /** Only valid if the image if imageFormat is \ref VrBio_ImageFormat::VRBIO_IMAGEFORMAT_ISO19794_4 
  *\ref VrBio_ISO197944Parameters
  */
  VrBio_ISO197944Parameters* ISO197944_parameters;

  /** Buffer storing the pixels of the image.
    The position(x,y,c) of a pixel is y*width*channels+x*channels+c.
  */
  unsigned char* buffer;

  /**Reserved for future use*/
  void* reserved;
};

typedef struct VrBio_BiometricImage VrBio_BiometricImage;
*)
  PBiometricImage = ^TBiometricImage;
  TBiometricImage = record
    width: Integer;                             { Image width. }
    height: Integer;                            { Image height }
    resolution: Integer;                        { Image resolution in dpi. For the obsolete functions, use pixels/cm.}
    channels: Integer;                          { Number of channels in the image. Fingerprint images should always be grayscale, so this value is always 1. }
    biometricModality: Integer;                 { Biometric modality. Always use VRBIO_BIOMETRIC_MODALITY_FINGERPRINT. \ref VrBio_BiometricModality. }
    scannerType: Integer;                       { Scanner type. \ref VrBio_ScannerType. }
    imageFormat: Integer;                       { Formato de imagem: Formato da imagem. \ ref VrBio_ImageFormat. }
    bufferSize: Integer;                        { Size of the buffer }
    compressionRate: Integer;                   { Compression rate. Valid for images that allow compression. \ref VrBio_CompressionRate }
    fingerQuality: Integer;                     { Quality of the fingerprint image. \ref VrBio_FingerQuality }
    ISO197944_parameters: PISO197944Parameters; { Only valid if the image if imageFormat is \ref VrBio_ImageFormat::VRBIO_IMAGEFORMAT_ISO19794_4 \ref VrBio_ISO197944Parameters }
    buffer: PByte;                              { Buffer storing the pixels of the image. The position(x,y,c) of a pixel is y*width*channels+x*channels+c. }
    reserved: Pointer;                          { Reserved for future use }
  end;

(*
#include "VTypes.h"

#ifdef WIN32
#define DLLIMPORT extern "C" __declspec(dllimport) int __stdcall
#else
#define DLLIMPORT extern "C"
#endif
*)

{ Callback function that receives events..
typedef void (*VrBio_CaptureEventCallback) (
                   int  eventType, 
            const char* readerName, 
  VrBio_BiometricImage* image, 
            const void* userData)
}
  TCaptureEventCallback = procedure(eventType: Integer; readerName: PAnsiChar; image: PBiometricImage; userData: Pointer); stdcall;

{ Function responsible for initializing the SDK. This function MUST be called before calling any other method, except \ref VInstallLicense
  DLLIMPORT  VStartSDK(VrBio_CaptureEventCallback eventCallback);
}
  function VStartSDK(eventCallback: TCaptureEventCallback): Integer; stdcall;

{ Function responsible for finalizing the SDK. This function should be called when the SDK is not needed in the application anymore.
  DLLIMPORT  VStopSDK();
}
  function VStopSDK(): Integer; stdcall;

{ Function responsible for starting the capture on a specific fingerprint reader.
  After calling this function, the application is able to receive events.
  DLLIMPORT  VStartReader(const char* readerName);
}
  function VStartReader(readerName: PAnsiChar): Integer; stdcall;

使用它看起来像这样:

implementation

{$R *.dfm}

procedure EventCallback(eventType: Integer; readerName: PAnsiChar; image: PBiometricImage; userData: Pointer); stdcall;
begin
  case eventType of
    VRBIO_CAPTURE_EVENT_UNPLUG:               Form1.Memo1.Lines.Add('Leitor desconectado!');
    VRBIO_CAPTURE_EVENT_REMOVED:              Form1.Memo1.Lines.Add('Dedo removido!');
    VRBIO_CAPTURE_EVENT_PLACED:               Form1.Memo1.Lines.Add('Dedo detectado!');
    VRBIO_CAPTURE_EVENT_IMAGE_FRAME:          Form1.Memo1.Lines.Add('Frame capturado!');
    VRBIO_CAPTURE_EVENT_IMAGE_CAPTURED:       Form1.Memo1.Lines.Add('Imagem capturada!');
    VRBIO_CAPTURE_EVENT_FAKE_FINGER_DETECTED: Form1.Memo1.Lines.Add('Dedo falso detectado!');
    VRBIO_CAPTURE_EVENT_FAKE_FINGER_REMOVED:  Form1.Memo1.Lines.Add('Dedo falso removido!');

    VRBIO_CAPTURE_EVENT_PLUG:
    begin
      VStartReader(readerName);
      Form1.Memo1.Lines.Add('Leitor conectado!');
    end;

  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  VStartSDK(EventCallback);
end;

我的问题:

我可以使用该应用程序并获取PlugUnplugPlaced事件,但是当我收到Image Captured事件时,我有访问权限。 在有效的事件中,EventCallback参数图像为nil。 TBiometricImage记录转换是否正确?

如何将TBiometricImage缓冲区转换为TBitmap并在TImage中显示捕获的图像?

1 个答案:

答案 0 :(得分:2)

  

当我收到Image Captured事件时,我有一个访问vilation。在正在运行的事件中,EventCallback参数图像为nil。 TBiometricImage记录转换是否正确?

单个字段被声明为正常,但请仔细检查Delphi记录的对齐和填充是否与C / C ++中结构使用的相同对齐和填充匹配。

另外,更重要的是,声明了C / C ++中的VrBio_CaptureEventCallback typedef而没有指定任何调用约定,因此它将使用编译器的默认约定,通常为__cdecl而不是__stdcall TCaptureEventCallback(可以在编译器设置中配置)。在Delphi中,您声明stdcall使用cdecl而不是stdcall。你必须确保正确匹配调用约定(导出的DLL函数确实使用buffer,所以你没关系。)

  

如何将TBiometricImage缓冲区转换为TBitmap并在TImage中显示捕获的图像?

SDK文档未说明如何处理各种图像格式。但是,只需查看结构声明,imageFormat字段指向实际图像数据,VrBio_ImageFormat字段指示该数据的格式(您有imageFormat枚举尚未翻译)。因此,您必须首先查看buffer以了解如何解释VConvertImage()

我确实看到了buffer功能。所以你应该能够将图像转换为BMP格式,如果它们还没有。根据SDK中的示例,看起来buffer数据可能是标准的BMP文件格式,因此您可以尝试将TMemoryStream复制到{{1}然后使用TBitmap.LoadFromStream()方法。

还有GIF和JPG图片格式,如果您想要显示扫描的GIF / JPG图像,可以分别由TGIFImageTJPEGImage处理,甚至可以TWICImage处理无需先将它们转换为BMP。还有一个RAW图像格式(显然你的图像正在使用),但没有标准的VCL TGraphic类来处理RAW图像,但我认为如果你环顾四周可能会有一些第三方类

否则,您可以尝试将图像数据转换为BMP(如果需要),然后将buffer传递给Win32 API CreateBitmap/Indirect()CreateDibSection()函数,然后分配结果{如果成功,则{1}}到HBITMAP属性。