0

我制作了一个增强现实应用程序,我需要从相机拍照并在其上覆盖 3d 模型。

我已经可以使用 3d 徽标截取 gl view 的屏幕截图,但我不知道如何从相机拍摄图像。

如何从相机拍照?

4

1 回答 1

0

如果您要显示实时视频流形式的相机,您可以使用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
    }
}

希望能帮助到你。

于 2012-05-23T04:15:08.820 回答