0

I making an augmented reality app and i need to take picture from camera and overlay 3d model over it.

I am already can take screenshot of gl view with 3d logo, but i can't figure out how to take image from camera.

How too take picture from camera?

1 Answers1

0

If you mean to display live video stream form camera you can use GPUImage.

If you only need to take a still image, use AVFoundation's AVCaptureStillImageOutput. See AVCam - Apple's sample code where you can strip the part of previewing live video (AVCaptureVideoPreviewLayer) and just capture a still image when you need it.

//you'll need to create an AVCaptureSession

_session = [[AVCaptureSession alloc] init];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//there are steps here where you adjust capture device if needed

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

if ([device supportsAVCaptureSessionPreset: AVCaptureSessionPreset640x480]) {
    _session.sessionPreset = AVCaptureSessionPreset640x480;
}

_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[_stillImageOutput setOutputSettings:outputSettings];
[outputSettings release];

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections) {
   for (AVCaptureInputPort *port in [connection inputPorts]) {
       if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
            videoConnection = connection;
           break;
       }
   }
   if (videoConnection) { break; }
}

[_session addOutput: _stillImageOutput];

[_session startRunning];

This code is used to take photo:

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections)
{
    for (AVCaptureInputPort *port in [connection inputPorts])
    {
        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
        {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

    if (imageSampleBuffer != NULL) {
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        UIImage *image = [UIImage imageWithData: imageData];
        //do something with image or data
    }
}

Hope it helps.

Dunja Lalic
  • 752
  • 13
  • 26
  • taking this error on your code: erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inactive/invalid connection passed.' – user1188620 May 23 '12 at 08:20
  • I didn't want to post complete code because it's huge and may be confusing. Take a look at Apple's sample code and strip everything from there you don't need. – Dunja Lalic May 23 '12 at 15:43