I am creating an app in which user takes the picture of a image with text and upload to server. I have used AVCaptureSession
to open camera and placed a bar button which captures the latest frame and uploads it to the server. In this app, user can send multiple images to the server one by one by clicking the bar button.
I was wondering if it was possible that user does not have to press the button to capture a frame. Is this possible that current frame is captured automatically with auto focus? Like user just places the camera for a second on the image and when the image is well-focused, it is automatically captured so that user can move to next image without having to press any button?
My code to capture frame is as follows:
- (void)initCapture {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
error:nil];
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES;
/*We create a serial queue to handle the processing of our frames*/
dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
// Set the video output to store frame in BGRA (It is supposed to be faster)
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];
/*And we create a capture session*/
captureSession = [[AVCaptureSession alloc] init];
/*We add input and output*/
[captureSession addInput:captureInput];
[captureSession addOutput:captureOutput];
AVCaptureConnection *conn = [captureOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
conn.videoMinFrameDuration = CMTimeMake(5,1);
if (conn.supportsVideoMaxFrameDuration)
conn.videoMaxFrameDuration = CMTimeMake(5,1);
[captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
customLayer = [CALayer layer];
customLayer.frame = self.view.bounds;
customLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
customLayer.contentsGravity = kCAGravityResizeAspectFill;
//[self.view.layer addSublayer:customLayer];
/*We add the imageView*/
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 100, 100);
//imageView.frame = self.view.bounds;
[self.view addSubview:imageView];
[captureSession startRunning];
}
I will be grateful for your help. Thanks.
You have to add
adjustingFocus
observer to your video input:and then add the
observeValueForKeyPath
method that takes a photo when adjusting focus is finished: