Windows* Desktop Touch Camera Sample

by Raj Puran

Download Article/Source Code

Download Windows* Desktop Touch Camera Sample [PDF 271KB]
Download win32touchcam.zip [ZIP 26KB]

Abstract

This paper introduces the touch-enabled webcam sample application. The sample utilizes simple C++ programming ideas with a few principals that enable the application to use both Windows 7 and Windows 8 touch APIs to control the camera’s basic functions such as starting the camera, displaying the motion images on screen and then capturing an image. The application is a desktop-based application that is using Win32 and Video for Windows (VFW) libraries and protocols. The application shows you how to create a simple console application, format windows and buttons to accept touch inputs, and capture video and images from the application. The code shows you how a video or frame is loaded into memory and then dumped out to a file location within the application root. The sample will utilize the inbox camera and video drivers and will enumerate the first camera available to the device manager within Windows 7 or Windows 8. This is a very simple application intended to demonstrate how Win32* programming still applies to Windows 8.

Introducing the Touch-Enabled Camera Sample

Touch sensors and cameras are considered key components of tablets and mobile devices with Intel® Atom™ processors. Although using touch as an input method is vital to the tablet experience, the focus is not on programming touch interfaces but instead making a simple application formatted correctly to make use of the inbox touch drivers offered by Windows 8. This paper also focuses on using Windows APIs for enumerating an attached web camera device and calling it into action to view the ingested image and then taking a snapshot from that image.

Getting started

To start this sample app, open the solution. Build and run the sample. You’ll see a window containing just a console box that responds to touch events. There is an option to start the camera, which will look for the first USB or hardware-attached camera on the device. There is an option to stop the camera and take a snapshot. Note that images from the snapshot are located in the folder where the project was unzipped if you are using Microsoft Visual Studio*.

The Sample Code

Addition of the VFW32 header file

First, add a header file called vfw.h

  • This is the VFW header and is where the camera controls are.
  • Also link vfw32 Library in your project’s linker settings.

Go to Project -> properties.
Configuration Properties -> Linker -> Input -> Additional Dependencies
type in Vfw32.lib; and check to make sure gdi32.lib; is also present in the dependencies.

- Go to Project -> Properties-> Configuration Properties -> General

- Click on Character Set and change it to use Multi-Byte Character Set.

Procedures

The sample will use three procedure sets to execute the code. The first is WindowProc. The second is CreateBitmapInfoStruct which is a PBITMAPINFO structure that contains information on color and dimension in a windows-based DIB. The third and last procedure is the CreateBMPFile, which creates and saves the bitmap as a .bmp file.

Window Message Commands

Case 1
This connects and shows the camera image. WM_CAP_SET_SCALE is set to true to enable the camera picture to be resized to the dimensions of the camera window. WM_CAP_SET_PREVIEWRATE is set to 66 frames per second. To provide a reasonable moving picture without too much demand on resources, WM_CAP_SET_PREVIEW is set to true so a picture will be drawn on the camera window camhwnd.

Case 2
Calls WM_CAP_DRIVER_DISCONNECT to disconnect the camera.

Case 3
Case 3 grabs the frame and saves it to a .bmp file called "Photo.bmp."
WM_CAP_GRAB_FRAME does the frame grabbing by freezing the camera. WM_CAP_EDIT_COPY copies the grabbed frame to the clipboard. The clipboard image data is then copied to a HBITMAP object called “Pic.” We then create and save the bitmap file itself using Pic and the function call CreateBMPFile(hwnd, "Photo.bmp," pbi, hbm, hdcMem);

Case WM_DESTROY
This disconnects the camera if the user closes the window.

Procedures to create and save a Bitmap

First up is CreateBitmapInfoStruct, which creates a PBITMAPINFO structure. It takes two arguments HWND and an HBITMAP object.

A DIB consists of two distinct parts: a BITMAPINFO structure describing the dimensions and colors of the bitmap and an array of bytes defining the pixels of the bitmap. The bits in the array are packed together, but each scan line must be padded with zeroes to end on a LONG data-type boundary. If the height of the bitmap is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. If the height is negative, the bitmap is a top-down DIB and its origin is the upper left corner.

A bitmap is packed when the bitmap array immediately follows the BITMAPINFO header. Packed bitmaps are referenced by a single pointer. For packed bitmaps, the biClrUsed member must be set to an even number when using the BI_RGB mode. BI_RGB means that the bitmap is uncompressed.

Next, we create the file using CreateFile the CREATE_ALWAYS.
This parameter creates a new file, each time the code is executed. If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS.

Conclusion

This is a somewhat crude and simple way to build a console app that enumerates the camera device and then allows the user to invoke the camera, monitor the motion images displayed within the application frame, and capture a single frame snapshot. You can manipulate the code as much as you want to build in more features. In the next revision of the sample, the code will use Windows Media Foundation and incorporate a larger list of features.

References

Using Video Capture (Windows):
http://msdn.microsoft.com/en-us/library/windows/desktop/dd757677(v=vs.85).aspx

Sample Code for the application can be found here

About the Author

Raj Puran is an Application Software Engineer in Developer Relations at Intel. He helps ISVs develop and optimize applications that are compatible with Intel Atom processor-based platforms.

Intel, the Intel logo, and Atom are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2013 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.