拡張現実アプリを作成しています。カメラから写真を撮り、その上に 3D モデルを重ねる必要があります。
私はすでに 3D ロゴ付きの gl ビューのスクリーンショットを撮ることができますが、カメラから画像を取得する方法がわかりません。
カメラから写真を撮る方法は?
拡張現実アプリを作成しています。カメラから写真を撮り、その上に 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
}
}
それが役に立てば幸い。