0

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

在此处输入图像描述

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")
    }
})

我试图用新的方向重新创建图像,但这不会改变它。

有谁知道该怎么做?

我已经尝试了所有这些建议 如何在 Swift 中旋转图像?

4

1 回答 1

1

尝试使用此函数规范化 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
    }
}

我希望这会有所帮助,因为我通常会在出现方向问题时使用它。

于 2018-07-03T02:54:22.837 回答