✎ 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.
Create a new directory in the \Examples\CDLL directory called WiaTutorial.
Copy everything in the SimpleLoad directory into the WiaTutorial directory.
Compile the project as it is and run SimpleLoad.exe to familiarize yourself with the basic program.
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)
Add the following line in StdAfx.h in the WiaTutorial directory:
#include "..\..\..\include\ltwia.h"
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
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
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
In the InitApplication function change the line:
wcWindowClass.lpszMenuName = NULL; /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");
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;
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;
In the WM_DESTROY message handler add the following code at the end of it (before the PostQuitMessage(0);
):
L_WiaEndSession(hSession);
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;
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;
In the IDM_ACQUIRE_IMAGE_FROM_VIDEO menu handler add the following code:
case IDM_ACQUIRE_IMAGE_FROM_VIDEO:
{
AcquireStillImageFromVideo(hWnd);
}
break;
In the IDM_STOP_VIDEO_PREVIEW menu handler add the following code:
case IDM_STOP_VIDEO_PREVIEW:
{
L_WiaEndVideoPreview(hSession);
}
break;
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;
}
Compile and test the program.