It looks like you're new here. If you want to get involved, click one of these buttons!
#import <Foundation/Foundation.h>
#import <CoreVideo/CoreVideo.h>
#import <AVFoundation/AVFoundation.h>
@interface CameraSessionManager : NSObject {
AVCaptureSession *captureSession;
AVCaptureVideoPreviewLayer *previewLayer;
AVCaptureVideoDataOutput *captureOutput;
}
- (id)init;
- (void)addVideoInput;
- (void)addPreviewLayer;
@property (nonatomic,retain) AVCaptureSession *captureSession;
@property (nonatomic,retain) AVCaptureVideoPreviewLayer *previewLayer;
@property (nonatomic,retain) AVCaptureVideoDataOutput *captureOutput;
@end
#import \"CameraSessionManager.h\"
@implementation CameraSessionManager
@synthesize captureSession, captureOutput,previewLayer;
- (id)init{
self = [super init];
if(self){
[self addVideoInput];
[self addPreviewLayer];
}
return self;
}
//GOAL:Sets up the videos input
- (void)addVideoInput{
self.captureSession = [[AVCaptureSession alloc]init];//initialize capture session
[self.captureSession setSessionPreset:AVCaptureSessionPresetMedium];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];//set up a capture device with Media Type Video
if(device){
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if(!error){
if([self.captureSession canAddInput:videoIn])
[self.captureSession addInput:videoIn];
else
NSLog(@\"Cannot add video input\");
}
else
NSLog(@\"Couldent create video input\");
}
else
NSLog(@\"Couldent create video capture device\");
[self.captureSession release];
}
//GOAL:Sets up the preview layer
- (void)addPreviewLayer{
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];//initialize the preview with the capture session
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];//set the previews layer view size
[self.previewLayer release];
}
- (void)dealloc{
[self.captureSession stopRunning];
[self.captureSession release];
[self.previewLayer release];
[self.captureOutput release];
[super dealloc];
}
#import <Foundation/Foundation.h>
#import \"CameraSessionManager.h\"
@interface CameraCaptureManager : CameraSessionManager<AVCaptureVideoDataOutputSampleBufferDelegate> {
@private
UIImage *frame;
AVCaptureStillImageOutput *captureStillImage;
}
- (id)init;
- (void)addVideoOutput;
- (void)addStillImageOutput;
- (UIImage*)captureImage;
- (UIImage*)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer;
@property (nonatomic,retain) UIImage *frame;
@property (nonatomic,retain) AVCaptureStillImageOutput *captureStillImage;
@end
#import \"CameraCaptureManager.h\"
@implementation CameraCaptureManager
@synthesize frame;
@synthesize captureStillImage;
- (id)init{
self = [super init];
if(self){
frame = [[UIImage alloc]init];
[self addVideoOutput];
[self addStillImageOutput];
}
return self;
}
//GOAL:Sets up the videos output
- (void)addVideoOutput{
self.captureOutput = [[AVCaptureVideoDataOutput alloc]init];//initialize video output
self.captureOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:@\"kCVPixelBufferPixelFormatTypeKey\"];
self.captureOutput.minFrameDuration = CMTimeMake(1, 15);
if([self.captureSession canAddOutput:self.captureOutput])//check if the capture session can add the output
[self.captureSession addOutput:self.captureOutput];//add the output to the capture session
else
NSLog(@\"Capture Session can't add output\");
dispatch_queue_t queue = dispatch_queue_create(\"Sample Delegate Queue\", NULL);
[self.captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
[self.captureOutput release];
}
- (void)addStillImageOutput{
self.captureStillImage = [[AVCaptureStillImageOutput alloc]init];
[self.captureStillImage setOutputSettings:[NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]];
if([self.captureSession canAddOutput:self.captureStillImage])
[self.captureSession addOutput:self.captureStillImage];
else
NSLog(@\"Capture Session could not add still image output\");
[self.captureStillImage release];
}
- (UIImage*)captureImage{
AVCaptureConnection *videoConnection = nil;
for(AVCaptureConnection *connection in captureStillImage.connections){
for(AVCaptureInputPort *port in connection.inputPorts){
if([port.mediaType isEqual:AVMediaTypeVideo]){
videoConnection = connection;
break;
}
}
if(videoConnection){
break;
}
}
[self.captureStillImage captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
frame = [self imageFromSampleBuffer:imageDataSampleBuffer];
}];
return frame;
}
- (UIImage*)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
}
- (void)dealloc{
[frame release];frame = nil;
[super dealloc];
}
#import <UIKit/UIKit.h>
#import \"CameraCaptureManager.h\"
@interface CameraEdgeDetectionViewController : UIViewController {
UIImageView *captureImageView;
CameraCaptureManager *sessionManager;
}
- (id)init;
- (void)setUpCameraSession;
- (void)takePicture;
@property (nonatomic,retain) UIImageView *captureImageView;
@property (nonatomic,retain) CameraCaptureManager *sessionManager;
@end
#import \"CameraEdgeDetectionViewController.h\"
@implementation CameraEdgeDetectionViewController
@synthesize sessionManager;
@synthesize captureImageView;
#pragma mark - Initializers
- (id)init{
self = [super init];
if(self){
[self setUpCameraSession];
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)viewDidUnload{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewDidAppear:(BOOL)animated{
captureImageView = [[UIImageView alloc]init];
[captureImageView setFrame:CGRectMake(30, 30, 50, 50)];
[self.view addSubview:captureImageView];
UIButton *captureButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[captureButton setFrame:CGRectMake(30, 90, 50, 50)];
[captureButton addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:captureButton];
[super viewDidAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Camera Session
//GOAL:Sets up the camera session and adds the preview layer to the screen
- (void)setUpCameraSession{
//initialize the session manager
sessionManager = [[CameraCaptureManager alloc]init];
//create Previews layer rect and position
CGRect layerRect = self.view.layer.bounds;
CGPoint layerPosition = CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect));
//set the previews layer and rect
[self.sessionManager.previewLayer setFrame:layerRect];
[self.sessionManager.previewLayer setPosition:layerPosition];
//add preview layer to views layer
[self.view.layer addSublayer:self.sessionManager.previewLayer];
//start capture session
[self.sessionManager.captureSession startRunning];
}
//GOAL:When the user taps the button on the screen this update the imageview to the image taken
- (void)takePicture{
[self.captureImageView setImage:[self.sessionManager captureImage]];
}
#pragma mark - Memory
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (void)dealloc{
[self.captureImageView release];self.captureImageView = nil;
[self.sessionManager release];self.sessionManager = nil;
[super dealloc];
}