ios - 在 Swift 4 上为 Google ML Vision 框架旋转 UIImage

标签 ios swift apple-vision

当图像被捕获时,它默认为左方向。因此,当您将其输入 Google Vision 框架 内的 textDetector 时,它会变得困惑,除非您将照片朝向左侧(右侧的主页按钮)。我希望我的应用支持两个方向。

enter image description here

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/

相关文章:

mysql - 我应该如何在 sql 数据库中存储 iOS 设备 token ?

ios - 使用 Swift 4 Decodable 将字符串 JSON 响应转换为 bool 值

swift - CoreML Vision 人脸检测的输入图像大小要求是多少

ios - 物体检测 : Output different from CreateML vs programatically

json - 解码 Json 不工作,也不报错

ios - 通过 Color Swift 过滤图像中的像素

ios - 限制 NSAttributedString 行数

ios - Cocoapods 在 Xcode 中重置 pod 配置

iphone - 在哪里可以找到用于 iO 的 Boids 算法的一些开源实现?

html - iOS:将大型 HTML 内容拆分为多个水平分页 View