6

好的,我的问题很简单。

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

但是,我仍然不确定如何将其关闭(笑)。

这是我的代码:

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

2 回答 2

10

看起来您无法两次检索采集设备(我不确定为什么),因此您应该将其存储在一个属性中:

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);
    }
}
于 2013-06-23T09:35:20.930 回答
0

试试这个

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

检查手电筒是否存在于设备中。

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

更改手电筒状态

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){ }
}
于 2015-11-19T10:28:09.667 回答