慢速相机捕捉
本文关键字:相机 | 更新日期: 2023-09-27 18:13:23
我正在使用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张照片…
如果你将所有的初始化和设备信息查询移动到一个初始化函数中,你可能会看到速度的提高。
根据我的经验,收集设备信息很慢。
尽量提前完成,以便在捕获时间到来时,只需要完成必要的事情。