ios switch camera 前后分辨率改变 AVCaptureSessionPresetHigh

ios switch camera front and back resolution is changed AVCaptureSessionPresetHigh

我使用了 GPUImage 库,我的前置摄像头会话预设是 AVCaptureSessionPresetPhoto,后置摄像头是 AVCaptureSessionPresetHigh,

if (self.isFrontFacingCameraPresent) {
    [self setCaptureSessionPreset: AVCaptureSessionPresetHigh];
} else {
    [self setCaptureSessionPreset:AVCaptureSessionPresetPhoto];
}
[self rotateCamera];
  1. 初始状态为使用前置摄像头,分辨率为1280x960;

  2. 现在换了后置摄像头,分辨率为1920x1080;

  3. 然后换前置摄像头,分辨率是1280x720,很奇怪;

我检查了这个委托方法:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

获取的宽度和高度:

    CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
    int bufferWidth = (int) CVPixelBufferGetWidth(cameraFrame);
    int bufferHeight = (int) CVPixelBufferGetHeight(cameraFrame);

bufferHeight是720,不知道为什么换了后置摄像头,高度从960变成了720!可能是苹果的bug?

我解决了这个问题,再见更改rotateCamera函数,我重写了一个用于前后切换相机的函数:

- (void)switchCameraFrontAndBack {

    NSError *error;
    AVCaptureDeviceInput *newVideoInput;
    AVCaptureDevicePosition currentCameraPosition = self.cameraPosition;

    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];
        [self configSessionPreset:currentCameraPosition];
        if ([_captureSession canAddInput:newVideoInput])
        {
            [_captureSession addInput:newVideoInput];
            videoInput = newVideoInput;
        }
        else
        {
            [_captureSession addInput:videoInput];
        }
        [_captureSession commitConfiguration];
    }

    _inputCamera = backFacingCamera;
    [self setOutputImageOrientation:self.outputImageOrientation];
}

- (void)configSessionPreset:(AVCaptureDevicePosition)currentPosition {
    if (currentPosition == AVCaptureDevicePositionBack) {
        if (WIDTH <= Iphone4SWidth) {
            if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
                [self setCaptureSessionPreset:AVCaptureSessionPreset1280x720];
            } else if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
                [self setCaptureSessionPreset:AVCaptureSessionPreset1920x1080];
            }
        } else {
            if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
                [self setCaptureSessionPreset:AVCaptureSessionPreset1920x1080];
            } else {
                [self setCaptureSessionPreset: AVCaptureSessionPresetHigh];
            }
        }
    } else {
        [self setCaptureSessionPreset:AVCaptureSessionPresetPhoto];
    }
}

bufferHeight是720,不知道为什么换了后置摄像头,高度从960变成了720!可能是苹果的bug?


当使用AVCaptureSessionPresetHigh时,不同camera实际分辨率不同,Front和Back不同,会得到camera的最高分辨率。我猜你使用了 iphone5.