的Windows Phone 8.1初始化摄像头 [英] Windows Phone 8.1 camera initialization

查看:218
本文介绍了的Windows Phone 8.1初始化摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道大约有更多这方面的重复的问题,但请,这对我很重要超强。我有Windows Phone的8.1 ​​C#相机初始化现在日子不好过。

I am aware about that there are more duplicate questions about this, but please, it is super important to me. I have hard time now with Windows Phone 8.1 C# camera initialization.

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);
         } 
    }

这是我的代码初始化的摄像头,但对于一些原因它 Windows.Media.Capture.MediaCapture()构造与 System.UnauthorizedAccessException的呼叫失败上的Lumia 920也在仿真器访问冲突。我GOOGLE了对这个问题,但没有答案为止。有些人告诉我,我应该能够不只是摄像头,而且还话筒,但是这并没有解决我的问题。一切似乎得到很好的设置,所有的访问是在应用程序清单理所当然的。此外,我想问问你,如果你有拍照与摄像头有些是好的,工作的例子/教程,请提供。

this is my code to initialize camera, but for some reason it fails on Windows.Media.Capture.MediaCapture() constructor call with System.UnauthorizedAccessException on Lumia 920 and also Access Violation on emulator. I've googled about the issue, but no answers so far. Some folks told that I should enable not just webcam, but also microphone, but that did not solved my issue. Everything seems to be well set, all access were granted in app manifest. Also I want to ask you, if you have some good and working example/tutorial of taking pictures with camera, please provide.

推荐答案

下面是我的摄像头采集,其工作方式,我在商店提交应用程序的代码:

Below is my code for camera capture, which works, I have a submitted app in the store:

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;
}

这篇关于的Windows Phone 8.1初始化摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