代码之家  ›  专栏  ›  技术社区  ›  Shubham Shrivastava

如何使用MediaCapture类打开和自动捕获摄像头

  •  1
  • Shubham Shrivastava  · 技术社区  · 7 年前

    我们正在尝试使用MediaCapture类从网络摄像头自动捕获图像。我们正在尝试创建一个应用程序,该应用程序打开相机,等待片刻,然后在没有人点击屏幕进行捕捉的情况下捕捉相机前面的图像。我们尝试使用LowLagPhotoCapture类,但效果并不理想。示例代码-

    async private void InitMediaCapture()
    {
        MediaCapture _mediaCapture = new MediaCapture();
            await _mediaCapture.InitializeAsync();
            _displayRequest.RequestActive();                        
            PreviewControlCheckIn.Source = _mediaCapture;
            await _mediaCapture.StartPreviewAsync();
            await Task.delay(500);
        CaptureImage();
    }
    async private void CaptureImage()
    {
        storeFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync   ("TestPhoto.jpg",CreationCollisionOption.GenerateUniqueName);
            ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
            await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, storeFile);
        await _mediaCapture.StopPreviewAsync();
    }
    

    1 回复  |  直到 7 年前
        1
  •  4
  •   Nico Zhu    7 年前

    我已经完成了您提供的代码并达到了您的要求。请参考以下代码。请注意,您应该在通用Windows平台(UWP)应用程序的软件包清单中声明摄像头和麦克风功能,以访问某些API。

    async private void InitMediaCapture()
    {
        _mediaCapture = new MediaCapture();
        var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
        var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
        await _mediaCapture.InitializeAsync(settings);
        _displayRequest.RequestActive();
        PreviewControl.Source = _mediaCapture;
        await _mediaCapture.StartPreviewAsync();
    
        var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
        _captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder;
    
        await Task.Delay(500);
        CaptureImage();
    }
    
     async private void CaptureImage()
     {
         var storeFile = await _captureFolder.CreateFileAsync("PreviewFrame.jpg", CreationCollisionOption.GenerateUniqueName);
         ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
         await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, storeFile);
         await _mediaCapture.StopPreviewAsync();
     }
     private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
     {
         // Get available devices for capturing pictures
         var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
         // Get the desired camera by panel
         DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredPanel);
    
         // If there is no device mounted on the desired panel, return the first device found
         return desiredDevice ?? allVideoDevices.FirstOrDefault();
     }
    

    Pictures 图书馆我已经上传了 code sample 到github。请检查!