2012-05-22 30 views
0

我制作的是增强现实应用程序,我需要从相机拍摄照片并在其上覆盖3d模型。如何在cocos3d中拍摄相机

我已经可以采取带有3d标志的gl视图截图,但我无法弄清楚如何从相机拍摄图像。

如何从相机拍照?

回答

0

如果您的意思是要显示实时视频流形式的相机,你可以使用GPUImage

如果您只需拍摄静止图像,请使用AVFoundation的AVCaptureStillImageOutput。请参阅AVCam - Apple's sample code,您可以从中删除预览实况视频(AVCaptureVideoPreviewLayer)的部分内容,并在需要时捕获静止图像。

//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]; 

这段代码是用来拍照:

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 
    } 
} 

希望它能帮助。

+0

在您的代码上发生此错误:由于未捕获异常'NSInvalidArgumentException'导致应用程序失败,原因:'*** - [AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - 无效/无效连接已通过。 – user1188620

+0

我不想发布完整的代码,因为它很庞大,可能会令人困惑。看看苹果的示例代码,并从那里除去所有你不需要的东西。 –

+0

是的,它是工作谢谢! – user1188620