c# - Windows Phone 8.1 相机初始化

标签 c# visual-studio camera windows-phone-8.1

我知道有更多关于此的重复问题,但拜托,这对我来说非常重要。我现在很难使用 Windows Phone 8.1 C# 相机初始化。

async private void InitCamera_Click(object sender, RoutedEventArgs e)
    {
        captureManager = new MediaCapture();
        await captureManager.InitializeAsync();

         try 
        {
            captureManager = new Windows.Media.Capture.MediaCapture();
            await captureManager.InitializeAsync();

            if (captureManager.MediaCaptureSettings.VideoDeviceId != "" && captureManager.MediaCaptureSettings.AudioDeviceId != "") 
            {
                System.Diagnostics.Debug.WriteLine("Init successful");


                captureManager.RecordLimitationExceeded += new Windows.Media.Capture.RecordLimitationExceededEventHandler(RecordLimitationExceeded);
                captureManager.Failed += new Windows.Media.Capture.MediaCaptureFailedEventHandler(Failed); 
            } 
            else 
            {
                System.Diagnostics.Debug.WriteLine("No Device");
            } 
        }
         catch (Exception exception)
         {
             System.Diagnostics.Debug.WriteLine("Exception raised!!!!:" + exception);
         } 
    }

这是我初始化相机的代码,但由于某种原因,它在 Lumia 920 上 Windows.Media.Capture.MediaCapture() 构造函数调用失败,System.UnauthorizedAccessException以及模拟器上的访问冲突。我已经用谷歌搜索了这个问题,但到目前为止还没有答案。有些人告诉我,我不仅应该启用网络摄像头,还应该启用麦克风,但这并没有解决我的问题。一切似乎都已设置好,所有访问权限均已在应用程序 list 中授予。另外我想问你,如果你有一些很好的用相机拍照的例子/教程,请提供。

最佳答案

下面是我的相机捕捉代码,它有效,我在商店中提交了一个应用程序:

private MediaCapture mediaCapture = null;
private async Task StartCapture()
{
  string error = null;

  try
  {
    if (mediaCapture == null)
    {
      mediaCapture = new MediaCapture();
      mediaCapture.Failed += mediaCapture_Failed;

      var _deviceInformation = await GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel.Back);

      var settings = new MediaCaptureInitializationSettings();
      settings.StreamingCaptureMode = StreamingCaptureMode.Video;
      settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
      settings.AudioDeviceId = "";
      if (_deviceInformation != null)
          settings.VideoDeviceId = _deviceInformation.Id;

      await mediaCapture.InitializeAsync(settings);

      var focusSettings = new FocusSettings();
      focusSettings.AutoFocusRange = AutoFocusRange.FullRange;
      focusSettings.Mode = FocusMode.Auto;
      focusSettings.WaitForFocus = true;
      focusSettings.DisableDriverFallback = false;

      mediaCapture.VideoDeviceController.FocusControl.Configure(focusSettings);
      await mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true);

      mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
      mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);
    }

    captureReceipt.Source = mediaCapture;
    await mediaCapture.StartPreviewAsync();
  }
  catch (Exception ex)
  {
    DisposeMediaCapture();
    error = ex.Message;
  }

  if (error != null)
  {
    await (new MessageBoxImpl()).ShowMessageAsync(error);
  }
}

private static async Task<DeviceInformation> GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel desiredPanel)
{

  DeviceInformation device = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
      .FirstOrDefault(d => d.EnclosureLocation != null && d.EnclosureLocation.Panel == desiredPanel);

  if (device == null)
  {
    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "No suitable devices found for the camera of type {0}.", desiredPanel));
  }
  return device;
}

关于c# - Windows Phone 8.1 相机初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825398/

相关文章:

c++ - 警告 C6273 : Non-integer passed as _Param_(2) when an integer is required

android - MapFragment和CameraView(Surfaceview)下的Navigationdrawer

c# - 找出从 CRM Dynamics 返回的实体中所有属性的名称

c# - 拖放到具有多个文本框的复合用户控件上

c# - 在继续之前强制挂载 USB 拇指驱动器

winforms - 如何使用 NuGet 工作流程避免因程序集版本不匹配而导致的 Winforms 设计器错误

使用相机后尝试编辑图片时,Android 新照片应用程序崩溃

javascript - 从网络浏览器拍照

c# - FileSystemWatcher 无法正常工作

c# - 使用 where 将 foreach 转换为 LINQ 语句