Created
March 29, 2021 03:41
-
-
Save woodycatliu/0af4b7c8dddbe1e1286374fd76df2d6c to your computer and use it in GitHub Desktop.
在處理相機圖片時(影片),需要先確定鏡頭方向,才能正確做ML分析,或是繪圖。以下分別列出iOS原生鏡頭跟UIImage orientation 跟 Firebase ML Kit 的轉換方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return UIImage.Orientation | |
private func imageOrientation(deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation, | |
cameraPosition: AVCaptureDevice.Position = .front) -> UIImage.Orientation { | |
var deviceOrientation = deviceOrientation | |
if deviceOrientation == .faceDown || deviceOrientation == .faceUp || deviceOrientation == .unknown { | |
deviceOrientation = currectDeviceOrientation() | |
} | |
switch deviceOrientation { | |
case .portrait: | |
return cameraPosition == .front ? .leftMirrored : .right | |
case .portraitUpsideDown: | |
return cameraPosition == .front ? .rightMirrored : .left | |
case .landscapeLeft: | |
return cameraPosition == .front ? .downMirrored : .up | |
case .landscapeRight: | |
return cameraPosition == .front ? .upMirrored : .down | |
case .faceUp, .faceDown, .unknown: | |
return .up | |
@unknown default: | |
return cameraPosition == .front ? .leftMirrored : .right | |
} | |
} | |
// return Firebase MLKit VisionDetectorImageOrientation | |
private func imageOrientation(deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation, | |
cameraPosition: AVCaptureDevice.Position = .front) -> VisionDetectorImageOrientation { | |
var deviceOrientation = deviceOrientation | |
if deviceOrientation == .faceDown || deviceOrientation == .faceUp || deviceOrientation == .unknown { | |
deviceOrientation = currectDeviceOrientation() | |
} | |
switch deviceOrientation { | |
case .portrait: | |
return cameraPosition == .front ? .leftTop : .rightTop | |
case .landscapeLeft: | |
return cameraPosition == .front ? .bottomLeft : .topLeft | |
case .portraitUpsideDown: | |
return cameraPosition == .front ? .rightBottom : .leftBottom | |
case .landscapeRight: | |
return cameraPosition == .front ? .topRight : .bottomRight | |
case .faceUp, .faceDown, .unknown: | |
return .topLeft | |
@unknown default: | |
return cameraPosition == .front ? .leftTop : .rightTop | |
} | |
} | |
private func currectDeviceOrientation()-> UIDeviceOrientation { | |
let status: UIInterfaceOrientation | |
if #available(iOS 13, *) { | |
status = UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.windowScene?.interfaceOrientation ?? .unknown | |
} else { | |
status = UIApplication.shared.statusBarOrientation | |
} | |
switch status { | |
case .portrait, .unknown: | |
return .portrait | |
case .portraitUpsideDown: | |
return .portraitUpsideDown | |
case .landscapeLeft: | |
return .landscapeRight | |
case .landscapeRight: | |
return .landscapeLeft | |
@unknown default: | |
return .portrait | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment