在 Windows Phone 如果想要控制閃光燈,該怎麼做?在 Windows Phone 8 提供類別與方法可直接控制閃光燈。
前言
在 Windows Phone 如果想要控制閃光燈,該怎麼做?在 Windows Phone 8 提供類別與方法可直接控制閃光燈。
實作
AudioVideoCaptureDevice 類別用來擷取音樂和影片。
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207322%28v=vs.105%29.aspx
首先宣告一個 AudioVideoCaptureDevice 類別變數 Device。
protected AudioVideoCaptureDevice Device { get; set; }
開啟閃光燈
private async void ButtonTurnOnFlashLight_Click(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
try
{
if (this.Device == null)
{
// 取得 AudioViceoCaptureDevice
this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
}
// Turn on FlashLight
var supportedCameraModes = AudioVideoCaptureDevice.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
// 設定亮度為最大
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
}
}
catch (Exception ex)
{
// 無法控制閃光燈
}
}
關閉閃光燈
private void ButtonTurnOnFlashLight_Click(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
try
{
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
// Turn off FlashLight
if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
{ this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
}
}
catch (Exception ex)
{
// 無法控制閃光燈
}
}