locked
How to turn on/off the flashlight in Win8.1 tablet

    Question

  • Hi,

    I used these codes :

    MediaCapture.VideoDeviceController.TorchControl.Enabled = true;
     MediaCapture.VideoDeviceController.TorchControl.PowerPercent = 100;

    but have error :

    There is no windows.Media.Capture.MediaCapture.VideoDeviceController.TorchControl.Enabled = true;

    Can some1 show me the code to do it. Thanks

    Tuesday, October 28, 2014 4:20 AM

Answers

  • Try this out:
     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Button x:Name="TorchButton" Content="Turn Torch On" Click="TorchButton_Click"/>
    </Grid>
    MediaCapture MyMediaCapture = new MediaCapture();
    
            private async void TorchButton_Click(object sender, RoutedEventArgs e)
            {
                await MyMediaCapture.InitializeAsync();
                var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;
                var MyTorch = MyVideoDeviceController.TorchControl;
    
                if (MyTorch.Supported)
                {
                    if ((TorchButton.Content as string) == "Turn Torch On")
                    {
                        TorchButton.Content = "Turn Torch Off";
                        MyTorch.PowerPercent = 100;
                        MyTorch.Enabled = true;
                    }
                    else
                    {
                        TorchButton.Content = "Turn Torch On";
                        MyTorch.PowerPercent = 0;
                        MyTorch.Enabled = false;
                    }
                }
                else
                {
                    TorchButton.Content = "Torch not supported!";
                }
            }
        }



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by imnbwd Wednesday, October 29, 2014 10:08 AM
    • Marked as answer by FireDance Wednesday, November 05, 2014 9:53 AM
    Tuesday, October 28, 2014 1:15 PM
    Moderator

All replies

  • Try this out:
     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Button x:Name="TorchButton" Content="Turn Torch On" Click="TorchButton_Click"/>
    </Grid>
    MediaCapture MyMediaCapture = new MediaCapture();
    
            private async void TorchButton_Click(object sender, RoutedEventArgs e)
            {
                await MyMediaCapture.InitializeAsync();
                var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;
                var MyTorch = MyVideoDeviceController.TorchControl;
    
                if (MyTorch.Supported)
                {
                    if ((TorchButton.Content as string) == "Turn Torch On")
                    {
                        TorchButton.Content = "Turn Torch Off";
                        MyTorch.PowerPercent = 100;
                        MyTorch.Enabled = true;
                    }
                    else
                    {
                        TorchButton.Content = "Turn Torch On";
                        MyTorch.PowerPercent = 0;
                        MyTorch.Enabled = false;
                    }
                }
                else
                {
                    TorchButton.Content = "Torch not supported!";
                }
            }
        }



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by imnbwd Wednesday, October 29, 2014 10:08 AM
    • Marked as answer by FireDance Wednesday, November 05, 2014 9:53 AM
    Tuesday, October 28, 2014 1:15 PM
    Moderator
  • Thanks.

    This issue leads me to many questions.

    1) I need clarification on these terms:

    a) Does Torch control mean Camera light or LED light?

    b) Does Flash control mean flashlight?

    2) Do all the different brands of tablets implement (a) and (b)?

    3) I did a test using the code for Panasonic tablet which has LED light or camera light.

    The code you provided will not work on Panasonic Tablet as it looks for Torch control. In this case, is Torch control same as Panasonic's Camera Light or LED light. Is there a different? or the same thing but call differently?

    then,I used Panasonic's own camera app which has turn on and off camera light feature. it works. So, what they use to talk to the camera light if MediaCapture not in used?

    I changed a bit of your code to include Torch and Flash. It did not work either on Panasonic tablet.

    await MyMediaCapture.InitializeAsync();

                    var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;

                    var MyTorch = MyVideoDeviceController.TorchControl;

                    var MyFlash = MyVideoDeviceController.FlashControl;


                    if (MyTorch.Supported)
                    {
                        MyTorch.PowerPercent = 100;
                        MyTorch.Enabled = true;

                    }
                    else
                    {

                        if (MyFlash.Supported)
                        {
                            MyFlash.PowerPercent = 100;
                            MyFlash.Enabled = true;
                        }
                        else
                        {
                             var mymsgbox = new Windows.UI.Popups.MessageDialog("No Flash and Torch Support", "Flash and Torch");
                            await mymsgbox.ShowAsync();
                        }

                    }             

    4) To turn off camera light or flashlight. Use below?

      MyFlash.Enabled = false;

    what I need to do?

    Thanks

    Wednesday, October 29, 2014 6:44 AM
  • The torch light is supposed to be any kind of light source linked to the Camera. Not all devices come with such a light source which is why you have to check with MyTorch.Supported if you can use it. It is however also dependent on the Camera driver if the light can be turned on without video data actually being captured. Several devices (at least on the phone side) only turn on the Camera Light when you're actually capturing video data - not when only in preview mode.

    The difference between MyTorch and MyFlash will likely talk to the same hardware in most devices (as they use the same LED for lighting in both Video Torch and Flash Mode.

    As for your last question - yes, changing the Enabled-Flag to false again should turn off the light.

    Wednesday, October 29, 2014 9:39 AM
  • hi,

    I would like to know the following:

    1. Light source for taking photo : Flash and Camera Light

    How come Panasonic's own Camera App can have access to the LED light?

    It is not that MediaCapture api will help to check if there are LED light and Flash. What I need to do what Panasonic's Camera App can do ? in this case, turn on the Camera light?

    My understanding is that I can turn on LED light or Flash base on what I choose. If I need flash light, I turn it on first before I take the photo. If I need LED light, I should be able to turn it on.

    2. Does the code work for normal conditions?

    In my case, my Panasonic tablet has LED light, First time when I click the button to check, it show no support for both. When I click the button again, it threw an exception. It should show no support message but it did not.

    will this code work ok on other brands?

     

    Thanks

    Thursday, October 30, 2014 8:16 AM
  • I need help on this. I am still not able to solve.

    Now I am using Lenonvo Miix 2  windows 8.1 pro

    I tested the Microsoft Cam App preloaded in this tablet.  The flash or camera light is working.

    But this code is not working. I Please help.

    using Windows.Media.Capture;
    using Windows.Media.Devices;

    Windows.Media.Capture.MediaCapture MyMediaCapture;

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
         MyMediaCapture = new MediaCapture();
         await MyMediaCapture.InitializeAsync();
    }

    private async void CamFlash_Click(object sender, RoutedEventArgs e)
     {
          bool ShowError = false;
           System.Exception myexception = new Exception();

          try
          {     

           var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;               
                                        
           var MyTorch = MyVideoDeviceController.TorchControl;
           var MyFlash = MyVideoDeviceController.FlashControl;


           if (MyTorch.Supported)
           {
               MyTorch.PowerPercent = 100;
               MyTorch.Enabled = true;

            }
           else
           {

             if (MyFlash.Supported)
             {
                MyFlash.PowerPercent = 100;
                MyFlash.Enabled = true;
              }
             else
              {
                //TorchButton.Content = "Torch not supported!";
                 var mymsgbox = new Windows.UI.Popups.MessageDialog("No Flash and Torch Support", "Flash and Torch");
                await mymsgbox.ShowAsync();
               }

             }             
            }
            catch (Exception ex)
            {
             ShowError = true;
             myexception = ex;

            }
            if(ShowError)
            {

            var mymsgbox = new Windows.UI.Popups.MessageDialog("encountered error: " + myexception, "Checking Flash and Torch");
                 await mymsgbox.ShowAsync();              
               
            }

    Wednesday, November 05, 2014 7:40 AM
  • This is solved for Lenovo Tablet.

    Just setup the camera:

    await m_mediaCaptureMgr.InitializeAsync(
                           new MediaCaptureInitializationSettings
                           {
                               VideoDeviceId = devices[1].Id
                           });

    Wednesday, November 05, 2014 9:54 AM