free web page counters

Windows Mobile Pocket PC Smartphone Programming

==>Click here for the SiteMap<==. Original contents with decent amount of source codes.

Sunday, April 16, 2006

How to use the new Camera API in Windows Mobile 5.0 SDK with only Embedded Visual C++ 4.0 (evc4)?

====>SiteMap of this Blog<===

How to use the new Camera API in Windows Mobile 5.0 SDK with only Embedded Visual C++ 4.0 (evc4)?

Almost all up-to-date phones, PDAs, or handheld gadgets have an embedded camera, allow users to snap pictures or capture videos. Microsoft releases new APIs in Windows Mobile 5.0 SDK to help developers controlling the camera.

Camera Capture API
Camera enabled devices are extremely popular. With these devices being so widely available, application developers now have the opportunity to substantially enhance the user experience by integrating the capture of both still and video images directly into their applications. To enable application developers to easily provide this functionality, Windows Mobile 5.0 provides the camera capture dialog. The unmanaged API is called SHCameraCapture.

The problem is that the only Visual Studio 2005 can work with Windows Mobile 5.0 SDK. What if you are still using the free Embedded Visual C++ 4 (EVC4), and you do not want to shed a cool $800 to get a copy of VS2005?

The solution is to use LoadLibrary to load aygshell.dll, get the entry point to SHCameraCapture, then call that function by jumping to the entry point. The API anticipates a structure as the parameter, so you need to instruct EVC4 compiler to generate the parameter correctly by defining it yourself. Luckily all the members in the structure are primitive types or character arrays, so there is really not much hassle.

Below is the source code that you can copy and paste into your EVC4 source code. It uses #ifdef _WIN32_WCE to make sure the code gets compiled for Pocket PC 2003 project or Smartphone 2003 project. The final executable can then be copied to a Pocket PC 5.0 device or Smartphone 5.0 device.

#include <aygshell.h>
#if _WIN32_WCE < 0x0500

//////////////////////////////////////////////////////////////////////////////
//
// Flags for camera capture UI

typedef enum {
   CAMERACAPTURE_MODE_STILL = 0,
   CAMERACAPTURE_MODE_VIDEOONLY,
   CAMERACAPTURE_MODE_VIDEOWITHAUDIO,
} CAMERACAPTURE_MODE;

typedef enum {
   CAMERACAPTURE_STILLQUALITY_DEFAULT = 0,
   CAMERACAPTURE_STILLQUALITY_LOW,
   CAMERACAPTURE_STILLQUALITY_NORMAL,
   CAMERACAPTURE_STILLQUALITY_HIGH,
} CAMERACAPTURE_STILLQUALITY;

typedef enum {
   CAMERACAPTURE_VIDEOTYPE_ALL = 0xFFFF,
   CAMERACAPTURE_VIDEOTYPE_STANDARD = 1,
   CAMERACAPTURE_VIDEOTYPE_MESSAGING = 2,
} CAMERACAPTURE_VIDEOTYPES;

typedef struct tagSHCAMERACAPTURE
{
   DWORD   cbSize;
   HWND   hwndOwner;
   TCHAR   szFile[MAX_PATH];   
   LPCTSTR   pszInitialDir;
   LPCTSTR   pszDefaultFileName;
   LPCTSTR   pszTitle;
   CAMERACAPTURE_STILLQUALITY   StillQuality;
   CAMERACAPTURE_VIDEOTYPES   VideoTypes;
   DWORD   nResolutionWidth;
   DWORD   nResolutionHeight;
   DWORD   nVideoTimeLimit;
   CAMERACAPTURE_MODE   Mode;
} SHCAMERACAPTURE, *PSHCAMERACAPTURE;


HRESULT SHCameraCapture(PSHCAMERACAPTURE pshcc);

typedef HRESULT (*fnSHCameraCapture)(PSHCAMERACAPTURE pshcc);

HRESULT SHCameraCapture(PSHCAMERACAPTURE pshcc)
{
   HRESULT hr = S_OK;
   HINSTANCE hAygShell = LoadLibrary(TEXT("aygshell.dll"));
   fnSHCameraCapture funcSHCameraCapture = NULL;

   if (!hAygShell) {
   hr = HRESULT_FROM_WIN32(GetLastError());
   goto FuncExit;
   }
   funcSHCameraCapture = (fnSHCameraCapture)GetProcAddress(hAygShell, TEXT("SHCameraCapture"));
   if (!funcSHCameraCapture) {
   hr = HRESULT_FROM_WIN32(GetLastError());
   goto FuncExit;
   }

   // call the API now
   hr = funcSHCameraCapture(pshcc);

FuncExit:
   if (hAygShell) {
   FreeLibrary(hAygShell);
   }
   return hr;
}

#endif   // faked one in 4.20/4.21 platform

Wrap Up

You may use similar approach to access other new 5.0 APIs from within EVC4. You need to find out which DLL exports the API, and more importantly, you need to let EVC4 compiler correctly generate parameters to feed into those APIs, so inevitably you have to copy some header definitions from Windows Mobile 5.0 SDKs.

====>SiteMap of this Blog<===