AVFoundation二維碼掃描和錄制視頻

之前使用過AVFoundation實(shí)現(xiàn)二維碼掃描的功能辐马,當(dāng)初第一次接觸這個(gè)框架彤路,只是搜索資料今野,實(shí)現(xiàn)功能就草草了事了耘沼,最近要實(shí)現(xiàn)視頻的錄制功能矗蕊,又用到了這個(gè)框架顽腾,而且發(fā)現(xiàn)珊佣,步驟跟實(shí)現(xiàn)二維碼的時(shí)候十分相似堂油,所以來總結(jié)一下棒卷。

使用AVFoundation拍照和錄制視頻的一般步驟如下:

  1. 創(chuàng)建AVCaptureSession對(duì)象顾孽。
  2. 使用AVCaptureDevice的靜態(tài)方法獲得需要使用的設(shè)備,例如拍照和錄像就需要獲得攝像頭設(shè)備比规,錄音就要獲得麥克風(fēng)設(shè)備若厚。
  3. 利用輸入設(shè)備AVCaptureDevice初始化AVCaptureDeviceInput對(duì)象。
  4. 初始化輸出數(shù)據(jù)管理對(duì)象蜒什,如果要拍照就初始化AVCaptureStillImageOutput對(duì)象测秸;如果拍攝視頻就初始化AVCaptureMovieFileOutput對(duì)象。
  5. 將數(shù)據(jù)輸入對(duì)象AVCaptureDeviceInput、數(shù)據(jù)輸出對(duì)象AVCaptureOutput添加到媒體會(huì)話管理對(duì)象AVCaptureSession中霎冯。
  6. 創(chuàng)建視頻預(yù)覽圖層AVCaptureVideoPreviewLayer并指定媒體會(huì)話铃拇,添加圖層到顯示容器中,調(diào)用AVCaptureSession的startRuning方法開始捕獲沈撞。
  7. 將捕獲的音頻或視頻數(shù)據(jù)輸出到指定文件慷荔。

錄制視頻

@property (weak, nonatomic) IBOutlet UIView *viewContainer;
@property (weak, nonatomic) IBOutlet UIButton *videoButton;

@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureDevice *audioDevice;
@property (strong, nonatomic) AVCaptureDevice *captureDevice;
@property (strong, nonatomic) AVCaptureDeviceInput *audioDeviceInput;
@property (strong, nonatomic) AVCaptureDeviceInput *captureDeviceInput;
@property (strong, nonatomic) AVCaptureMovieFileOutput *captureMovieOutput; //視頻輸出流
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *capturePreviewLayer; // 相機(jī)預(yù)覽化圖層
    // 初始化會(huì)話
    if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {
        // 設(shè)置分辨率
        self.captureSession.sessionPreset = AVCaptureSessionPreset640x480;
    }
    
    // 獲取輸入數(shù)據(jù) 視頻和音頻
    NSError *error;
    self.captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:&error];
    if (error) {
        NSLog(@"Error Description:%@", error.localizedDescription);
    }

    self.audioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.audioDevice error:&error];
    if (error) {
        NSLog(@"Error Description:%@", error.localizedDescription);
    }
    
    // 初始化輸出對(duì)象
    self.captureMovieOutput = [[AVCaptureMovieFileOutput alloc] init];
    
    // 將設(shè)備輸入添加到會(huì)話
    if ([self.captureSession canAddInput:self.captureDeviceInput] && [self.captureSession canAddInput:self.audioDeviceInput]) {
        [self.captureSession addInput:self.audioDeviceInput];
        [self.captureSession addInput:self.captureDeviceInput];
        
        AVCaptureConnection *captureConnection = [self.captureMovieOutput connectionWithMediaType:AVMediaTypeVideo];
        if ([captureConnection isVideoStabilizationSupported]) {
            captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
        }
    }
    
    // 將設(shè)備輸出添加到會(huì)話中
    if ([self.captureSession canAddOutput:self.captureMovieOutput]) {
        [self.captureSession addOutput:self.captureMovieOutput];
    }
    
    // 創(chuàng)建預(yù)覽化圖層
    self.capturePreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    CALayer *layer = self.viewContainer.layer;
    self.capturePreviewLayer.frame = layer.bounds;
    // 填充模式
    self.capturePreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [layer insertSublayer:self.capturePreviewLayer below:self.videoButton.layer];
