Created
February 28, 2012 15:57
-
-
Save anonymous/1933313 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "CaptureSessionManager.h" | |
| @implementation CaptureSessionManager | |
| @synthesize captureSession; | |
| @synthesize previewLayer; | |
| @synthesize stillImage, stillImageOutput; | |
| #pragma mark Capture Session Configuration | |
| - (id)init { | |
| if ((self = [super init])) { | |
| self.captureSession = [[[AVCaptureSession alloc] init] autorelease]; | |
| if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) | |
| self.captureSession.sessionPreset = AVCaptureSessionPreset640x480; | |
| } | |
| return self; | |
| } | |
| - (void)addVideoInput | |
| { | |
| AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
| if (videoDevice) { | |
| NSError *error; | |
| AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; | |
| if (!error && [self.captureSession canAddInput:videoIn]) | |
| [self.captureSession addInput:videoIn]; | |
| } | |
| else | |
| NSLog(@"ERROR: Couldn't create video capture device"); | |
| } | |
| - (void)addVideoPreviewLayer | |
| { | |
| self.previewLayer = [[[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession] autorelease]; | |
| [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; | |
| } | |
| - (void)addStillImageOutput | |
| { | |
| self.stillImageOutput = [[[AVCaptureStillImageOutput alloc] init] autorelease]; | |
| NSDictionary *outputSettings = [[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil] autorelease]; | |
| [self.stillImageOutput setOutputSettings:outputSettings]; | |
| if ([self.captureSession canAddOutput:self.stillImageOutput]) | |
| [self.captureSession addOutput:self.stillImageOutput]; | |
| else | |
| NSLog(@"ERROR: Could not create video output"); | |
| } | |
| - (AVCaptureConnection*)getConnection | |
| { | |
| for (AVCaptureConnection *connection in self.stillImageOutput.connections) | |
| for (AVCaptureInputPort *port in [connection inputPorts]) | |
| if ([[port mediaType] isEqual:AVMediaTypeVideo]) | |
| return connection; | |
| NSLog(@"ERROR: Could not find connection"); | |
| return nil; | |
| } | |
| - (void)captureStillImage | |
| { | |
| [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self getConnection] | |
| completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) | |
| { | |
| CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); | |
| if (exifAttachments) | |
| NSLog(@"EXIF: %@", exifAttachments); | |
| else | |
| NSLog(@"no EXIF"); | |
| NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; | |
| UIImage *image = [[UIImage alloc] initWithData:imageData]; | |
| self.stillImage = image; | |
| [image release]; | |
| [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil]; | |
| }]; | |
| } | |
| - (void)dealloc { | |
| [self.captureSession stopRunning]; | |
| [previewLayer release]; | |
| [captureSession release]; | |
| [super dealloc]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment