简体   繁体   English

打开/关闭闪光灯

[英]Turn Flash On/Off

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). 但是,我仍然不确定如何关闭它(笑)。

Here's my code : 这是我的代码:

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

Any ideas? 有任何想法吗?


PS PS

  • I imagined that converting .on s to .off s could have worked, but it doesn't. 我想象着转换.on s到.off小号可以工作,但事实并非如此。
  • This has been tested on a HTC 8S and Lumia 820. 这已经在HTC 8S和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);
    }
}

Then, to turn it off: 然后,将其关闭:

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

Try this one 试试这个吧

private static VideoTorchMode _videoTorchMode = VideoTorchMode.Off;
private AudioVideoCaptureDevice _videoRecordingDevice;

Check torch is exist in device. 检查割炬存在于设备中。

private async void CheckTorch() {
  if(AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) &&
      AudioVideoCaptureDevice.GetSupportedPropertyValues(CameraSensorLocation.Back, KnownCameraAudioVideoProperties.VideoTorchMode).ToList().Contains((UInt32)VideoTorchMode.On)) {
     var temp = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back)[0];
     var resolution = new Windows.Foundation.Size(temp .Width, temp .Height);
     _videoRecordingDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution);
   } 
   else
     MessageBox.Show("Your device does not support torch");
}

To change torch state 改变火炬状态

private void SetTorchMode(){   
   try {
     if (BackgroundHandler.Instance.IsBackTorchExist) {
        if (_videoTorchMode == VideoTorchMode.Off) {
           _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
           _videoTorchMode = VideoTorchMode.On;
         }
         else {
            _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
            _videoTorchMode = VideoTorchMode.Off;                                    
         }
      }
   }
   catch (Exception ex){ }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM