年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2937|回复: 0

[框架接口] AVFoundation框架基本工作原理

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2015-5-22 13:55:38 | 显示全部楼层 |阅读模式
    6597166523681802641.png

    128071114503353997.png


    相机相关应用一般会用到AVFoundation. 这里简单做个整理。

    1、session

    AVFoundation是基于session(会话)概念的。 一个session用于控制数据从input设备到output设备的流向。
    [Objective-C] 纯文本查看 复制代码
    // 创建一个session:
    self.session = [[AVCaptureSession alloc] init];
    // session允许定义图片的拍摄质量。
    self.session.sessionPreset = AVCaptureSessionPresetPhoto;

    2、capture device

    定义好session后,就该定义session所使用的设备了。(使用AVMediaTypeVideo 来支持视频和图片)
    [Objective-C] 纯文本查看 复制代码
    #pragma mark - 前后摄像头
    self.devices =  [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *device = [self.devices firstObject];
    [self setDevice:device FlashMode:AVCaptureFlashModeAuto];
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    3、capture device input

    有了capture device, 然后就获取其input capture device, 并将该input device加到session上。
    [Objective-C] 纯文本查看 复制代码
    self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
    if ([self.session canAddInput:self.deviceInput]) [self.session addInput:self.deviceInput];

    4、preview

    在定义output device之前,我们可以先使用preview layer来显示一下camera buffer中的内容。这也将是相机的“取景器”。
    AVCaptureVideoPreviewLayer可以用来快速呈现相机(摄像头)所收集到的原始数据。我们使用第一步中定义的session来创建preview layer, 并将其添加到main view layer上。
    [Objective-C] 纯文本查看 复制代码
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.preview.layer addSublayer:self.previewLayer];

    5、start Run

    最后需要start the session.
    一般在viewWillAppear:方法中开启,在viewDidDisappear:方法中关闭
    [Objective-C] 纯文本查看 复制代码
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.session startRunning];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
        [self.session stopRunning];
    }
    ==============以下内容为“对视频进行实时处理”部分================

    6、the output buffer

    如果向对视频进行实时处理,则需要直接对camera buffer中的视频流进行处理。
    首先我们定义一个图像数据输出(AVCaptureStillImageOutput), 并将其添加到session上。
    [Objective-C] 纯文本查看 复制代码
        // 媒体输出
        self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
        self.imageOutput.outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG};
        AVCaptureConnection * connection = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
        connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
        if([self.session canAddOutput:self.imageOutput])[self.session addOutput:self.imageOutput];

    7、获取图片
    [Objective-C] 纯文本查看 复制代码
        [self.dataOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
            if (imageDataSampleBuffer == NULL) {
                return;
            }
            NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage * image = [UIImage imageWithData:imageData];




    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-27 15:47 , Processed in 0.053931 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表