07-12-2011, 01:52 PM
#1 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
AVFoundation question
New problem scroll down 4 posts
CameraSessionManager.h
Code:
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreVideo/CoreVideo.h>
#import <AVFoundation/AVFoundation.h>
#import <QuartzCore/QuartzCore.h>
@interface CameraSessionManager : NSObject {
@private
AVCaptureSession *captureSession;
AVCaptureVideoPreviewLayer *previewLayer;
}
- (id)init;
- (void)addVideoInput;
- (void)addPreviewLayer;
@property (nonatomic,retain) AVCaptureSession *captureSession;
@property (nonatomic,retain) AVCaptureVideoPreviewLayer *previewLayer;
@end
;
CameraSessionManager.m
Code:
#import "CameraSessionManager.h"
@implementation CameraSessionManager
@synthesize captureSession,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 in refernce to its frame
[self.previewLayer release];
}
- (void)dealloc{
[self.captureSession stopRunning];
[self.captureSession release];
[self.previewLayer release];
[super dealloc];
}
CameraCaptureManager.h
Code:
#import <Foundation/Foundation.h>
#import "CameraSessionManager.h"
@interface CameraCaptureManager : CameraSessionManager<AVCaptureVideoDataOutputSampleBufferDelegate> {
@private
UIImage *frame;
AVCaptureStillImageOutput *captureStillImageOutput;
AVCaptureVideoDataOutput *captureVideoOutput;
}
- (id)init;
- (void)addVideoOutput;
- (void)addStillImageOutput;
- (void)captureImage;
- (UIImage*)returnFrame;
- (void)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer;
@property (nonatomic,retain) UIImage *frame;
@property (nonatomic,retain) AVCaptureStillImageOutput *captureStillImageOutput;
@property (nonatomic,retain) AVCaptureVideoDataOutput *captureVideoOutput;
@end
CameraCaptureManager.m
Code:
#import "CameraCaptureManager.h"
@implementation CameraCaptureManager
@synthesize frame;
@synthesize captureStillImageOutput,captureVideoOutput;
#pragma mark - Inilitializer
- (id)init{
self = [super init];
if(self){
self.frame = [[UIImage alloc]init];
[self addVideoOutput];
[self addStillImageOutput];
}
return self;
}
#pragma mark - Setup output
//GOAL:Sets up the videos output
- (void)addVideoOutput{
self.captureVideoOutput = [[AVCaptureVideoDataOutput alloc]init];//initialize video output
self.captureVideoOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[self.captureVideoOutput setAlwaysDiscardsLateVideoFrames:TRUE];
self.captureVideoOutput.minFrameDuration = CMTimeMake(1, 15);
if([self.captureSession canAddOutput:self.captureVideoOutput])//check if the capture session can add the output
[self.captureSession addOutput:self.captureVideoOutput];//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.captureVideoOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
[self.captureVideoOutput release];
}
- (void)addStillImageOutput{
self.captureStillImageOutput = [[AVCaptureStillImageOutput alloc]init];
[self.captureStillImageOutput setOutputSettings:[NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil]];
if([self.captureSession canAddOutput:self.captureStillImageOutput])
[self.captureSession addOutput:self.captureStillImageOutput];
else
NSLog(@"Capture Session could not add still image output");
[self.captureStillImageOutput release];
}
#pragma mark - Capturing Image
- (void)captureImage{
AVCaptureConnection *videoConnection = nil;
for(AVCaptureConnection *connection in captureStillImageOutput.connections){
for(AVCaptureInputPort *port in connection.inputPorts){
if([port.mediaType isEqual:AVMediaTypeVideo]){
videoConnection = connection;
break;
}
}
if(videoConnection)
break;
}
if(videoConnection)
[self.captureStillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if(!error)
[self imageFromSampleBuffer:imageDataSampleBuffer];
else
NSLog(@"%@",error);
}];
}
- (UIImage*)returnFrame{
return self.frame;
}
- (void)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
[self setFrame:image];
}
#pragma mark - AVCaptureVideoOutput Delegate
- (void)captureVideoOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
NSLog(@"Capturing Video Frames");
}
#pragma mark - Memory Management
- (void)dealloc{
[self.captureVideoOutput release];self.captureVideoOutput = nil;
[self.captureStillImageOutput release];self.captureStillImageOutput = nil;
[self.frame release];self.frame = nil;
[super dealloc];
}
@end
;
ViewController.h
Code:
#import <UIKit/UIKit.h>
#import "CameraCaptureManager.h"
@interface CameraEdgeDetectionViewController : UIViewController {
UIImageView *captureImageView;
CameraCaptureManager *sessionManager;
}
- (id)init;
- (void)setUpCameraSession;
- (void)takePicture;
- (void)updateImageView;
@property (nonatomic,retain) UIImageView *captureImageView;
@property (nonatomic,retain) CameraCaptureManager *sessionManager;
@end
ViewController.m
Code:
#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];
}
- (void)viewDidAppear:(BOOL)animated{
self.captureImageView = [[[UIImageView alloc]init]retain];
[self.captureImageView setFrame:CGRectMake(30, 30, 50, 50)];
[self.captureImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:self.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 (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
self.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];
[self.sessionManager release];
}
//GOAL:When the user taps the button on the screen this update the imageview to the image taken
- (void)takePicture{
[self.sessionManager captureImage];
[self updateImageView];
}
- (void)updateImageView{
[self.captureImageView setImage:[self.sessionManager returnFrame]];
}
#pragma mark - Memory
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (void)dealloc{
[self.captureImageView release];self.captureImageView = nil;
[self.sessionManager release];self.sessionManager = nil;
[super dealloc];
}
@end
Last edited by Esko2300; 07-13-2011 at 11:18 AM .
07-12-2011, 02:57 PM
#2 (permalink )
Senior Member
iPhone Dev SDK Supporter
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
It would be helpful to know what the error message is when your app crashes.
07-12-2011, 03:07 PM
#3 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Im getting a bad access error when i press the button that calls my take picture method. So im guessing it has something to do with the UIIMageView on the view controller. The console just says bad access and it stops at this line
Code:
- (void)takePicture{
[self.captureImageView setImage:[self.sessionManager captureImage]];//this is where the bad access is happening
}
;
07-12-2011, 03:14 PM
#4 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Ok i got it turns out that the frame object in my Camera Capture class wasent always being called like self.frame. So it was causing the Bad Access Error I just went in and changed all the frames to self.frame and its working fine now thanks
07-13-2011, 11:14 AM
#5 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
OK im back here on this thread because i have yet another problem ill edit all the code up above after i write this. What happens is the code runs.
When i take a picture of a shirt using the button, i hear the camera click. Then i look to see what pops up in the captureImageView of my view controller but nothing shows. Then i go and take a picture of a cup what happens now is the picture of the shirt is showing in the imageview. Then i take a picture of a pen, but the pixture of the cup shows itself in the imageview. So what im seeing is that its one frame behind and i have no idea where to even start looking for the bug since this is only my second day playing around with this framework. Can anyone see why this is happening?
07-13-2011, 02:47 PM
#6 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
I think i found the problem but i dont know how to fix it. Turns out this function
Code:
- (void)takePicture{
[self.sessionManager captureImage];
[self updateImageView];
}
is being ran but it dosent wait for the first function to run in order to start the next function. I tried using dispatch_sync() in order to make it run the first function first but to no luck its not working. Hope this helps you help me.
07-13-2011, 04:03 PM
#7 (permalink )
Reading the Documentation
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
Why not set up a notification in CameraCaptureManager to notify the caller class when the image has been taken, and then when you receive the notification you can call the updateImageView method.
07-13-2011, 04:23 PM
#8 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Quote:
Originally Posted by
baja_yu
Why not set up a notification in CameraCaptureManager to notify the caller class when the image has been taken, and then when you receive the notification you can call the updateImageView method.
Smart idea i never used them b4 but nows the time thanks ill check it out
07-14-2011, 09:36 AM
#9 (permalink )
Awesome
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Worked perfectly thanks a lot
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 372
9 members and 363 guests
apatsufas , chemistry , Kirkout , leostc , lzwasyc , MarkC , Sami Gh , SamorodovAlex , VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80