Click here to Skip to main content
15,609,332 members
Articles / Mobile Apps / Android
Posted 31 Jan 2012


23 bookmarked

Android + iOS Torch sample

Rate me:
Please Sign up or sign in to vote.
3.91/5 (10 votes)
12 Dec 2013CPOL2 min read
Samples for present Android and iOS to control torch.


These are simple samples to present how to control touch in iOS and Android.


I developed a cross-platform framework named CloudBox. In the alpha version, I went to verify CloudBox, so I thought I could develop an easy application. The touch sample was the first application developed using CloudBox.

But CloudBox is not ready to open, so these samples are normal Android and iOS samples.

Using the code

In Android, I implemented a class named CloudLed to control the camera.

public class CloudLed {
    boolean m_isOn;
    Camera m_Camera;
    public boolean getIsOn() { return m_isOn; }
    public CloudLed()
        m_isOn = false;
    public void turnOn()
            m_isOn = true;
                m_Camera =;
                Camera.Parameters mParameters;
                mParameters = m_Camera.getParameters();
            }catch(Exception ex){}
    public void turnOff()
            m_isOn = false;
                Camera.Parameters mParameters;
                mParameters = m_Camera.getParameters();
            }catch(Exception ex){}

The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.

To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also, be sure to include the <uses-feature> manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus features, your Manifest should include the following:

<uses-permission android:name="android.permission.CAMERA" />

In Android development, call to get the instance, then get the parameters from the instance and modify them.

The Camera.Parameters.FLASH_MODE_TORCH parameter can easily set the flash LED to torch mode.

Remember to call release() to release the camera when the program is onPause.

The sample was already tested in Samsung Galaxy S2.

#import <AVFoundation/AVFoundation.h> 
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: AVCaptureTorchModeOn];
        [device unlockForConfiguration];

In iOS, it is also to easy to control touch.

[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] will return the default device used to capture the data of a given media type.

In the sample, call hasTorch to check if the device supports touch or not.

Before controlling touch, we must call lockForConfiguration to attempt to acquire a lock on the capture device.

And call unlockForConfiguration to relinquish a lock on a device.

[device setTorchMode: AVCaptureTorchModeOff]; 

Use the code to turn off.


  • V1.0, 31 January, 2012- New article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect SIS
Taiwan Taiwan
CloudBox cross-platform framework. (iOS+ Android)
Github: cloudhsu
1. Super Baby Pig (iOS+Android)
2. God Lotto (iOS+Android)
2. Ninja Darts (iOS)
3. Fight Bingo (iOS)

Comments and Discussions

GeneralMy vote of 2 Pin
KarstenK27-Nov-14 0:26
mveKarstenK27-Nov-14 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.