#pragma mark - Getter And Setter
- (AVCaptureSession *)captureSession{
    if (_captureSession == nil) {
        _captureSession = [[AVCaptureSession alloc] init];
    }
    return _captureSession;
}

- (AVCaptureDevice *)audioDevice{
    if (_audioDevice == nil) {
        _audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
    }
    return _audioDevice;
}

- (AVCaptureDevice *)captureDevice{
    if (_captureDevice == nil) {
        NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *camera in cameras) {
            if ([camera position] == AVCaptureDevicePositionBack) {
                return camera;
            }
        }
    }
    return _captureDevice;
}

二維碼掃描

    // 初始化捕捉設(shè)備(AVCaptureDevice),類型為AVMediaTypeVideo
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    // captureDevice創(chuàng)建輸出流
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@",[error localizedDescription]);
        return;
    }
    // 創(chuàng)建媒體數(shù)據(jù)輸出流
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    // 實(shí)例化捕捉會(huì)話
    _caputureSession = [[AVCaptureSession alloc] init];
    // 輸入流,輸出流添加到會(huì)話
    [_caputureSession addInput:input];
    [_caputureSession addOutput:output];
    // 設(shè)置代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    // 設(shè)置輸出媒體數(shù)據(jù)類型為QRCode
    [output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeInterleaved2of5Code, nil]];
    // 實(shí)例化預(yù)覽圖層
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_caputureSession];
    // 設(shè)置圖層填充方式
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//    _videoPreviewLayer.frame = _viewPreview.layer.bounds;
    _videoPreviewLayer.frame = [UIScreen mainScreen].bounds;
    // 添加到預(yù)覽圖層
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
    // 掃描范圍
//    CGSize size = _viewPreview.bounds.size;
    CGSize size = [UIScreen mainScreen].bounds.size;
    CGRect cropRect = CGRectMake(_videoPreviewLayer.bounds.size.width/2 - (105/375.0)*SCREEN_WIDTH, (200/667.5)*SCREEN_HEIGHT, (210/375.0)*SCREEN_WIDTH, (210/667.5)*SCREEN_HEIGHT);
    [output setRectOfInterest:CGRectMake(cropRect.origin.y/size.height, cropRect.origin.x/size.width, cropRect.size.height/size.height, cropRect.size.width/size.width)];
    // 掃描框
    _boxView = [[UIView alloc] initWithFrame:_videoPreviewLayer.frame];
    _boxView.backgroundColor = [UIColor clearColor];
    UIImageView *scanView = [[UIImageView alloc] initWithFrame:_boxView.frame];
    scanView.image = [UIImage imageNamed:@"device_QR_scanning"];
    [_boxView addSubview:scanView];
    [_viewPreview addSubview:_boxView];
    // 開始掃描
    [_caputureSession startRunning];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缠俺,一起剝皮案震驚了整個(gè)濱河市显晶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌晋修,老刑警劉巖吧碾,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異墓卦,居然都是意外死亡倦春,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門落剪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來睁本,“玉大人,你說我怎么就攤上這事忠怖∧匮撸” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵凡泣,是天一觀的道長枉疼。 經(jīng)常有香客問我,道長鞋拟,這世上最難降的妖魔是什么骂维? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮贺纲,結(jié)果婚禮上航闺,老公的妹妹穿的比我還像新娘。我一直安慰自己猴誊,他們只是感情好潦刃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著懈叹,像睡著了一般乖杠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上澄成,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天滑黔,我揣著相機(jī)與錄音笆包,去河邊找鬼。 笑死略荡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的歉胶。 我是一名探鬼主播汛兜,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼通今!你這毒婦竟也來了粥谬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤辫塌,失蹤者是張志新(化名)和其女友劉穎漏策,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體臼氨,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掺喻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了储矩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片感耙。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖持隧,靈堂內(nèi)的尸體忽然破棺而出即硼,到底是詐尸還是另有隱情,我是刑警寧澤屡拨,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布只酥,位于F島的核電站,受9級(jí)特大地震影響呀狼,放射性物質(zhì)發(fā)生泄漏裂允。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一赠潦、第九天 我趴在偏房一處隱蔽的房頂上張望叫胖。 院中可真熱鬧,春花似錦她奥、人聲如沸瓮增。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绷跑。三九已至,卻和暖如春凡资,著一層夾襖步出監(jiān)牢的瞬間砸捏,已是汗流浹背谬运。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留垦藏,地道東北人梆暖。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像掂骏,于是被迫代替她去往敵國和親轰驳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

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