在mobile上呼叫SHCameraCapture介面進行拍照
本文所要講的是使用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;
- }