ios - 在 iOS 中使用 AVFoundation Framework 来自相机的视频帧?

标签 ios objective-c avfoundation video-processing

我从未使用过 AVFoundation Framework,我想从后置摄像头获取视频帧并处理这些帧。任何人帮助我,您的经验将不胜感激。谢谢

最佳答案

您可以使用以下代码启动与 AVFoundation 的相机 session 以捕获静态图像:

AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;

session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if ([session canAddInput:deviceInput]) {
    [session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.frameForCapture.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];

[session startRunning];

然后,为了实际捕获图像,您可以使用带有以下代码的按钮:

- (IBAction)takePhoto:(id)sender {
    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 imageDataSampleBuffer, NSError *error) {
                                                      if (imageDataSampleBuffer != NULL) {
                                                          NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                          UIImage *image = [UIImage imageWithData:imageData];
                                                      }
                                                  }];
}

然后,您可以对保存的图像做任何您想做的事情。

关于ios - 在 iOS 中使用 AVFoundation Framework 来自相机的视频帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670924/

相关文章:

ios - 无法在我的音乐播放器应用程序上播放音乐

swift/Spritekit : Play music through specific scenes

iphone - 在 GDB 中访问静态方法

ios - 使用 iOS 将图像上传到在线存储的最佳(或最佳)做法是什么?

ios - 无法锁定和解锁屏幕方向

objective-c - Objective-C 是否具有用于协议(protocol)消息转发的运行时方法?

iphone - 如何创建和使用大于屏幕分辨率的图像?

ios - 如何简单地删除旧的核心数据并重建新的?

ios - UIImagePickerController 预览大小与实时大小不一样

ios - Xcode iOS : How to reset a sound once it has played using AVFoundation