Ipad 开发二维码扫描,摄像头旋转角度问题解决办法

在ipad上,用户是横屏操作的,虽然界面旋转了,是横屏的,但是摄像头里显示的依然是竖屏效果,也就是说从摄像头里看到的和人眼看到的内容相差90度或者270度,网上搜索了一番,是这个AVCaptureVideoPreviewLayer 的问题,设备旋转了,但是它没有旋转,导致屏幕显示的,和人眼看到的有偏差,找到如下解决办法,希望对大家有帮助

AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];

layer.videoGravity = AVLayerVideoGravityResizeAspectFill;

layer.frame= view.frame;

[view.layer insertSublayer:layer atIndex:0];

CGRectlayerRect = view.layer.bounds;//获取当前设备的旋转角度

UIDeviceOrientation deorientation =[[UIDevice currentDevice] orientation];

switch(deorientation) {

    case UIDeviceOrientationLandscapeLeft:

        layer.affineTransform = CGAffineTransformMakeRotation(M_PI+ M_PI_2);// 270 degrees

        //旋转270度和90度的效果是一样的,设备旋转之后,屏幕宽和高对换了,那么摄像头的layer的宽高也应该对换

        [layer setBounds:CGRectMake(0,0, layerRect.size.height, layerRect.size.width)];

        break;

    case UIDeviceOrientationLandscapeRight:

        layer.affineTransform = CGAffineTransformMakeRotation(M_PI_2);

        // 90 degrees

        [layer setBounds:CGRectMake(0,0, layerRect.size.height, layerRect.size.width)];

        break;

    case UIDeviceOrientationPortraitUpsideDown:

        layer.affineTransform = CGAffineTransformMakeRotation(M_PI);

        // 180 degrees

        //设备正着和倒着都是竖屏,宽高没变化,那么摄像头的layer的宽高也还是屏幕的宽高

        [layer setBounds:layerRect];

        break;

    default:

    {

        layer.affineTransform = CGAffineTransformMakeRotation(0.0);

        [layer setBounds:layerRect];

    }

    break;



}

[layer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];

你可能感兴趣的:(Ipad 开发二维码扫描,摄像头旋转角度问题解决办法)