2013-05-12 153 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