加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 打开/关闭闪光灯

发布时间:2020-12-15 08:22:25 所属栏目:百科 来源:网络整理
导读:好的,我的问题很简单. 我已经设法打开闪光灯(并保持打开). 但是,我仍然不确定如何关闭它(笑). 这是我的代码: var sensorLocation = CameraSensorLocation.Back;try{ // get the AudioViceoCaptureDevice var avDevice = await AudioVideoCaptureDevice.Open
好的,我的问题很简单.

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

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

这是我的代码:

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上进行了测试.

解决方法

看起来你无法检索两次采集设备(我不知道为什么),所以你应该将它存储在一个属性中:
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,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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读