Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top