iOS-鏡頭采集(Camera capture&AVCaptureSession)

iPhone 手機在圖片拍照和視頻錄制方面有很強大的功能典奉,小編認為如果很好使用 iPhone 拍攝出很好照片 & 錄制很有趣的視頻捶枢。本章將詳細講解 AVCaptureSession 肚医,并最后給出 FYCameraKit 項目Demo匙奴。
下面小編為大家講解 iOS 拍照和視頻錄制功能實現(xiàn)叙赚,下圖是鏡頭采集需要的類:

iOS-Camera Capture (iOS 鏡頭采集).png

總體介紹

在整個鏡頭采集過程小編把整個過程比作我們常見發(fā)電過程:均是通過特定的步驟實現(xiàn)得到最后的產(chǎn)物怨咪。下面??做詳細解釋:

AVCaptureSession (發(fā)電機)

AVCaptureSession :作為音視頻捕捉會話乏矾,把鏡頭和麥克風捕捉到的音視頻源輸出到設備中孟抗。
提示:可以有多個輸入和輸出。

AVCaptureDevice (發(fā)電設備)

AVCaptureDevice:輸入設備钻心,包括:攝像頭和麥克風凄硼。可以通過設置一些參數(shù)來調(diào)節(jié)設備采集效果(例如:曝光捷沸,白平衡和聚焦等)摊沉。

AVCaptureInput (水風能源)

AVCaptureInput:輸入數(shù)據(jù)管理對象,經(jīng)過 AVCaptureDevice 實現(xiàn)初始化痒给,然后添加到 AVCaptureSession (發(fā)電機) 中進行相應的輸出说墨。
子類:AVCaptureDeviceInput骏全、AVCaptureScreenInputAVCaptureMetadataInput

AVCaptureOutput (電力)

AVCaptureOut:數(shù)據(jù)輸出管理尼斧,通過 AVCaptureSession 中輸出姜贡。可以通過相關(guān)的協(xié)議實對應的數(shù)據(jù)輸出棺棵。
子類:AVCaptureFileOutput鲁豪、AVCapturePhotoOutputAVCaptureStillImageOutput律秃、AVCaptureVideoDataOutputAVCaptureAudioDataOutput治唤、AVCaptureAudioPreviewOutput棒动、AVCaptureDepthDataOutputAVCaptureMetadataOutput

AVCaptureVideoPreviewLayer (發(fā)電預覽)

AVCaptureVideoPreviewLayer:可以支持在拍攝過程中進行相關(guān)的預覽宾添,只需要在初始時實現(xiàn)對應的 AVCaptureSession 即可船惨。

視頻錄制

下面展示在蘋果官方開發(fā)文檔給出的圖:


iOS-Video Capture(iOS 視頻錄制).png

視頻錄制總概

  • 創(chuàng)建 AVCaptureSession 對象
  • 使用 AVCaptureDevice 靜態(tài)方法獲取設備常使用設備,例如:拍照和錄像需要的攝像頭缕陕,錄音所需的麥克風
  • 利用輸入設備 AVCaptureDevice 初始化 AVCaptureDeviceInput 對象
  • 初始化輸出數(shù)據(jù)管理對象粱锐,拍照進行初始化 AVCaptureStillImageOutput 對象,拍照初始化 AVCaptureMovieFileOutput 對象
  • 將數(shù)據(jù)輸入對象 AVCaptureDeviceInput 和數(shù)據(jù)輸出對象 AVCaptureDeviceOutput 添加到媒體會話管理對象 AVCaptureSession
  • 創(chuàng)建預覽的圖層 AVCaptureVideoPreviewLayer 指定媒體會話扛邑,添加圖層到容器怜浅,調(diào)用 AVCaptureSession 中的 StartRunning 方法開始捕捉
  • 將捕捉到內(nèi)容保存到指定文件中

詳解鏡頭采集類

AVCaptureSession

小編查看 AVCaptureSession.h文件看到,在鏡頭采集的過程中有兩種實現(xiàn)方式蔬崩。

  • 使用系統(tǒng) AVCaptureSession自動配置輸入和輸出
  • 手動配置輸入和輸出之間的對應關(guān)系

(1) AVCaptureSession 初始化

  • 使用AVCaptureSession 自動配置輸入和輸出之間對應關(guān)系
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    NSError *error = nil;
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
    [session beginConfiguration];

    session.sessionPreset = AVCaptureSessionPresetPhoto;
    if (audioInput) {
        
    }else {
        //// Handle the failure.
        [session commitConfiguration];
    }
    if ([session canAddInput:audioInput]) {
        [session addInput:audioInput];
    }else {
        // Handle the failure.
        [session commitConfiguration];
    }

