直播中摄像头切换功能实现

目前我知道有两种实现方法.
1.基于 AVFoundation 实现
2.利用 GPUImage 框架实现

一.利用 AVFoundation 实现摄像头切换

步骤分析:

1.获取当前的摄像头位置状态

  • 是前置摄像头还是后置摄像头

2.设置我们需要改变的摄像头的位置状态.

  • 如果是前置,那么就改成后置;如果是后置.那么就改成前置

3.根据我们新设置的摄像头位置状态来获取改变后的摄像头设备及输入设备

4.移除之前的摄像头设备.

5.添加新的摄像头设备到会话中.

6.并跟新记录

代码示例

#pragma mark - 切换摄像头
- (IBAction)SWAPMethod:(UIButton *)sender {
    
    [self.avCaptureSession beginConfiguration];
    
    // 1.获取当前设备摄像头位置
    AVCaptureDevicePosition currentPosition = self.currentVideoDeviceInput.device.position;
    // 2.获取需要改变的方向
    AVCaptureDevicePosition needChangePosition = currentPosition == AVCaptureDevicePositionFront ? AVCaptureDevicePositionBack : AVCaptureDevicePositionFront;
    // 3.根据需要改变的方向来获取摄像头设备
    AVCaptureDevice *needChangeDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:needChangePosition];
    // 4.获取新的摄像头输入设备
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:needChangeDevice error:nil];
    // 5.移除之前的摄像头输入设备
    [self.avCaptureSession removeInput:self.currentVideoDeviceInput];
    // 6.添加新的摄像头输入设备
    [self.avCaptureSession addInput:deviceInput];
    // 7.记录当前摄像头输入设备
    self.currentVideoDeviceInput = deviceInput;

    [self.avCaptureSession commitConfiguration];
    
}

二.利用 GPUImage 框架实现摄像头切换功能

只需调用一个方法

  • -(void)rotateCamera;
// 译 : 切换前后摄像头
/** This flips between the front and rear cameras
 */
- (void)rotateCamera;

用这个方法就可以非常方便的切换摄像头了!
下面是这个方法的实现:

- (void)rotateCamera
{
    if (self.frontFacingCameraPresent == NO)
        return;
    
    NSError *error;
    AVCaptureDeviceInput *newVideoInput;
    AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];
    
    if (currentCameraPosition == AVCaptureDevicePositionBack)
    {
        currentCameraPosition = AVCaptureDevicePositionFront;
    }
    else
    {
        currentCameraPosition = AVCaptureDevicePositionBack;
    }
    
    AVCaptureDevice *backFacingCamera = nil;
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) 
    {
        if ([device position] == currentCameraPosition)
        {
            backFacingCamera = device;
        }
    }
    newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
    
    if (newVideoInput != nil)
    {
        [_captureSession beginConfiguration];
        
        [_captureSession removeInput:videoInput];
        if ([_captureSession canAddInput:newVideoInput])
        {
            [_captureSession addInput:newVideoInput];
            videoInput = newVideoInput;
        }
        else
        {
            [_captureSession addInput:videoInput];
        }
        //captureSession.sessionPreset = oriPreset;
        [_captureSession commitConfiguration];
    }
    
    _inputCamera = backFacingCamera;
    [self setOutputImageOrientation:_outputImageOrientation];
}

其实和我们用 AVFoundation 实现几乎一样.

你可能感兴趣的:(直播中摄像头切换功能实现)