Monday, October 1, 2012

Camera integtration in iPhone application

It has quite time since I have blogged something. Let me write how to access camera from an iPhone application and handle the delegats well to grab the image out of it. Its quite easy to include camera feature into any ios application provided the device has a camera support, and you make use of the imagepickerController of apple :p.
You can have a secondary view controller created for managing the overlap view to the camera.
In this class you define and declare the needed controls to apperar as a overlay to the camera, such as snap button cancel or done, cancel etc: This can be made used to customise the way how the camera screen should apper once camera is selected from your application.
You then have to use the apples imagepickerController to pic the image snaped.

Creating a overlayViewController includes the following.
  1. Declare the needed controlls in .h file
  2. Implement those in .m
  3. Include the UIImagePickerControllerDelegate
  4. Create needed delegates for your overlayViewController
A Sample overlay having buttons takepicture button and done button with actions takePhoto, close respectively for those. Also the delegate methods didTakePicture, the method where you get the snaped picture and didFinshWithCamera that can be used to perform any action once you are done with camera. 

The .h file of overlayViewController:

@protocol OverlayViewControllerDelegate;
@interface OverlayViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
id <OverlayViewControllerDelegate> delegate;
UIImagePickerController *imagePickerController;
@private
UIBarButtonItem *takePictureButton;
UIBarButtonItem *cancelButton;
}

@property (nonatomic, assign) id <OverlayViewControllerDelegate> delegate;
@property (nonatomic, retain) UIImagePickerController *imagePickerController;

@property (nonatomic, retain) IBOutlet UIBarButtonItem *takePictureButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton;

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType;
// camera page (overlay view)
- (IBAction)done:(id)sender;
- (IBAction)takePhoto:(id)sender;
@end

@protocol OverlayViewControllerDelegate
- (void)didTakePicture:(UIImage *)picture;
- (void)didFinishWithCamera;
@end

Now in you .m file. Initiate the imagePickerController and map its delegate

self.imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
self.imagePickerController.delegate = self;

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
NSLog(@"enters setupImagePicker");
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
// user wants to use the camera interface
//
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
{
// setup your custom overlay view for the camera
//
// ensure that your custom view's frame fits within the parent frame
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,
CGRectGetHeight(overlayViewFrame) -
self.view.frame.size.height - 10.0,
CGRectGetWidth(overlayViewFrame),
self.view.frame.size.height + 10.0);
self.view.frame = newFrame;
[self.imagePickerController.cameraOverlayView addSubview:self.view];
}
}
}

This calls the didFinishWithCamera delegats.
- (IBAction)done:(id)sender
{
[self.delegate didFinishWithCamera];
}

Action that maps takesPhoto. This just calls the takePicture of imgaePickerController which is responsible for initiating still image capture.

- (IBAction)takePhoto:(id)sender
{
[self.imagePickerController takePicture];
}

Also include the follwoing delegates of imgePicker which gets called when an image is taken by the camera.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
if (self.delegate)
[self.delegate didTakePicture:image];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.delegate didFinishWithCamera]; // tell your delegate we are finished with the picker
}

Thats all you do with your overlayController.

Now How to use this in your class. You have to now include the overlayController delegate and imgaPickerController in header of the class where you wish to inclue a button for a camera capture.
Now call the below function in the button action you need.

- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
NSLog(@"entered showImagePicker camera");
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
}
}

This checks for the sourcetype you give that should be a camera source type the syntax is as below

[self showImagePicker:UIImagePickerControllerSourceTypeCamera];

Thants it now include the delegates of ovrelay class that was created

- (void)didTakePicture:(UIImage *)picture
{
//do some thing with the picture.
}
- (void)didFinishWithCamera
{
[self dismissModalViewControllerAnimated:YES];
}

The didTakePicture tells that a picture was take, you get the picture you snaped in this delegate. And didFinishWithCamera tells to finish with the camera.
Thats it you get your camera to snap from your application and the image you snaped.

No comments:

Post a Comment