打开/关闭闪光灯
Posted
技术标签:
【中文标题】打开/关闭闪光灯【英文标题】:Turn Flash On/Off 【发布时间】:2013-06-20 00:06:42 【问题描述】:好的,我的问题很简单。
我已经成功地打开了闪光灯(并保持打开状态)。
但是,我仍然不确定如何将其关闭(笑)。
这是我的代码:
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);
有什么想法吗?
附言
我原以为将.on
s 转换为.off
s 可以,但它没有。
已在 HTC 8S 和 Lumia 820 上进行了测试。
【问题讨论】:
【参考方案1】:您似乎无法两次检索采集设备(我不确定为什么),因此您应该将其存储在一个属性中:
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);
【讨论】:
嗯,这太棒了——而且如此简单! 有效!非常感谢,伙计! ;-)【参考方案2】:试试这个
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)
【讨论】:
以上是关于打开/关闭闪光灯的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中打开/关闭 iPhone LED(闪光灯)? [复制]