InteractiveHand.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "InteractiveHand.h"
  2. InteractiveHand::InteractiveHand() : QQuickImageProvider(QQuickImageProvider::Image)
  3. {
  4. initHandle();
  5. m_switch = 0;
  6. // 图像取1s 1帧
  7. m_timerId = startTimer(1000);
  8. }
  9. InteractiveHand::~InteractiveHand()
  10. {
  11. destroyHandle();
  12. }
  13. void InteractiveHand::setFingerImageAppearCallBack(QJSValue callback)
  14. {
  15. m_imageAppearCallback = callback;
  16. }
  17. void InteractiveHand::fingerImagePlay()
  18. {
  19. m_switch = 1;
  20. }
  21. void InteractiveHand::fingerImageStop()
  22. {
  23. m_switch = 0;
  24. }
  25. void InteractiveHand::initHandle()
  26. {
  27. m_templateSize = MAX_TEMPLATE_SIZE;
  28. m_templateData.resize(MAX_TEMPLATE_SIZE);
  29. int zkfpErr = ZKFPM_Init(); // 调用DLL中的函数
  30. if(zkfpErr == ZKFP_ERR_OK || zkfpErr == ZKFP_ERR_ALREADY_INIT)
  31. {
  32. int count = ZKFPM_GetDeviceCount();
  33. if(count > 0)
  34. {
  35. m_handle = ZKFPM_OpenDevice(0);
  36. if(m_handle != NULL)
  37. {
  38. unsigned int uArgs = 1;
  39. unsigned int uSize = 4;
  40. ZKFPM_SetParameters(m_handle, 2002, (unsigned char *)&uArgs, 4);
  41. ZKFPM_SetParameters(m_handle, 104, (unsigned char *)&uArgs, 4);
  42. // 获取宽高
  43. ZKFPM_GetParameters(m_handle, 1, (unsigned char *)&m_imageW, &uSize);
  44. ZKFPM_GetParameters(m_handle, 2, (unsigned char *)&m_imageH, &uSize);
  45. // ZKFPM_GetParameters(m_handle, 3, (unsigned char *)&m_impDpi, &uSize);
  46. m_imageData.resize(m_imageW * m_imageH);
  47. m_isInit = true;
  48. }
  49. }
  50. }
  51. }
  52. void InteractiveHand::destroyHandle()
  53. {
  54. if(m_handle != NULL)
  55. {
  56. ZKFPM_CloseDevice(m_handle);
  57. m_isInit = false;
  58. }
  59. ZKFPM_Terminate();
  60. }
  61. QString InteractiveHand::getImageUrl()
  62. {
  63. // image://InteractiveHandImage/(0-9999)
  64. return QString("image://") +
  65. QString(INTERACTIVE_HAND_IMAGE_URL) +
  66. QString("/") +
  67. // 取随机数,确保图片刷新(Qt机制,两次url地址相同,图片不会显示)
  68. QString::number(QRandomGenerator::global()->bounded(10000));
  69. }
  70. QImage InteractiveHand::requestImage(const QString &, QSize *, const QSize &)
  71. {
  72. return m_image;
  73. }
  74. void InteractiveHand::timerEvent(QTimerEvent *event)
  75. {
  76. if (event->timerId() == m_timerId)
  77. {
  78. if (m_switch && m_isInit)
  79. {
  80. unsigned int templateSize = m_templateSize;
  81. int zkfpErr = ZKFPM_AcquireFingerprint(m_handle,
  82. reinterpret_cast<unsigned char *>(m_imageData.data()),
  83. static_cast<unsigned int>(m_imageData.size()),
  84. reinterpret_cast<unsigned char *>(m_templateData.data()),
  85. &templateSize);
  86. if(zkfpErr == ZKFP_ERR_OK)
  87. {
  88. QImage::Format format = QImage::Format_Grayscale8;
  89. // 将字节流转换为 QImage
  90. m_image = QImage(reinterpret_cast<const uchar*>(m_imageData.constData()),
  91. m_imageW,
  92. m_imageH,
  93. QImage::Format_Grayscale8);
  94. if (m_imageAppearCallback.isCallable())
  95. {
  96. QJSValueList args;
  97. args << getImageUrl();
  98. m_imageAppearCallback.call(args);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. // 获取图像
  105. QImage InteractiveHand::getImage()
  106. {
  107. return m_image.copy();
  108. }