在Windows Phone Silverlight 8.1上打开使用MediaCapture的手电筒 [英] Turn on torch with MediaCapture on Windows Phone Silverlight 8.1

查看:213
本文介绍了在Windows Phone Silverlight 8.1上打开使用MediaCapture的手电筒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,我无法打开闪光灯与MediaCapture API从Windows Phone 8.1。 (我用8.0 API成功)



我创建了一个非常简单的项目,有两个按钮,一个用于切换FlashControl,另一个用于切换TorchControl。

没有崩溃,也没有异常。我的手机支持FlashControl和TorchControl。



这是我的代码:







b $ b

  MediaCapture m_captureManager; 

public MainPage()
{
InitializeComponent();
}

private static async Task< DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID =(await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation!= null&& x.EnclosureLocation.Panel == desiredCamera);
if(deviceID!= null)return deviceID;
else throw new Exception(string.Format(Camera {0} does not exist,desiredCamera));
}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
m_captureManager = new MediaCapture();

await m_captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});

}

private void button_ClickTorch(object sender,RoutedEventArgs e)
{
var torch = m_captureManager.VideoDeviceController.TorchControl;
if(torch.Supported)
{
if(torch.Enabled)
torch.Enabled = false;
else
torch.Enabled = true;
}
}

private void button_ClickFlash(object sender,RoutedEventArgs e)
{
if(captureManager.VideoDeviceController.FlashControl.Supported)
{
if(captureManager.VideoDeviceController.FlashControl.Enabled)
captureManager.VideoDeviceController.FlashControl.Enabled = false;
else
captureManager.VideoDeviceController.FlashControl.Enabled = true;
}
}

这是一段简单的代码,工作...我非常绝望,所以我试图切换使用一个中间对象和没有,你可以看到,但它没有改变结果(这是一致的)。

解决方案

我终于找到了什么问题。为了能够使用摄像头服务,我们必须开始预览。
从Silverlight我们不能使用CaptureElement,我们必须使用CustomPreviewSink与VideoBrush



这是怎么做的(从 microsoft doc

  private async void StartPreview()
{
previewSink = new Windows.Phone.Media.Capture.MediaCapturePreviewSink ;

//默认预览格式选择器使用的支持的视频预览格式列表。
var supportedVideoFormats = new List< string> {nv12,rgb32};

//查找支持的预览格式
var availableMediaStreamProperties = mediaCaptureManager.VideoDeviceController.GetAvailableMediaStreamProperties(
Windows.Media.Capture.MediaStreamType.VideoPreview)
.OfType< Windows .Media.MediaProperties.VideoEncodingProperties>()
.Where(p => p!= null
&&!String.IsNullOrEmpty(p.Subtype)
&& supportedVideoFormats .Contains(p.Subtype.ToLower()))
.ToList();
var previewFormat = availableMediaStreamProperties.FirstOrDefault();

//开始预览流
await mediaCaptureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(
Windows.Media.Capture.MediaStreamType.VideoPreview,previewFormat);
await mediaCaptureManager.StartPreviewToCustomSinkAsync(
new Windows.Media.MediaProperties.MediaEncodingProfile {Video = previewFormat},previewSink);


//设置用于预览的VideoBrush的源代码
Microsoft.Devices.CameraVideoBrushExtensions.SetSource(viewfinderBrush,previewSink);
}

将这段代码添加到以前的代码中,它会工作。重要的一点是在更改任何参数之前开始预览


My problem is quite simple, I cannot turn on the flash light with the MediaCapture API from windows phone 8.1. (I succedded with the 8.0 API)

I built a very simple project with 2 buttons, one to toggle the FlashControl and the other one to toggle TorchControl.

There is no crash, no exception. My phones support FlashControl and TorchControl. I also debug step-by-step and everything looks good, values are changed when buttons are clicked.

Here is my code:

MediaCapture m_captureManager;

public MainPage()
{
  InitializeComponent();
}

private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
  DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
      .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
  if (deviceID != null) return deviceID;
  else throw new Exception(string.Format("Camera {0} doesn't exist", desiredCamera));
}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
  var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
  m_captureManager = new MediaCapture();

  await m_captureManager.InitializeAsync(new MediaCaptureInitializationSettings
  {
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
  });

}

private void button_ClickTorch(object sender, RoutedEventArgs e)
{
  var torch = m_captureManager.VideoDeviceController.TorchControl;
  if (torch.Supported)
  {
    if (torch.Enabled)
      torch.Enabled = false;
    else
      torch.Enabled = true;
  }
}

private void button_ClickFlash(object sender, RoutedEventArgs e)
{
  if (captureManager.VideoDeviceController.FlashControl.Supported)
  {
    if (captureManager.VideoDeviceController.FlashControl.Enabled)
      captureManager.VideoDeviceController.FlashControl.Enabled = false;
    else
      captureManager.VideoDeviceController.FlashControl.Enabled = true;
  }
}

It's a simple piece of code and I cannot make it works... I was quite desperate so I tried to toggle by using an intermediate object and without, as you can see, but it did not change the result (which is in conformity).

解决方案

I finally found out what was wrong. To be able to use camera services, we have to start a preview. Since with Silverlight we can’t use a CaptureElement, we have to use a CustomPreviewSink with a VideoBrush

This is how to do it ( from microsoft doc)

private async void StartPreview()
{
previewSink = new Windows.Phone.Media.Capture.MediaCapturePreviewSink();

// List of supported video preview formats to be used by the default preview format selector.
var supportedVideoFormats = new List<string> { "nv12", "rgb32" };

// Find the supported preview format
var availableMediaStreamProperties = mediaCaptureManager.VideoDeviceController.GetAvailableMediaStreamProperties(
    Windows.Media.Capture.MediaStreamType.VideoPreview)
        .OfType<Windows.Media.MediaProperties.VideoEncodingProperties>()
        .Where(p => p != null 
            && !String.IsNullOrEmpty(p.Subtype) 
            && supportedVideoFormats.Contains(p.Subtype.ToLower()))
        .ToList();
var previewFormat = availableMediaStreamProperties.FirstOrDefault();

// Start Preview stream
await mediaCaptureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(
    Windows.Media.Capture.MediaStreamType.VideoPreview, previewFormat);
await mediaCaptureManager.StartPreviewToCustomSinkAsync(
    new Windows.Media.MediaProperties.MediaEncodingProfile { Video = previewFormat }, previewSink);


// Set the source of the VideoBrush used for your preview
Microsoft.Devices.CameraVideoBrushExtensions.SetSource(viewfinderBrush, previewSink);
}

Add this piece of code to previous code and it will work. The important point is to start the preview before changing any parameters

这篇关于在Windows Phone Silverlight 8.1上打开使用MediaCapture的手电筒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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