Acquire A Still Image from Video Stream

Note: The purpose of this WIA tutorial is to provide you a quick and easy way to acquire a still image from a video stream. For more in-depth WIA programming, refer to the WIA demo.

To acquire a still image from video stream device:

1.

Create a new directory in the \Examples\CDLL directory called WiaTutorial.

2.

Copy everything in the SimpleLoad directory into the WiaTutorial directory.

3.

Compile the project as it is and run SimpleLoad.exe to familiarize yourself with the basic program.

4.

In the Imports.cpp, add the following lines:

 

#if defined(WIN64) 
   …
   …
      #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltwia_x.lib")
#else
   …
   …
   #pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltwia_u.lib")
#endif // #if defined(WIN64) 

5.

Add the following line in StdAfx.h in the WiaTutorial directory:

 
#include "..\..\..\include\ltwia.h"

6.

While you are inside the StdAfx.h add the following lines before any #include statement, since the some defines like WIA_DPV_IMAGES_DIRECTORY requires that the _WIN32_WINNT version to be equal or more than 0x0501 in order for you demo to compile:

#if _WIN32_WINNT < 0x0501
  #define _WIN32_WINNT = 0x0501
#endif

 

7.

Define the following global variable in Ezfunc.h in the WiaTutorial directory:

HWIASESSION hSession = NULL; 
Also, add the following defines: 
   #define IDM_SELECT_VIDEO_DEVICE        200
#define IDM_START_VIDEO_PREVIEW        201
#define IDM_ACQUIRE_IMAGE_FROM_VIDEO   202
#define IDM_STOP_VIDEO_PREVIEW         203

8.

Edit EZFUNC.RC file in the WiaTutorial directory and add the following lines:

#include "EZFUNC.H"
MAIN_MENU   MENU
BEGIN
MENUITEM "Select Video Device"         IDM_SELECT_VIDEO_DEVICE
MENUITEM "Start Video Preview"         IDM_START_VIDEO_PREVIEW
MENUITEM "Acquire Image From Video"    IDM_ACQUIRE_IMAGE_FROM_VIDEO
MENUITEM "Stop Video Preview"          IDM_STOP_VIDEO_PREVIEW
END

9.

In the InitApplication function change the line:

wcWindowClass.lpszMenuName = NULL;  /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");
 

10.

In the MainWndProc function and before the handling of the WM_PALETTECHANGED message add the following code:

 

   case WM_COMMAND: 
      {
         switch (LOWORD(wParam)) 
         {
case IDM_SELECT_VIDEO_DEVICE: 
            {
            }
            break;
         case IDM_START_VIDEO_PREVIEW: 
            {
            }
            break; 
case IDM_ACQUIRE_IMAGE_FROM_VIDEO: 
{
}
break; 
case IDM_STOP_VIDEO_PREVIEW: 
{
}
break; 
}
}
break; 

11.

In the WM_CREATE message handler add the following code at the end of it (after SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);):

 

nRet = L_WiaInitSession(WiaVersion1, &hSession); 
if (nRet != WIA_SUCCESS) 
   return FALSE; 

12.

In the WM_DESTROY message handler add the following code at the end of it (before the PostQuitMessage (0);):

 

L_WiaEndSession(hSession); 

13.

In the IDM_SELECT_VIDEO_DEVICE menu handler add the following code:

case IDM_SELECT_VIDEO_DEVICE:
   {
nRet = L_WiaSelectDeviceDlg(hSession, hWnd, WiaDeviceTypeStreamingVideo, L_WIA_SELECT_DEVICE_NODEFAULT); 
if (nRet != WIA_SUCCESS) 
   return FALSE; 
}
break; 

14.

In the IDM_START_VIDEO_PREVIEW menu handler add the following code:

case IDM_START_VIDEO_PREVIEW:
   {
if(LeadBitmap.Flags.Allocated) 
L_FreeBitmap(&LeadBitmap); 

InvalidateRect (hWnd, NULL, TRUE); 

         nRet = L_WiaStartVideoPreview(hSession, hWnd, FALSE); 
         if (nRet != WIA_SUCCESS) 
return nRet; 
}
break; 

15.

In the IDM_ACQUIRE_IMAGE_FROM_VIDEO menu handler add the following code:

case IDM_ACQUIRE_IMAGE_FROM_VIDEO:
   {
AcquireStillImageFromVideo(hWnd); 
}
break; 

16.

In the IDM_STOP_VIDEO_PREVIEW menu handler add the following code:

 

case IDM_STOP_VIDEO_PREVIEW:
   {
L_WiaEndVideoPreview(hSession); 
}
break; 

17.

In Ezfunc.cpp file, add the following function before MainWndProc function:

 

L_INT CALLBACK AcquireStillImageFromVideo(HWND hWnd)
{
   L_TCHAR szSavedFileName[MAX_PATH] = TEXT("");
   L_TCHAR szImagesDirectory[MAX_PATH] = TEXT("");
   L_TCHAR szMsg[MAX_PATH] = TEXT("");
   L_BOOL bAvailable = FALSE; 
   L_SIZE_T uLength = MAX_PATH; 
   IWiaItem * pRootItem = NULL; 
   L_INT nRet = WIA_SUCCESS; 

   bAvailable = L_WiaIsVideoPreviewAvailable(hSession); 
   if(!bAvailable) 
   {
     MessageBox(hWnd,
         TEXT("No video preview available."),
         TEXT("ERROR"),
         MB_OK | MB_ICONERROR);

      return FALSE; 
   }

   nRet = L_WiaAcquireImageFromVideo(hSession, szSavedFileName, &uLength); 
   if(nRet != WIA_SUCCESS && nRet == ERROR_BUFFER_TOO_SMALL) 
   {
      nRet = L_WiaGetRootItem(hSession, NULL, (L_VOID**)&pRootItem); 
      if(nRet != WIA_SUCCESS) 
         return FALSE; 

      nRet = L_WiaGetPropertyString(hSession, pRootItem, NULL, WIA_DPV_IMAGES_DIRECTORY, szImagesDirectory, &uLength); 
      if(nRet == WIA_SUCCESS) 
      {
         wsprintf(szMsg, 
            TEXT("Provided string buffer is too small to be updated.\n")
            TEXT("But still an image was saved to the following directory:\n\n%s"),
            szImagesDirectory); 

         MessageBox(hWnd, szMsg, TEXT("ERROR"), MB_OK | MB_ICONERROR); 
      }

      return FALSE; 
   }

   wsprintf(szMsg, 
      TEXT("Captured still image was saved to the following path:\n\n%s"),
      szSavedFileName); 

   MessageBox(hWnd, szMsg, TEXT("Captured Image Path"), MB_OK | MB_ICONINFORMATION); 

   return WIA_SUCCESS; 
}
 

18.

Compile and test the program.