在mobile上调用SHCameraCapture接口进行拍照

在mobile上调用SHCameraCapture接口进行拍照 

<script type="text/javascript"> </script> <script class="blogstory"> </script>

 本文所要讲的是使用SHCameraCapture接口可以调出照相机进行拍照、摄像,并得到图片或视频文件的路径。

以下代码进行拍照:

  1. SHCAMERACAPTURE shcc;
  2. ZeroMemory(&shcc,  sizeof (shcc));
  3. shcc.cbSize             =  sizeof (shcc);
  4. shcc.hwndOwner          = NULL;
  5. shcc.pszInitialDir      = NULL;
  6. shcc.pszDefaultFileName = NULL;
  7. shcc.pszTitle           = NULL;
  8. shcc.StillQuality       = CAMERACAPTURE_STILLQUALITY_DEFAULT;
  9. shcc.VideoTypes         = CAMERACAPTURE_VIDEOTYPE_ALL;
  10. shcc.nResolutionWidth   = 0;
  11. shcc.nResolutionHeight  = 0;
  12. shcc.nVideoTimeLimit    = 0;
  13. shcc.Mode               = CAMERACAPTURE_MODE_STILL;
  14. 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. 1.拍照
  2. LPCTSTR  CphotoDlg::StartTakePic( void )
  3. {
  4.      HRESULT  hReturn;   
  5.     SHCAMERACAPTURE shcc;   
  6.     CString str = GetCurWorkingDir();
  7.      //   Specify   the   arguments   of   SHCAMERACAPTURE   
  8.     ZeroMemory( &shcc,  sizeof ( shcc ) );   
  9.     shcc.cbSize =  sizeof ( shcc );   
  10.      //shcc.hwndOwner = m_hWnd;  
  11.     shcc.hwndOwner = NULL;
  12.     shcc.pszDefaultFileName = L "photo.jpg" ;
  13.     shcc.pszTitle = NULL;
  14.     shcc.pszInitialDir = str;
  15.     shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_DEFAULT;   
  16.     shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_ALL;   
  17.     shcc.nResolutionWidth   = 0;
  18.     shcc.nResolutionHeight  = 0;
  19.     shcc.nVideoTimeLimit    = 0;
  20.     shcc.Mode = CAMERACAPTURE_MODE_STILL;     
  21.      //Call   SHCameraCapture()   function   
  22.     hReturn = SHCameraCapture( &shcc );   
  23.      //Check   the   return   codes   of   the   SHCameraCapture()   function   
  24.      switch  (hReturn)   
  25.     {   
  26.      case  S_OK:   
  27.         AfxMessageBox( L "成功拍照"  );
  28.          return   ( LPCTSTR )shcc.szFile;   
  29.      case  S_FALSE:   
  30.          //The   user   canceled   the   Camera   Capture   dialog   box.   
  31.         AfxMessageBox( L "没有拍照"  );
  32.          break ;   
  33.      case  E_INVALIDARG:   
  34.          break ;   
  35.      case  E_OUTOFMEMORY:   
  36.         {
  37.             AfxMessageBox( L "Out of Memory"  );
  38.         }   
  39.          break ;   
  40.      default :   
  41.          break ;   
  42.     }   
  43.      return    NULL;
  44. }
  45. 2.摄像(默认保存的是mp4格式)
  46. LPCTSTR  CphotoDlg::StartRecord( void )
  47. {
  48.      HRESULT  hReturn;   
  49.     SHCAMERACAPTURE shcc;   
  50.     CString str = GetCurWorkingDir();
  51.      //Specify   the   arguments   of   SHCAMERACAPTURE   
  52.     ZeroMemory( &shcc,  sizeof ( shcc ) );   
  53.     shcc.cbSize =  sizeof ( shcc );   
  54.     shcc.hwndOwner = m_hWnd;  
  55.     shcc.pszInitialDir = str;
  56.     shcc.pszDefaultFileName = TEXT(  "video.3gp"  );
  57.     shcc.pszTitle = NULL;
  58.     
  59.     shcc.StillQuality = CAMERACAPTURE_STILLQUALITY_HIGH;   
  60.     shcc.VideoTypes = CAMERACAPTURE_VIDEOTYPE_STANDARD;   
  61.     shcc.nResolutionWidth   = 640;     //这两个参数如果设置非零,模拟器是调不出来的,但是真机上是没得问题的。
  62.     shcc.nResolutionHeight  = 480;
  63.     shcc.nVideoTimeLimit = 0;    //0表示无限制录像时间
  64.     shcc.Mode = CAMERACAPTURE_MODE_VIDEOWITHAUDIO;     
  65.      //Call   SHCameraCapture()   function   
  66.     hReturn = SHCameraCapture( &shcc );   
  67.      //Check   the   return   codes   of   the   SHCameraCapture()   function   
  68.      switch  (hReturn)   
  69.     {   
  70.      case  S_OK: 
  71.         AfxMessageBox( L "成功录像"  );
  72.          return   ( LPCTSTR )shcc.szFile;   
  73.      case  S_FALSE:   
  74.          //The   user   canceled   the   Camera   Capture   dialog   box.  
  75.         AfxMessageBox( L "没有录像"  );
  76.          break ;   
  77.      case  E_INVALIDARG: 
  78.         AfxMessageBox( L "E_INVALIDARG"  );
  79.          break ;   
  80.      case  E_OUTOFMEMORY:   
  81.         AfxMessageBox( L "Out of Memory"  ); 
  82.          break ;   
  83.      default :   
  84.          break ;   
  85.     }   
  86.      return    NULL;
  87. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值