Translate

Tuesday, April 17, 2012

Writing a Flashlight Application using the iPhone LED

With the introduction of the iPhone 4, Apple has added an LED alongside the camera. It’s amazing how bright this little bugger is, ideal for a flashlight!

Since the release of the iPhone 4 I’ve been meaning to take a few minutes and write a flashlight application using the LED. My understanding from what I’ve gleaned from the API’s is that in order to turn on the LED, we need to essentially go through the same steps we would to capture video – if you know of a quicker and/or simpler process, please post some code below.
iPhone Flashlight UI

The user interface of this app is about as simple as they get – nothing more than a button to toggle the flashlight on and off.

The code for the UI is equally as trivial, in loadView, check if the device has a torch and if so, add a button to toggle the flashlight state.

- (void)loadView
{
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

// If torch supported, add button to toggle flashlight on/off
if ([device hasTorch] == YES)
{
flashlightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, 320, 98)];
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOn.png"] forState:UIControlStateNormal];
[flashlightButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];

[[self view] addSubview:flashlightButton];
}
}

Toggling the Flashlight On and Off

The event management code for this app is nothing more than swapping the button background image to reflect the current state of the flashlight followed by a call to the code that will toggle the LED on and off.

- (void)buttonPressed:(UIButton *)button
{
if (button == flashlightButton)
{
if (flashlightOn == NO)
{
flashlightOn = YES;
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOff.png"] forState:UIControlStateNormal];
}
else
{
flashlightOn = NO;
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOn.png"] forState:UIControlStateNormal];
}

[self toggleFlashlight];
}
}

AVCaptureSession Configuration

There are a few objects that we’ll need to interact with to get everything working. AVCaptureDevice is the object we’ll use to represent the physical video device. An AVCaptureSession object will manage the flow of data. And finally, AVCaptureDeviceInput will represent the video data.

With the objects above defined, we configure the AV session to set the torch on and start the session running…

- (void)toggleFlashlight
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if (device.torchMode == AVCaptureTorchModeOff)
{
// Create an AV session
AVCaptureSession *session = [[AVCaptureSession alloc] init];

// Create device input and add to current session
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
[session addInput:input];

// Create video output and add to current session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];

// Start session configuration
[session beginConfiguration];
[device lockForConfiguration:nil];

// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];

[device unlockForConfiguration];
[session commitConfiguration];

// Start the session
[session startRunning];

// Keep the session around
[self setAVSession:session];

[output release];
}
else
{
[AVSession stopRunning];
[AVSession release], AVSession = nil;
}
}

Caveats

Stating the obvious, this application will only work on iPhone 4+ devices. Also, this application works on only on a device – you will receive compile time errors regarding AVCaptureSession if you attempt to build the app for the simulator.

One last thought – chances are running the LED for any length of time may give your battery a pretty intense workout. It may be worthwhile to keep an eye on your battery indicator if you plan to turn the light on for more than a few minutes.
iPhone LED Flashlight- Xcode Project

Here is the link to download the source code for the iPhone LED Flashlight application. iosdevelopertips.com/wp-content/uploads/2010/12/flashlightApp.zip

No comments:

Post a Comment