Thursday, October 4, 2012

Camera integration in iPhone

- Rashid Khaleefa
Blog on how to integrate camera into iPhone application.
You can easily include camera feature into any ios application provided the device has a camera support, and you make use of the imagePickerController of apple.

Camera integration:
You have to create a secondary view controller for managing the overlap view to the camera.
This customises the way how the camera screen should apper once camera is initiated from your application.
Use the apples imagepickerController to pic the image snaped.
Creating overlayViewController
  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;
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;

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

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,
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 for overlayViewController

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 sourcetype the syntax is as below

[self showImagePicker:UIImagePickerControllerSourceTypeCamera];

Thats it now include the delegates of overlay 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 taken, you get the picture you snapped 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 snapped. 


Mansi Rao said...

Teradata Training in Hyderabad and Bangalore with live projects by experts, will help you with strong foundation with terada operations.

Lathika Honey said...

It's A Great Pleasure reading your Article, learned a lot of new things, we have to keep on updating it learn salesforce online Thanks for posting.

Richard Majece said...

It's really interesting that I found info when I had to write my cover letter. Now I can use it every time in order to get a high grade.