• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios切换摄像头前后分辨率改变 AVCaptureSessionPresetHigh

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:47:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

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

我检查了这个委托(delegate)方法:

- (void)captureOutputAVCaptureOutput *)captureOutput didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)connection

获取宽度和高度:

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

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



Best Answer-推荐答案


我解决了这个问题,再见改变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)configSessionPresetAVCaptureDevicePosition)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];
    }
}

关于ios切换摄像头前后分辨率改变 AVCaptureSessionPresetHigh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728032/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap