zoukankan      html  css  js  c++  java
  • iOS 中正确切换摄像头&正确实现设置帧率的方式

    以前编写【魔拍】应用的时候,发现一个问题,设置相机按每秒20帧的速度输出视频。开始时一切正常,但是切换摄像头后,帧率就变成动态的了且小于20帧/s。解决方式:

            [_captureSession stopRunning];
            [_captureSession beginConfiguration];
            
            [_captureSession removeInput:videoInput];
            if ([_captureSession canAddInput:newVideoInput]){
                videoInput = newVideoInput;
                [_captureSession addInput:videoInput];
            }
            
            [self setFrameRate];
            
            [_captureSession commitConfiguration];
            [_captureSession startRunning];
            
     1 -(void) setFrameRate;
     2 {
     3     
     4     float rate = currentCameraPosition == 1 ? 20 : 30;
     5     
     6     if ([_inputCamera respondsToSelector:@selector(activeVideoMinFrameDuration)]) {
     7         [_inputCamera lockForConfiguration:nil];
     8         _inputCamera.activeVideoMinFrameDuration = CMTimeMake(1, rate);
     9         _inputCamera.activeVideoMaxFrameDuration = CMTimeMake(1, rate);
    10         [_inputCamera unlockForConfiguration];
    11         
    12     }else{
    13         AVCaptureConnection *conn = [[_captureSession.outputs lastObject] connectionWithMediaType:AVMediaTypeVideo];
    14         if (conn.supportsVideoMinFrameDuration)
    15             conn.videoMinFrameDuration = CMTimeMake(1,rate);
    16         if (conn.supportsVideoMaxFrameDuration)
    17             conn.videoMaxFrameDuration = CMTimeMake(1,rate);
    18         
    19     }
    20 }

    Tips: 在切换摄像头的时候Stop Seesion,然后重新设置FrameRate,最后重新Run Seesion。

  • 相关阅读:
    基于朴素贝叶斯的书籍评价信息分类任务
    贝叶斯原理
    knn算法手写字识别案例
    knn原理及借助电影分类实现knn算法
    航空公司案列分析
    k-meanas原理自实现
    df认识
    箱线图
    pandas认识
    分析system_call中断处理过程
  • 原文地址:https://www.cnblogs.com/junay/p/3302211.html
Copyright © 2011-2022 走看看