📜 ⬆️ ⬇️

Flashlight for Windows Phone 7

Foreword


I bought myself a VDNophone and decided to try to write something for it, especially since I am familiar with .NET by occupation. The idea was suggested by a friend with a flashlight android application, the light source of which was an LED flash.


First decision


I read msdn , everything seemed simple. Create a Microsoft.Devices.PhotoCamera object; see if our device supports flash photography; if supported, then switch PhotoCamera.FlashMode , by pressing a button. As Livanov would say: “Elementary, Watson!”. But it was not there.

The first moment that puzzled me was the initialization of the camera. Just creating a Microsoft.Devices.PhotoCamera object is not enough. For these purposes, in the already familiar article on msdn , the installation of the created camera object is used as a source for VideoBrush . This is an excessive load, as it seems to me, but I did not find other ways (maybe someone from the reader will tell). By the way, I didn’t search tightly, because I was moving by another goal.
')
But the second moment stumped the described solution option. As a result, at the touch of a button, we see that the display is illuminated, and the LED flash does not want to work. Well, sadness.


Second solution


We are starting to dig further ... I was surprised to learn from Google that the guys from Microsoft had hidden some assemblies from the unenlightened (here are the villains).

I found a craftsman on the codeproject , who hacked the WP7 camera with the help of hidden features. But the post itself is not so interesting as the links it provides for general use. The idea is to, as described in Tom's blog , make the necessary assemblies available and use them for highlighting, as advised by the Clarity Consulting Blog , Microsoft.Devices.VideoCamera .


Implementation


So, more than enough information, let's get down to business. Tom's blog says that the easiest way is to get the necessary builds from the “Emulator BIN” or download its own dump. I did not understand what “Emulator BIN” is and did not bother for a long time, I took advantage of its work. We take from the GAC_Microsoft.Phone.Media.Extended_v7_0_0_0_cneutral_1.dll archive and, to make it easier for us to add links next time to this build, copy it to the folder " C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ Silverlight \ v4 .0 \ Profile \ WindowsPhone71 "(need administrator rights) named Microsoft.Phone.Media.Extended.dll . Naturally, one OS is on a different partition or not 64-bit, there will be small variations.
Then open the folder named RedistList (it lies in the same directory as the file just copied), and in it is the file FrameworkList.xml . It needs to be edited (I did it in the spotlight, for a folder with specific access rights). Copy an arbitrary File node, for example, for AssemblyName = "Microsoft.Phone.Interop" , delete the PublicKeyToken node in the newly copied node and change the AssemblyName to Microsoft.Phone.Media.Extended .
Now you need to open the visual studio command prompt and execute the sn -Vr Microsoft.Phone.Media.Extended.dll command, otherwise we will have errors when building the project with this build.


Now the most pleasant thing is creating the application. It is enough to create a regular Silverlight project for WP7 and add the Microsoft.Phone.Media.Extended assembly. It will have a rather simple markup, the only thing that is worthy of attention here is the CameraVisualizer control, it displays the image captured from the camera (but we remember that we only need it to initialize the camera object). And so that we are not distracted by the picture on the display, we will set this element to full opacity.
Here is the markup


 <phone:PhoneApplicationPage x:Class="PhoneApp1.MainPage" ... xmlns:media="clr-namespace:Microsoft.Phone;assembly=Microsoft.Phone.Media.Extended" ... shell:SystemTray.IsVisible="False"> <Grid x:Name="LayoutRoot" Background="Transparent"> <media:CameraVisualizer x:Name="_cameraVisualizer" Opacity="0" /> <Button x:Name="_btnFlash" Click="ButtonClick" IsEnabled="false"> <Button.Content> <Image VerticalAlignment="Center" HorizontalAlignment="Center" Width="300" Height="300" Source="Power_On_Off.png"/> </Button.Content> </Button> </Grid> </phone:PhoneApplicationPage> 


It is also interesting that the code should indicate the need to use the LED flash _videoCamera.LampEnabled = true; and the fact that the flashlight starts to work only while recording from the camera. More precisely, the record itself will not, that is, the data stream will not occupy physical space.


 using System.Windows; using Microsoft.Phone; namespace PhoneApp1 { public partial class MainPage { private VideoCamera _videoCamera; public MainPage() { InitializeComponent(); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); InitializeCamera(); } protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedFrom(e); _videoCamera.Dispose(); _videoCamera = null; } private void InitializeCamera() { _videoCamera = new VideoCamera(); //    _videoCamera.Initialized += VideoCameraInitialized; _cameraVisualizer.SetSource(_videoCamera);//    } void VideoCameraInitialized(object sender, System.EventArgs e) { _videoCamera.Initialized -= VideoCameraInitialized; _videoCamera.LampEnabled = true; //   !!! Dispatcher.BeginInvoke(() => { _btnFlash.IsEnabled = true; }); } private void ButtonClick(object sender, RoutedEventArgs e) { //   ,  .   ,  . if (_videoCamera.IsRecording) _videoCamera.StopRecording(); else _videoCamera.StartRecording(); } } } 


PS: Constructive criticism of the first topic and my presentation style is welcome.

Sources used


1 msdn
2 codeproject
3 Tom's blog
4 Clarity Consulting Blog
5 xda-developers


UPD Ladies and gentlemen, I understand perfectly well that someone liked the article, someone did not. But in lichku write only those who positively reacted to the post as a whole, but have comments on certain points. We kindly request - if you minus, leave a review that did not like. It is important for me at the first stage to understand what I am doing wrong.

Source: https://habr.com/ru/post/133968/


All Articles