[英]how to take camera shot in cocos3d
我制作了增強現實應用程序,需要從相機拍攝照片並將其覆蓋3D模型。
我已經可以拍攝帶有3d徽標的gl視圖的屏幕截圖,但是我不知道如何從相機中拍攝圖像。
怎么也從相機拍照?
如果您打算顯示來自攝像機的實時視頻流,則可以使用GPUImage 。
如果只需要拍攝靜止圖像,請使用AVFoundation的AVCaptureStillImageOutput。 請參閱AVCam-Apple的示例代碼 ,您可以在其中剝離預覽實時視頻的部分( 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
}
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.