检测扫描图像所属的相机

Detecting the camera that scanned image belongs to

我正在使用 Leadtools 20 API 来控制扫描仪并扫描一些文档。我有 2 个问题。
1 - 当 L_TwainAcquire 与回调函数一起使用时,回调函数从扫描仪接收图像并不断返回 SUCCESS 以获取下一张图像。回调函数中是否有判断图像是来自前置摄像头还是来自后置摄像头?
2 - 有没有办法强制扫描仪仅使用后置摄像头进行扫描?

谢谢你
山姆

如果您指的是使用双面扫描仪时扫描文档的两个面,答案如下:

  1. 要在位图回调中知道当前正在处理纸张的哪一面,可以在回调函数中使用以下代码:
L_INT EXT_CALLBACK LTwainBitmapCallback(HTWAINSESSION hSession, pBITMAPHANDLE pBitmap, L_VOID * pUserData)
{
   L_INT nRet = SUCCESS;
   TW_EXTIMAGEINFO* pExtImg = NULL;
   int nExtraInfoAftreZero = 0;  // we have only one info, so no extra needed
   pExtImg = (TW_EXTIMAGEINFO *)malloc(sizeof TW_EXTIMAGEINFO  + nExtraInfoAftreZero * sizeof TW_INFO);
   if (pExtImg)  
   { 
      pExtImg->NumInfos = 1;
      pExtImg->Info[0].InfoID = TWEI_PAGESIDE;
      pExtImg->Info[0].ItemType = TWTY_UINT16;
      nRet = L_TwainGetExtendedImageInfo (hSession, pExtImg); 
      if (nRet == SUCCESS) 
      { 
         // Do processing to returned values
         if(pExtImg->Info[0].Item == TWCS_TOP)
            OutputDebugString("Front of sheet.\n");
         else if (pExtImg->Info[0].Item == TWCS_BOTTOM)
            OutputDebugString("Rear of sheet.\n");
         else
            OutputDebugString("Unexpected!\n");
         nRet = L_TwainFreeExtendedImageInfoStructure (&pExtImg); 
         if(nRet != SUCCESS) 
            return nRet; 
      }
      free(pExtImg);
   }
   return SUCCESS;
}
  1. 可以使用 CAP_CAMERASIDE 和 CAP_CAMERAENABLED 功能的组合来强制扫描特定面,如 Twain Spec 中所述。

引用该文档:

To enable bottom only scanning, set CAP_CAMERASIDE to TWCS_BOTTOM and set CAP_CAMERAENABLED to TRUE, then set CAP_CAMERASIDE to TWCS_TOP and set CAP_CAMERAENABLED to FALSE.

如果你的意思是你的电脑有 2 个 Twain 相机源(前后),答案如下:

  1. 要知道调用 L_TwainAcquire() 时哪个 Twain 源(例如相机)触发了 Twain 位图回调,您可以使用 L_TwainGetSources() 函数与 LTWAIN_SOURCE_ENUMERATE_DEFAULT 标志一起获取当前选择的 Twain 设备。这可以在 inside Bitmap Callback 中完成,如以下代码所示:
// Source Info Callback
L_INT EXT_CALLBACK TwainSourceInfoCallbackCurrent(HTWAINSESSION hSession, pLTWAINSOURCEINFO pSourceInfo, L_VOID * pUserData)
{
   strcpy((L_CHAR *)pUserData, pSourceInfo->pszTwnSourceName);
   return SUCCESS;
}

// Twain Bitmap Callback
L_INT EXT_CALLBACK LTwainBitmapCallback(HTWAINSESSION hSession, pBITMAPHANDLE pBitmap, L_VOID * pUserData)
{
   char szSourceName[1024];
   L_TwainGetSources(hSession, TwainSourceInfoCallbackCurrent, sizeof LTWAINSOURCEINFO, LTWAIN_SOURCE_ENUMERATE_DEFAULT, szSourceName);
   // Now szSourceName contains name of Twain Source.
   return SUCCESS;
}
  1. 是的,您可以通过在扫描前将其名称发送到 L_TwainSelectSource() 函数来强制从特定设备(扫描仪、相机等)进行扫描,如下所示:
LTWAINSOURCE TwainSource;
TwainSource.uStructSize = sizeof LTWAINSOURCE;
TwainSource.pszTwainSourceName = pszRearCameraTwainSourceName;
L_TwainSelectSource(twainSession, &TwainSource);
// Now L_TwainAcquire() will capture from Rear Camera

要查找所有设备的名称,您可以使用此代码:

L_TwainGetSources(twainSession, TwainSourceInfoCallback, sizeof LTWAINSOURCEINFO, LTWAIN_SOURCE_ENUMERATE_ALL, NULL);

源信息回调将为每个 Twain 源触发一次。可以这样实现:

L_INT EXT_CALLBACK TwainSourceInfoCallback(HTWAINSESSION hSession, pLTWAINSOURCEINFO pSourceInfo, L_VOID * pUserData)
{
   OutputDebugString(pSourceInfo->pszTwnSourceName); // You can save the names of Twain Sources into global variables if you like
   OutputDebugString("\n");
   return SUCCESS;
}