2013-05-12 231 views
0

我正在使用Windows 8的媒體捕捉類來單擊桌面應用程序中的照片並將其複製到剪貼板。慢速攝像頭捕捉

My功能採用兩個輸入作爲參數, 1)所需的設備(前,後或USB網絡攝像)和 2)所需的分辨率

這裏是功能:

async public void UseCamera(int x, int y) 
    { 
     MediaCapture _mediaCapture = new MediaCapture(); 
     var _ImageFormat = ImageEncodingProperties.CreatePng(); 
     var _fileStream = new InMemoryRandomAccessStream(); 
     MediaCaptureInitializationSettings _cameraSettings1 = new MediaCaptureInitializationSettings(); 
     DeviceInformationCollection _deviceInformationCollection = null; 
     IReadOnlyList<IMediaEncodingProperties> res; 

     _deviceInformationCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 

     if (x > _deviceInformationCollection.Count - 1) 
     { 
      MessageBox.Show("Device Not found"); 
     } 

     else 
     { 
      _cameraSettings1.VideoDeviceId = _deviceInformationCollection[x].Id; 
      _cameraSettings1.AudioDeviceId = ""; 
      _cameraSettings1.PhotoCaptureSource = PhotoCaptureSource.VideoPreview; 
      _cameraSettings1.StreamingCaptureMode = StreamingCaptureMode.Video; 

      await _mediaCapture.InitializeAsync(_cameraSettings1); 

      res = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); 

      uint maxResolution = 0; 
      List<int> indexMaxResolution = new List<int>(); 
      if (res.Count >= 1) 
      { 
       for (int i = 0; i < res.Count; i++) 
       { 
        VideoEncodingProperties vp = (VideoEncodingProperties)res[i]; 

        if (vp.Width > maxResolution) 
        { 
         indexMaxResolution.Add(i); 
         maxResolution = vp.Width; 
        } 
       } 

       indexMaxResolution.Reverse(); 
       if (y > indexMaxResolution.Count()) 
       { 
        MessageBox.Show("Maximum supported resolution index : " + (indexMaxResolution.Count - 1).ToString()); 
       } 

       //this is the part that I believe is the trouble maker 


       else 
       { 
        await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, res[indexMaxResolution.ElementAt(y)]); 
        await _mediaCapture.CapturePhotoToStreamAsync(_ImageFormat, _fileStream); 

        Clipboard.SetImage(Image.FromStream(_fileStream.AsStream())); 
       } 
      } 
     } 
    } 

該功能正在工作,但問題是,它非常慢。拍攝照片需要將近4-5秒。任何人都可以告訴我我哪裏錯了,我怎麼能加快速度。因爲我測試了我的相機,並且每秒可以拍攝幾乎2張圖片。

回答

0

如果將所有初始化和設備信息查詢移動到初始化函數,您可能會看到速度提高。

根據我的經驗收集設備信息很慢。

嘗試儘可能多地做前期準備,以便在需要捕捉時,只需完成必要的事情。

+0

我也試過。我初始化所有三個攝像頭並在程序啓動前列出他們的分辨率。相機初始化和分辨率索引製作速度非常快,我相信這是實際拍攝速度下降的原因。 – 2013-05-12 17:45:16

+0

您也可以嘗試將圖像保存在內存中,並只在需要時才寫入。剪貼板可能也很慢。據我所知,這是唯一可以改變的東西。也許增加一些定時器可能會幫助您確定最慢的部分。 – 2013-05-12 17:50:32