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

Get value from the parameter of a callback function

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

看起来像这样:

const
{VrBio_EventType}
  VRBIO_CAPTURE_EVENT_UNPLUG               = [=10=]1; {Fingerprint scanner unplugged from the computer.}
  VRBIO_CAPTURE_EVENT_PLUG                 = [=10=]2; {Fingerprint scanner plugged on the computer.}
  VRBIO_CAPTURE_EVENT_REMOVED              = [=10=]4; {Finger removed from the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_PLACED               = [=10=]8; {Finger placed on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_IMAGE_FRAME          = ;  {A fingerprint frame was captured on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_IMAGE_CAPTURED       = 0; {A fingerprint image was captured on the fingerprint scanner.}
  VRBIO_CAPTURE_EVENT_FAKE_FINGER_DETECTED = 0; {A false finger has been detected on the sensor}
  VRBIO_CAPTURE_EVENT_FAKE_FINGER_REMOVED  = 0; {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 中显示捕获的图像?

when I get the Image Captured event I have an acces vilation. In the events that are working the EventCallback parameter image is nil. Is the TBiometricImage record conversion correct?

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

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

How can I convert the TBiometricImage buffer to a TBitmap and display the captured image in a TImage?

SDK 文档没有解释如何处理各种图像格式。但是,只看结构声明,buffer 字段指向实际图像数据,imageFormat 字段指示该数据的格式(有一个 VrBio_ImageFormat 枚举尚未翻译)。因此,您必须先查看 imageFormat 才能知道如何解释 buffer.

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

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

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