Windows Phone 8.1相机初始化

3

我知道有很多关于这个问题的重复提问,但是,对我来说非常重要。我现在在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,在模拟器上则显示访问冲突。我已经在Google上搜索了这个问题,但目前没有答案。有些人告诉我不仅需要启用网络摄像头,还需要启用麦克风,但这并没有解决我的问题。一切似乎都设置得很好,在应用程序清单中授予了所有访问权限。另外,如果你有一些关于如何使用摄像头拍照的好的、可行的示例/教程,请提供。
1个回答

7
以下是我的摄像头捕获代码,它可以工作,我已经在商店提交了一个应用程序:
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;
}

@LászlóCitrusNagy,另外在软件包清单中,我已经设置了网络摄像头功能,但我想你也已经完成了... - Jogy
@LászlóCitrusNagy,你试过第17节的例子吗?我试了一下,对我也有效。 - Jogy
是的,我已经尝试过了,但没有成功,所以我就生气了,把代码移植到了WP8.0上,现在它可以完美运行 :) - Citrus
1
小心自动对焦设置。我建议在代码的这部分周围加上“if(MediaCapture.VideoDeviceController.FocusControl.FocusChangedSupported){}”。一些设备,如糟糕的Lumia 530,在没有此检查的情况下会崩溃。 - muetzenflo
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接