WP8 Flashlight Application Source code example

Source: Internet
Author: User
Tags static class

Perhaps a lot of people think, can use mobile phone on camera flash flashlight, of course, beneficial will be harmful, each time use not too long, a few minutes generally will not have any problems, if the time is too long, it will be damaged.

The previous scheme was to call the video recording function to start recording the video, and at the same time turn on the camera light to make a flashlight.

In fact, in 8.1, the Mediacapture class, which is ported from the RT library (located in the Windows.Media.Capture namespace), can turn on the camera light when no video is recorded.

That is, you don't need to call the Startrecordtoxxx method to start recording, but you can just turn the camera light on so that you don't have to write files or consume memory. I'll tell you about the idea.

1, initialization mediacapture components, this step must not be less;

2, Mediacapture class has a Videodevicecontroller property, can return Videodevicecontroller instance;

3, the returned Videodevicecontroller instance has a Torchcontrol property, can return a Torchcontrol instance;

4, the Torchcontrol class has an enabled property, set to True, the camera light is turned on, do not have to wait to start recording it will open, if set to false, the camera light will be closed.

Smart you, must understand, yes, that's it.

Some people will ask, is not flashcontrol, not flash is flash, is the time to take photos flash that flash can only flash, and flashlight is to continue to light, therefore can not use flash.

Well, with the principle, to realize it depends on the hands.

Here I will initialize the mediacapture, clean the mediacapture, turn on/off the camera lights and so on to encapsulate the operation.

Internal Static class Captureoperator
{
#region Private Fields
static mediacapture m_capture = null;
static bool m_istorchopened = FALSE;
static bool m_iscapturecreated = FALSE;
#endregion

#region Properties
<summary>
Indicates whether the camera is turned on
</summary>
public static bool Istorchopened
{
get {return m_istorchopened;}
}
<summary>
Indicates whether Mediacapture has been initialized
</summary>
public static bool Iscapturecreated
{
get {return m_iscapturecreated;}
}
#endregion

#region method
<summary>
Initializing capture objects
</summary>
Public async static Task Createcaptureasync ()
{
Find the rear camera, and the general Flash is on the back-facing camera.
Deviceinformation backcapture = (from D in await Getcapturedeviceseasync () where D.enclosurelocation.panel = = Panel.Back S Elect D). FirstOrDefault ();

if (backcapture!= null)
{
Mediacaptureinitializationsettings settings = new Mediacaptureinitializationsettings ();
Settings. Videodeviceid = backcapture.id; Device ID
Settings. Streamingcapturemode = Streamingcapturemode.video;
Settings. Photocapturesource = Photocapturesource.auto;
Class
M_capture = new Mediacapture ();
Await M_capture. Initializeasync (settings);
M_iscapturecreated = true;
}
}

<summary>
Get a list of camera devices (front, rear camera)
</summary>
<returns></returns>
Private async static task<deviceinformation[]> Getcapturedeviceseasync ()
{
var DVS = await deviceinformation.findallasync (deviceclass.videocapture);
Return DVS. ToArray ();
}

<summary>
Clean up capture objects
</summary>
<returns></returns>
public static void Cleanupcaptureasync ()
{
if (m_capture!= null)
{
M_capture. Dispose ();
M_capture = null;
m_iscapturecreated = false;
}
}

public static void Opentorch ()
{
Open Flash
var Vdcontrol = m_capture. Videodevicecontroller.torchcontrol;
if (Vdcontrol. Supported)
{
Vdcontrol. Enabled = true;
M_istorchopened = true;
}
}

public static void Closetorch ()
{
Turn off the Flash
var torch = m_capture. Videodevicecontroller.torchcontrol;
if (torch. Supported)
{
Torch. Enabled = false;
m_istorchopened = false;
}
}


#endregion
}


Other than the previous I have shared the "Resolution call camera when the crash" code, are a way of thinking. The point is to look at:

Turn on the camera light
var Vdcontrol = m_capture. Videodevicecontroller.torchcontrol;
if (Vdcontrol. Supported)
{
Vdcontrol. Enabled = true;
M_istorchopened = true;
}

......
Turn off the camera light
var torch = m_capture. Videodevicecontroller.torchcontrol;
if (torch. Supported)
{
Torch. Enabled = false;
m_istorchopened = false;
}


This is the key code for the flashlight.

Another point, as I said earlier, must dispose of the Mediacapture object when the application hangs or shuts down, otherwise the system resources are occupied, will cause the card to die. When the application continues to run reinitialization.

Public App ()
{
This. InitializeComponent ();
This. suspending = this. onsuspending;
This. resuming = this. onresuming;
This. UnhandledException + = app_unhandledexception;
}

void App_unhandledexception (object sender, UnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine (E.exception.message);
Captureoperator.cleanupcaptureasync ();
}

Private async void Onresuming (object sender, Object e)
{
await Captureoperator.createcaptureasync ();
}
private void Onsuspending (object sender, Suspendingeventargs e)
{
var deferral = e.suspendingoperation.getdeferral ();

TODO: Save application state and stop any background activity
Captureoperator.cleanupcaptureasync ();
Deferral.complete ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.