上面代碼是 AVCaptureSession 進行初始化并且實現(xiàn)初始化麥克風進行相關(guān)音頻錄制恶座,并添加到 AVCaptureSession中。
beginConfiguration & commitConfiguration 在進行配置 AVCaptureSession 的相關(guān)屬性值時需要對這兩個函數(shù)調(diào)用沥阳,并且這兩個函數(shù)是成對出現(xiàn)跨琳。

  • 手動配置 AVCaptureSession 輸入和輸出之間對應關(guān)系
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    
    [session beginConfiguration];
    NSError *error = nil;
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    if (!videoInput) {
        //handle configuration video device
    }
    AVCaptureInputPort *videoPort = videoInput.ports[0];
    
    error = nil;
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
    if (!videoInput) {
        //handle configuration audio device
    }
    AVCaptureInputPort *audioPort = audioInput.ports[0];
    
    NSArray<AVCaptureInputPort *> *inputPorts = @[videoPort, audioPort];
    AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    
    AVCaptureConnection *connection = [AVCaptureConnection connectionWithInputPorts:inputPorts output:videoDataOutput];
    if ([session canAddConnection:connection]) {
        [session addConnection:connection];
    }else {
        //handle session can not add AVCaptureConnection
        
        [session commitConfiguration];
        return;
    }
    [session commitConfiguration];

上面??代碼是使用 AVCaptureConnection實現(xiàn)在 AVCaptureSession 使輸入和輸出進行自己配置。
詳細的細節(jié)會在 FYCameraKit 進行詳細的講解桐罕。

注釋:
1)只有在調(diào)用 beginConfiguration 時我們對 AVCaptureSession 配置才會被真正開始修改脉让。

(2) AVCaptureSession 中斷

在音視頻錄制過程中可能對因為電話導致中斷,開發(fā)過程中提供中斷結(jié)束后獲取中斷產(chǎn)生的原因功炮。

  • 注冊中斷監(jiān)聽 & 刪除監(jiān)聽
//監(jiān)聽注冊
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCaptureSession:) name:AVCaptureSessionInterruptionEndedNotification object:_session];

//監(jiān)聽清除   
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureSessionInterruptionEndedNotification object:_session];
  • 從傳遞的 userInfo 通過關(guān)鍵 key 獲取原因:
AVCaptureSessionInterruptionReasonKey
  • 獲取具體的中斷原因:
typedef NS_ENUM(NSInteger, AVCaptureSessionInterruptionReason) {
    AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableInBackground               = 1,//從后臺運行開啟 camera溅潜,不夠 iOS 9.0 之后不提供中斷 
    AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient                   = 2,//音頻軟軟件被占用
    AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient                   = 3,//視頻軟件被占用
    AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps = 4,//全屏或者是錄制屏幕變化
}
  • 監(jiān)聽事件的處理
- (void)handleCaptureSession:(NSNotification *)notification {
    AVCaptureSessionInterruptionReason reason = [[notification.userInfo objectForKey:AVCaptureSessionInterruptionReasonKey] unsignedIntegerValue];
    
    switch (reason) {
        case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableInBackground:
        {
           //handle device not available background
            break;
        }
        case AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient:
        {
           //handle that audio was occupied  
           break;
        }
        case AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient:
        {  
           //handle that video was occupied  
            break;
        }
        case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps:
        {
           //handle that UI was multiple
            break;
        }
    }
}

實現(xiàn)拍照和視頻錄制的 FYCameraKit 的下載地址: FYCameraKit

最后給出 AVFoundation API 的總結(jié):

AVFoundation-Struct.png

參考資料:
AVCaptureSession
AVCaptureDevice
AVCaptureConnection

第一次發(fā)布:2017/8/10 20:49:21 星期四
第二次發(fā)布:2017/8/10 23:36:34 星期四

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市死宣,隨后出現(xiàn)的幾起案子伟恶,更是在濱河造成了極大的恐慌,老刑警劉巖毅该,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件博秫,死亡現(xiàn)場離奇詭異潦牛,居然都是意外死亡,警方通過查閱死者的電腦和手機挡育,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門巴碗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人即寒,你說我怎么就攤上這事橡淆。” “怎么了母赵?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵逸爵,是天一觀的道長。 經(jīng)常有香客問我凹嘲,道長师倔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任周蹭,我火速辦了婚禮趋艘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凶朗。我一直安慰自己瓷胧,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布棚愤。 她就那樣靜靜地躺著搓萧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪遇八。 梳的紋絲不亂的頭發(fā)上矛绘,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音刃永,去河邊找鬼货矮。 笑死,一個胖子當著我的面吹牛斯够,可吹牛的內(nèi)容都是我干的囚玫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼读规,長吁一口氣:“原來是場噩夢啊……” “哼抓督!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起束亏,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤铃在,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體定铜,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡阳液,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了揣炕。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帘皿。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖畸陡,靈堂內(nèi)的尸體忽然破棺而出鹰溜,到底是詐尸還是另有隱情,我是刑警寧澤丁恭,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布曹动,位于F島的核電站,受9級特大地震影響牲览,放射性物質(zhì)發(fā)生泄漏仁期。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一竭恬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧熬的,春花似錦痊硕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至橡伞,卻和暖如春盒揉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背兑徘。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工刚盈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人挂脑。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓藕漱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親崭闲。 傳聞我的和親對象是個殘疾皇子肋联,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容