打开/关闭闪光灯 [英] Turn Flash On/Off

查看:30
本文介绍了打开/关闭闪光灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的问题很简单.

OK, My issue is quite simple.

我已经设法打开闪光灯(并保持打开状态).

I've managed to turn the flash On (and keep it On).

但是,我仍然不确定如何关闭它(笑).

However, I'm still not sure how to turn it off (lol).

这是我的代码:

var sensorLocation = CameraSensorLocation.Back;

try
{
    // get the AudioViceoCaptureDevice
    var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
        AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());

    // turn flashlight on
    var supportedCameraModes = AudioVideoCaptureDevice
        .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
    if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
    {
        avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

        // set flash power to maxinum
        avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
            AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
    }
    else
    {
        turnWhiteScreen(true);
    }

}
catch (Exception ex)
{
    // Flashlight isn't supported on this device, instead show a White Screen as the flash light
    turnWhiteScreen(true);
}

有什么想法吗?

附言

  • 我认为将 .ons 转换为 .offs 可以工作,但它没有.
  • 这已经在 HTC 8S 和 Lumia 820 上进行了测试.
  • I imagined that converting .ons to .offs could have worked, but it doesn't.
  • This has been tested on a HTC 8S and Lumia 820.

推荐答案

看起来你不能两次检索采集设备(我不知道为什么),所以你应该把它存储在一个属性中:

It looks like you can't retrieve the acquisition device twice (I'm not sure why), so you should store it in a property:

protected AudioVideoCaptureDevice Device { get; set; }

private async void ButtonTurnOn_Click(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;

    try
    {
        if (this.Device == null)
        {
            // get the AudioViceoCaptureDevice
            this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
            AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
        }

        // turn flashlight on
        var supportedCameraModes = AudioVideoCaptureDevice
            .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
        if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
        {
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

            // set flash power to maxinum
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
                AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
        }
        else
        {
            turnWhiteScreen(true);
        }

    }
    catch (Exception ex)
    {
        // Flashlight isn't supported on this device, instead show a White Screen as the flash light
        turnWhiteScreen(true);
    }
}

然后,将其关闭:

private void ButtonTurnOff_Click(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;

    try
    {
        // turn flashlight on
        var supportedCameraModes = AudioVideoCaptureDevice
            .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
        if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
        {
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
        }
        else
        {
            turnWhiteScreen(false);
        }
    }
    catch (Exception ex)
    {
        // Flashlight isn't supported on this device, instead show a White Screen as the flash light
        turnWhiteScreen(false);
    }
}

这篇关于打开/关闭闪光灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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