ios切换相机前后分辨率改变了AVCaptureSessionPresetHigh

时间:2015-11-16 03:22:26

标签: ios camera gpuimage

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

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

  2. 现在换回相机,分辨率为1920x1080;

  3. 然后更换前置摄像头,分辨率为1280x720,非常奇怪;

  4. 我检查了这个委托方法:

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

    获取宽度和高度:

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

    缓冲区高度是720,我不知道为什么当改变前置摄像头时,高度从960变为720!也许这是苹果公司的错误?

2 个答案:

答案 0 :(得分:1)

我解决了这个问题,再次更改了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];
    }
}

答案 1 :(得分:0)

bufferHeight是720,我不知道为什么当改变前置摄像头时,高度从960变为720!也许这是苹果公司的错误?

当使用AVCaptureSessionPresetHigh时,实际的分辨率与不同的相机不同,前后不同,它将获得相机的最高分辨率。我猜你用的是iphone5。