Created
August 28, 2014 17:08
-
-
Save circuitsenses/01c92963faa305b02545 to your computer and use it in GitHub Desktop.
Modulate the camera flash on a Lumia 920 Windows Phone 8 device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using Microsoft.Phone.Controls; | |
using Microsoft.Phone.Shell; | |
using FlashKontrol.Resources; | |
using System.Windows.Media; | |
using Windows.Phone.Media.Capture; | |
using System.Windows.Threading; | |
using Windows.System.Threading; | |
namespace FlashKontrol | |
{ | |
public partial class MainPage : PhoneApplicationPage | |
{ | |
private AudioVideoCaptureDevice _captureDev; | |
private const CameraSensorLocation _sensorLoc = CameraSensorLocation.Back; | |
private IReadOnlyList<object> supportedCameraModes; | |
private bool torchMode = false; | |
private DispatcherTimer dispTimer; | |
private ThreadPoolTimer periodicTimer; | |
private bool flashFlag = false; | |
// Constructor | |
public MainPage() | |
{ | |
InitializeComponent(); | |
InitializeCaptureDevice(); | |
SelectCaptureDevice(); | |
flashOff.IsEnabled = false; | |
tspan.Text = "500"; | |
// Sample code to localize the ApplicationBar | |
//BuildLocalizedApplicationBar(); | |
} | |
private async void InitializeCaptureDevice() | |
{ | |
_captureDev = await GetCaptureDevice(); | |
} | |
private async Task<AudioVideoCaptureDevice> GetCaptureDevice() | |
{ | |
AudioVideoCaptureDevice captureDevice = await AudioVideoCaptureDevice.OpenAsync(_sensorLoc, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(_sensorLoc).First()); | |
return captureDevice; | |
} | |
private void SelectCaptureDevice() | |
{ | |
try | |
{ | |
supportedCameraModes = AudioVideoCaptureDevice.GetSupportedPropertyValues(_sensorLoc, KnownCameraAudioVideoProperties.VideoTorchMode); | |
torchMode = supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On); | |
if (torchMode) | |
{ | |
_captureDev.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, AudioVideoCaptureDevice.GetSupportedPropertyRange(_sensorLoc, KnownCameraAudioVideoProperties.VideoTorchPower).Max); | |
} | |
} | |
catch (Exception ex) | |
{ | |
} | |
} | |
private void BitOn() | |
{ | |
_captureDev.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); | |
} | |
private void BitOff() | |
{ | |
_captureDev.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off); | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
if (torchMode) | |
{ | |
BitOn(); | |
flashOn.IsEnabled = false; | |
flashOff.IsEnabled = true; | |
} | |
} | |
private void Button_Click_1(object sender, RoutedEventArgs e) | |
{ | |
if (torchMode) | |
{ | |
if (this.dispTimer != null) | |
{ | |
this.dispTimer.Stop(); | |
timerOn.IsEnabled = true; | |
BitOff(); | |
flashOn.IsEnabled = true; | |
flashOff.IsEnabled = false; | |
poolTime.IsEnabled = true; | |
} | |
else | |
{ | |
BitOff(); | |
flashOn.IsEnabled = true; | |
flashOff.IsEnabled = false; | |
} | |
if (periodicTimer != null) | |
{ | |
periodicTimer.Cancel(); | |
flashOn.IsEnabled = true; | |
flashOff.IsEnabled = false; | |
poolTime.IsEnabled = true; | |
} | |
_captureDev.Dispose(); | |
} | |
} | |
private void timerOn_Click(object sender, RoutedEventArgs e) | |
{ | |
if (this.dispTimer == null) | |
{ | |
this.dispTimer = new DispatcherTimer(); | |
this.dispTimer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(tspan.Text)); | |
this.dispTimer.Tick += new EventHandler(dispTimerEventHandler); | |
} | |
flashOn.IsEnabled = false; | |
flashOff.IsEnabled = true; | |
timerOn.IsEnabled = false; | |
poolTime.IsEnabled = false; | |
this.dispTimer.Start(); | |
} | |
private void dispTimerEventHandler(object sender, EventArgs e) | |
{ | |
if (flashFlag == false) | |
{ | |
if (torchMode) | |
{ | |
BitOn(); | |
flashFlag = true; | |
} | |
} | |
else | |
{ | |
if (torchMode) | |
{ | |
BitOff(); | |
flashFlag = false; | |
} | |
} | |
} | |
private void poolTime_Click(object sender, RoutedEventArgs e) | |
{ | |
if (periodicTimer == null) | |
{ | |
flashOn.IsEnabled = false; | |
flashOff.IsEnabled = true; | |
timerOn.IsEnabled = false; | |
poolTime.IsEnabled = false; | |
periodicTimer = ThreadPoolTimer.CreatePeriodicTimer(PoolTimerHandler, TimeSpan.FromMilliseconds(Convert.ToDouble(tspan.Text)), DestroyPoolTmrHandler); | |
} | |
} | |
private void PoolTimerHandler(ThreadPoolTimer timer) | |
{ | |
if (flashFlag == false) | |
{ | |
if (torchMode) | |
{ | |
BitOn(); | |
flashFlag = true; | |
} | |
} | |
else | |
{ | |
if (torchMode) | |
{ | |
BitOff(); | |
flashFlag = false; | |
} | |
} | |
} | |
private void DestroyPoolTmrHandler(ThreadPoolTimer dTimer) | |
{ | |
dTimer.Cancel(); | |
} | |
// Sample code for building a localized ApplicationBar | |
//private void BuildLocalizedApplicationBar() | |
//{ | |
// // Set the page's ApplicationBar to a new instance of ApplicationBar. | |
// ApplicationBar = new ApplicationBar(); | |
// // Create a new button and set the text value to the localized string from AppResources. | |
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative)); | |
// appBarButton.Text = AppResources.AppBarButtonText; | |
// ApplicationBar.Buttons.Add(appBarButton); | |
// // Create a new menu item with the localized string from AppResources. | |
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText); | |
// ApplicationBar.MenuItems.Add(appBarMenuItem); | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment