×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Oslo, Norway
    Posts
    7

    An easy way to turn flash on and off?

    Hi

    Anyone know how to turn the phone flash on and off like a flashlight?

    The only example I currently have found is this: http://www.locked.nl/wp7-flashlight-getting-started

    And this example doesn't make the flash stay on constantly and the whole example is a bit complicated.

    Thanks for replies!

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: An easy way to turn flash on and off?

    Hello,

    As far as I know there is no way you can directly access and turn on/off the flash programmatically you need to do it with a workaround which is stated in your example, and the link you provided does show a way to maintain it constantly and it's by using the VideoCamera approach (not the Photo Camera).

    hope it helps,
    Yassine

  3. #3
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    434

    Re: An easy way to turn flash on and off?

    For WP7 unfortunately the complicated way you linked to is the only way to do it. With WP8 it's alot easier.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    Oslo, Norway
    Posts
    7

    Re: An easy way to turn flash on and off?

    Quote Originally Posted by SB Dev View Post
    For WP7 unfortunately the complicated way you linked to is the only way to do it. With WP8 it's alot easier.
    Great! I'm developing on WP8. How can I do this in an easy way?

  5. #5
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    434

    Re: An easy way to turn flash on and off?

    Basically you only need to instantiate an AudioVideoCaptureDevice and use this code to activate the Torch:

    Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

    To show the whole thing in a real life scenario I have removed all unnecessary code from the Camera class I use in one of my projects to only include what's needed for the Torch functionality. I have not tested it after removing the unnecessary code so there might issues with the initialize method but basically you should be able to just call initialize on that class and then activate/deactivate the torch (flash led) using the according methods.

    Code:
    internal class Camera : IDisposable
        {
            private static Camera sInstance;
            private AudioVideoCaptureDevice mDevice;
            private Windows.Foundation.Size mCaptureResolution;
            private bool mCapturing;
    
            public static bool SupportsTorch
            {
                get
                {
                    //value 2 equals VideoTorchMode.On
                    return AudioVideoCaptureDevice.GetSupportedPropertyValues(CameraSensorLocation.Back, KnownCameraAudioVideoProperties.VideoTorchMode).Contains(2u);
                }
            }
    
            public static Camera Instance
            {
                get {
                    if (sInstance == null)
                    {
                        throw new InvalidOperationException("Camera Instance can only be used after it has been initialized.");
                    }
                    else
                    {
                        return sInstance;
                    }
                }
            }
    
            public static void DisposeInstance()
            {
                if (sInstance != null)
                {
                    sInstance.Dispose();
                    sInstance = null;
                }
            }
    
            public AudioVideoCaptureDevice Device
            {
                get { return mDevice; }
            }
    
            public static bool IsInitialized
            {
                get { return sInstance != null; }
            }
    
            public bool TorchActive
            {
                get;
                private set;
            }
    
            private Camera(AudioVideoCaptureDevice device, Windows.Foundation.Size resolution)
            {
                if (device == null)
                {
                    throw new ArgumentNullException("device", "Parameter 'device' must not be null.");
                }
                mDevice = device;
                mCapturing = false;
                mCaptureResolution = resolution;
                TorchActive = false;
            }
    
            public static async Task Initialize()
            {
                List<Windows.Foundation.Size> list = new List<Windows.Foundation.Size>();
                foreach (Windows.Foundation.Size s in AudioVideoCaptureDevice.GetAvailablePreviewResolutions(CameraSensorLocation.Back))
                {
                    list.Add(s);
                    App.AddToCameraLog(String.Format("{0} x {1}", s.Width, s.Height));
                }
                Windows.Foundation.Size captRes = list.Where(p => p.Width == list.Max(q => q.Width)).FirstOrDefault();
    
                AudioVideoCaptureDevice d = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(CameraSensorLocation.Back, captRes);
    
                sInstance = new Camera(d, captRes);
            }
    
            public bool ActivateTorch()
            {
                if (SupportsTorch)
                {
                    Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
                    Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, AudioVideoCaptureDevice.GetSupportedPropertyRange(CameraSensorLocation.Back, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
                    TorchActive = true;
                }
                return SupportsTorch;
            }
    
            public bool DeactivateTorch()
            {
                if (SupportsTorch)
                {
                    Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
                    TorchActive = false;
                }
                return SupportsTorch;
            }
    
            public void Dispose()
            {
                this.mDevice.Dispose();
                App.AddToCameraLog("Disposed of CameraCaptureDevice");
            }
        }

Similar Threads

  1. How to turn the camera flash light always on?
    By haroldf in forum Symbian
    Replies: 5
    Last Post: 2010-12-09, 06:20
  2. How to turn camera led flash on/off programmatically
    By wiikka in forum General Development Questions
    Replies: 4
    Last Post: 2009-04-01, 23:49
  3. How to turn off the flash in N73 during snapshot?
    By blisspark in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2007-09-24, 15:47
  4. creating SWF Launcher for Flash Lite is easy now
    By chall3ng3r in forum Symbian
    Replies: 2
    Last Post: 2007-03-16, 13:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •