---页首---

如何使用AVFoundation从相机捕获视频帧生成图像

如何使用AVFoundation从相机捕获视频帧生成图像

    1. 实时捕获,第一步通过实例化AVCaptureSession对象创建一个捕获会话。使用AVCaptureSession对象去协调从输入设备到输出数据流。
    1. 通过实例化一个AVCaptureDeviceInput对象创建一个输入数据源提供视频数据给会话,调用addInput添加到会话中
    1. 实例化一个AVCaptureVideoDataOutput对象创建一个输出,并添加到会话中

AVCaptureVideoDataOutput用来处理来自视频中捕获的未压缩的帧。AVCaptureVideoDataOutput实例可以生成使用其它多媒体API处理的视频帧。可以通过captureOutput:didOutputSampleBuffer:fromConnection:代理方法访问到帧。用setSampleBufferDelegate:queue设置取样缓冲区代理和回调的队列。AVCaptureVideoDataOutputSampleBuffer对象必须遵守AVCaptureVideoDataOutputSampleBufferDelegate协议。用sessionPreset属性自定义输出的质量。调用会话startRunning方法启动从输入到输出的数据流,调用stopRunning方法停止数据流。

#import "TestViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface TestViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
{
    AVCaptureSession *_session;
}
@end
@implementation TestViewController
// 创建和配置捕获会话,并启动运行
- (void)setupCaptureSession {
    NSError *error = nil;
    // 创建会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    // 如果你的处理算法可以处理,配置会话生成低分辨率的视频帧。
    // 此处我们将为选择的设备指定中等质量
    session.sessionPreset = AVCaptureSessionPresetMedium;
    // 获取合适的AVCaptureDevice
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    // 通过device创建一个输入并添加到会话
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // 处理错误
        return;
    }
    [session addInput:input];
    // 创建一个输出并添加到会话
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    // 配置output
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    [output setSampleBufferDelegate:self queue:queue];
    // 指定像素格式
    output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    // 如果想指定一个帧率
    output.minFrameDuration = CMTimeMake(1, 15);
    // 启动会话,开始数据流
    [session startRunning];
    // 保存会话
    _session = session;
}

// 代理方法,当有取样缓冲区写入时被调用
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    // 从取样缓冲区数据创建一个UIImage
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
    // 其它处理
}

- (UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    // 从多媒体数据中获取一个 CMSampleBuffer 的 Core Video 图像
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // 锁定像素缓冲区的基址
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    // 获取基址
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
    // 从像素缓冲区获取每行的字节数
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // 获取像素缓冲区的宽和高
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    // 创建一个设备相关的RGB颜色空间
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 用取样缓冲区数据创建一个位图图形上下文
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // 从位图图形上下文中的像素数据创建一个Quartz图像
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    // 解锁像素缓冲区
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    // 用Quartz图像创建一个图像
    UIImage *image = [UIImage imageWithCGImage:quartzImage];
    CGImageRelease(quartzImage);
    return image;
}
posted @ 2020-05-12 17:13  20190311  阅读(542)  评论(0编辑  收藏  举报
---页脚---