本文所要讲的是使用SHCameraCapture接口可以调出照相机进行拍照、摄像,并得到图片或视频文件的路径。
以下代码进行拍照:
- SHCAMERACAPTURE shcc;
- ZeroMemory(&shcc, sizeof(shcc));
- shcc.cbSize = sizeof(shcc);
- shcc.hwndOwner = NULL;
- shcc.pszInitialDir = NULL;
- shcc.pszDefaultFileName = NULL;
- shcc.pszTitle = NULL;
- shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_DEFAULT;
- shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_ALL;
- shcc.nResolutionWidth = 0;
- shcc.nResolutionHeight = 0;
- shcc.nVideoTimeLimit = 0;
- shcc.Mode = CAMERACAPTURE_MODE_STILL;
- HRESULT hReturn = SHCameraCapture(&shcc);
如果hReturn为S_OK,则表示拍照成功,shcc.szFile即为文件名称(包含路径)。shcc.pszInitialDir和shcc.pszDefaultFileName可以设置保存路径和默认文件名。shcc.hwndOwner如果使用了不同的窗体,可能会有问题。
如果要进行摄像,有些参数需进行调整,如下
shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_NORMAL;
shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_STANDARD;
shcc.nResolutionWidth = 640;
shcc.nResolutionHeight = 480;
shcc.Mode = CAMERACAPTURE_MODE_VIDEOWITHAUDIO;
这些参数涉及到几个枚举变量,我们来看看:
typedef enum {
CAMERACAPTURE_MODE_STILL = 0,
CAMERACAPTURE_MODE_VIDEOONLY,
CAMERACAPTURE_MODE_VIDEOWITHAUDIO,
} CAMERACAPTURE_MODE;
CAMERACAPTURE_MODE_STILL对应照片,CAMERACAPTURE_MODE_VIDEOONLY对应无声视频,CAMERACAPTURE_MODE_VIDEOWITHAUDIO对应有声视频。
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;
CAMERACAPTURE_VIDEOTYPE_ALL对应照片,CAMERACAPTURE_VIDEOTYPE_STANDARD对应WMV视频,CAMERACAPTURE_VIDEOTYPE_MESSAGING对应MMS视频。当使用后两个值时,shcc的nResolutionWidth和nResolutionHeight成员均不能为零。一般是640x480。
很遗憾,这个接口只能在WM5.0以上使用。WM2003上没有统一摄像头标准,硬件厂商各做各的,只能跟硬件厂商询问调用方法。
下面给出两个函数代码:
- 1.拍照
- LPCTSTR CphotoDlg::StartTakePic(void)
- {
- HRESULT hReturn;
- SHCAMERACAPTURE shcc;
- CString str = GetCurWorkingDir();
- // Specify the arguments of SHCAMERACAPTURE
- ZeroMemory( &shcc, sizeof( shcc ) );
- shcc.cbSize = sizeof( shcc );
- //shcc.hwndOwner = m_hWnd;
- shcc.hwndOwner = NULL;
- shcc.pszDefaultFileName = L"photo.jpg";
- shcc.pszTitle = NULL;
- shcc.pszInitialDir = str;
- shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_DEFAULT;
- shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_ALL;
- shcc.nResolutionWidth = 0;
- shcc.nResolutionHeight = 0;
- shcc.nVideoTimeLimit = 0;
- shcc.Mode = CAMERACAPTURE_MODE_STILL;
- //Call SHCameraCapture() function
- hReturn = SHCameraCapture( &shcc );
- //Check the return codes of the SHCameraCapture() function
- switch (hReturn)
- {
- case S_OK:
- AfxMessageBox( L"成功拍照" );
- return (LPCTSTR)shcc.szFile;
- case S_FALSE:
- //The user canceled the Camera Capture dialog box.
- AfxMessageBox( L"没有拍照" );
- break;
- case E_INVALIDARG:
- break;
- case E_OUTOFMEMORY:
- {
- AfxMessageBox( L"Out of Memory" );
- }
- break;
- default:
- break;
- }
- return NULL;
- }
- 2.摄像(默认保存的是mp4格式)
- LPCTSTR CphotoDlg::StartRecord(void)
- {
- HRESULT hReturn;
- SHCAMERACAPTURE shcc;
- CString str = GetCurWorkingDir();
- //Specify the arguments of SHCAMERACAPTURE
- ZeroMemory( &shcc, sizeof( shcc ) );
- shcc.cbSize = sizeof( shcc );
- shcc.hwndOwner = m_hWnd;
- shcc.pszInitialDir = str;
- shcc.pszDefaultFileName = TEXT( "video.3gp" );
- shcc.pszTitle = NULL;
- shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_HIGH;
- shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_STANDARD;
- shcc.nResolutionWidth = 640; //这两个参数如果设置非零,模拟器是调不出来的,但是真机上是没得问题的。
- shcc.nResolutionHeight = 480;
- shcc.nVideoTimeLimit = 0; //0表示无限制录像时间
- shcc.Mode = CAMERACAPTURE_MODE_VIDEOWITHAUDIO;
- //Call SHCameraCapture() function
- hReturn = SHCameraCapture( &shcc );
- //Check the return codes of the SHCameraCapture() function
- switch (hReturn)
- {
- case S_OK:
- AfxMessageBox( L"成功录像" );
- return (LPCTSTR)shcc.szFile;
- case S_FALSE:
- //The user canceled the Camera Capture dialog box.
- AfxMessageBox( L"没有录像" );
- break;
- case E_INVALIDARG:
- AfxMessageBox( L"E_INVALIDARG" );
- break;
- case E_OUTOFMEMORY:
- AfxMessageBox( L"Out of Memory" );
- break;
- default:
- break;
- }
- return NULL;
- }