Click here to Skip to main content
Licence CPOL
First Posted 31 Jan 2012
Views 8,274
Downloads 657
Bookmarked 9 times

Android + iOS Torch sample

By | 31 Jan 2012 | Article
Samples for present Android and iOS to control torch.

Introduction

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

Background

I developed a cross-platform framework that named CloudBox.

In alpha version, I went to verify CloudBox, so I thought that I could develop some easy application.

The touch sample was first application developing by CloudBox.

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

Using the code

In Android, I implement a class named CloudLed to control camera.
public class CloudLed {
    boolean m_isOn;
    Camera m_Camera;
    
    public boolean getIsOn() { return m_isOn; }
    
    public CloudLed()
    {
        m_isOn = false;
    }
    
    public void turnOn()
    {
        if(!m_isOn)
        {
            m_isOn = true;
            try
            {
                m_Camera = Camera.open();
                Camera.Parameters mParameters;
                mParameters = m_Camera.getParameters();
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                m_Camera.setParameters(mParameters);
            }catch(Exception ex){}
        }
    }
    
    public void turnOff()
    {
        if(m_isOn)
        {
            m_isOn = false;
            try
            {
                Camera.Parameters mParameters;
                mParameters = m_Camera.getParameters();
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                m_Camera.setParameters(mParameters);
                m_Camera.release();
            }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 feature, your Manifest should include the following:

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

In android develop, call Camera.open() to get the instance, then get parameters from the instance and modify.

The Camera.Parameters.FLASH_MODE_TORCH parameter can easy to set flash led to torch mode.

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

The sample 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 control touch.

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

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

Before control 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]; 

Using the code to turn off.

History

V1.0, 31 January, 2012- New Article.

License

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

About the Author

Cloud Hsu

Architect
LPI
Taiwan Taiwan

Member

Follow on Twitter Follow on Twitter
CloudBox cross-platform framework. (iOS+ Android)
My iPhone APP:
1. Toilet War
2. Ninja Darts
3. Fight Bingo

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 31 Jan 2012
Article Copyright 2012 by Cloud Hsu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid