c# – 打开/关闭闪光灯

前端之家收集整理的这篇文章主要介绍了c# – 打开/关闭闪光灯前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我的问题很简单.

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

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

这是我的代码

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

猜你在找的C#相关文章