慢相机捕捉

时间:2013-05-12 17:11:50

标签: c# .net windows-8 camera desktop-application

我正在使用Windows 8的媒体捕获类来点击桌面应用程序中的照片并将其复制到剪贴板。

我的函数接受两个输入作为参数, 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个图片..

1 个答案:

答案 0 :(得分:0)

如果将所有初始化和设备信息查询移动到初始化函数,您可能会看到速度增加。

根据我的经验,收集设备信息的速度很慢。

尝试尽可能提前做好,以便在捕获时,只需要完成必要的事情。