iOS 獲取攝像頭當前方向


在做二維碼掃描和直播獲取視頻流的過程中,可能會用到

AVCaptureDevice

AVCaptureVideoPreviewLayer

AVCaptureSession

這幾個參數,其中

1、定義顯示layer

var preview:AVCaptureVideoPreviewLayer! = nil

 

2、獲取攝像頭方向

self.preview = AVCaptureVideoPreviewLayer(session: self.session)
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill
self.preview.connection.videoOrientation = self.getDeviceDirection()

 

重點就是這里getDeviceDirection方法,這里我的做法就是根據當前狀態欄方向判斷攝像頭方向。

代碼實現:

  /// 獲取當前攝像頭方向
    ///
    /// - Returns: <#return value description#>
    func getDeviceDirection() -> AVCaptureVideoOrientation{
        switch UIApplication.shared.statusBarOrientation {
        case .landscapeLeft:
            return AVCaptureVideoOrientation.landscapeLeft
        case .landscapeRight:
            return AVCaptureVideoOrientation.landscapeRight
        case .portrait:
            return AVCaptureVideoOrientation.portrait
        case .portraitUpsideDown:
            return AVCaptureVideoOrientation.portraitUpsideDown
        default:
            return AVCaptureVideoOrientation.landscapeRight
        }
    }
    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM