[WP Development] Flashlight

Source: Internet
Author: User

Many people may think that they can use the flashlight from the camera on their mobile phones to make a flashlight. Of course, the advantage will be harmful. each time you use the flashlight, it should not be too long. Generally, there will be no problem in a few minutes, if the time is too long, it is difficult to guarantee the damage.

The previous solution was to call the video recording function to start recording videos and turn on the camera lamp to make a flashlight.

In fact, in 8.1, The mediacapture class transplanted from the RT Library (located in the windows. Media. Capture namespace) can enable the camera light when no video is recorded.

That is to say,You do not need to call the startrecordtoxxx method to start recording. Instead, you just need to turn on the camera lamp, so that you do not need to write files or consume memory.. Let me talk about the general idea.

1. initialize the mediacapture component. This step is certainly indispensable;

2. The mediacapture class has a videodevicecontroller attribute, which can return the videodevicecontroller instance;

3. The returned videodevicecontroller instance has a torchcontrol attribute and can return a torchcontrol instance;

4. The torchcontrol class has an enabled attribute. When it is set to true, the camera light will be turned on, and it will be turned on without waiting to start recording. If it is set to false, the camera light will be turned off.

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

Someone may ask, isn't it flashcontrol? It's not Flash, it's a flash, it's just a flash when you take a photo. Flash can only be a flash, and the flashlight is always on, therefore, flash cannot be used.

Well, with the principle, you can start to implement it.

Here, I will initialize mediacapture, clean up mediacapture, enable/disable camera lights, and other operations will be encapsulated.

Internal static class captureoperator {# region private field static mediacapture m_capture = NULL; static bool m_istorchopened = false; static bool m_iscapturecreated = false; # endregion # region attributes // <summary> // indicates whether the camera is enabled /// </Summary> Public static bool istorchopened {get {return m_istorchopened ;}} /// <summary> /// indicates whether mediacapture has been initialized. // </Summary> Public static bool iscapturecreated {get {r Eturn m_iscapturecreated; }}# endregion # region method // <summary> // initialize the object to be captured // </Summary> Public async static task createcaptureasync () {// locate the rear camera. Generally, the flashlight is mounted to the rear camera. deviceinformation backcapture = (from D in await getcapturedeviceseasync () Where D. enclosurelocation. panel = panel. back select D ). firstordefault (); If (backcapture! = NULL) {mediacaptureinitializationsettings = new mediacaptureinitializationsettings (); settings. videodeviceid = backcapture. ID; // The device ID settings. streamingcapturemode = streamingcapturemode. video; settings. photocapturesource = photocapturesource. auto; // initialize m_capture = new mediacapture (); await m_capture.initializeasync (settings); m_iscapturecreated = true ;}/// <summary> /// obtain the list of camera devices (Front and rear cameras) /// </Summary> /// <returns> </returns> private async static task <deviceinformation []> getcapturedeviceseasync () {var DVS = await deviceinformation. findallasync (deviceclass. videocapture); Return DVS. toarray () ;}/// <summary> // clear the captured object /// </Summary> /// <returns> </returns> Public static void cleanupcaptureasync () {If (m_capture! = NULL) {m_capture.dispose (); m_capture = NULL; m_iscapturecreated = false;} public static void opentorch () {// turn on the flashlight var vdcontrol = enabled; If (vdcontrol. supported) {vdcontrol. enabled = true; m_istorchopened = true;} public static void closetorch () {// disable the flashlight var torch = m_capture.videodevicecontroller.torchcontrol; If (torch. supported) {torch. enabled = false; m_istorchopened = false; }}# endregion}

The other code that I shared with you earlier is the same as the code that solves the problem of "crash when calling a camera. Focus:

// 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 of the flashlight.

In addition, as I have mentioned earlier, the mediacapture object must be dispose when the application is suspended or closed. Otherwise, system resources will become stuck. When the application continues to run and reinitializes.

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 the application status and stop any background activity captureoperator. cleanupcaptureasync (); deferral. complete ();}

Sample Code download: http://files.cnblogs.com/tcjiaan/FlashlightApp.zip

[WP Development] Flashlight

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.