当图像被捕获时,它默认为左方向。因此,当您将其输入 Google Vision 框架
内的 textDetector
时,它会变得困惑,除非您将照片朝向左侧(右侧的主页按钮)。我希望我的应用支持两个方向。
let visionImage = VisionImage(image: image)
self.textDetector?.detect(in: visionImage, completion: { (features, error) in
//2
guard error == nil, let features = features, !features.isEmpty else {
print("Could not recognize any text")
self.dismiss(animated: true, completion: nil)
return
}
//3
print("Detected Text Has \(features.count) Blocks:\n\n")
for block in features {
//4
print("\(block.text)\n\n")
}
})
我已尝试以新方向重新创建图像,但不会改变它。
有人知道怎么办吗?
我已经尝试了所有这些建议 How to rotate image in Swift?
最佳答案
尝试规范化 UIImage 对象,使用这个函数:
import UIKit
public extension UIImage {
public func normalizeImage() -> UIImage {
if self.imageOrientation == UIImageOrientation.up {
return self
}
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return normalizedImage
}
}
我希望这会有所帮助,因为我通常会在出现方向问题时使用它。
关于ios - 在 Swift 4 上为 Google ML Vision 框架旋转 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51145859/