检测文本方向

Detecting text orientation

我可以使用 CIDetector(使用 CIDetectorTypeText)在文本周围绘制矩形,但我找不到检测文本方向的方法。有谁知道如何使用 Swift 检测文本方向?

这将打印出识别的文本方向。

guard let image = CIImage(image: imageView.image!) else { return }

let detector = CIDetector(ofType: CIDetectorTypeText, context: nil, options: [
    CIDetectorAccuracy: CIDetectorAccuracyHigh
])

let orientations = [Int](1..<9).flatMap {
    detector?.features(in: image, options: [
        CIDetectorImageOrientation: [=10=]
    ]).count ?? 0 > 0 ? [=10=] : nil
}

if orientations.filter({ [=10=] < 5 }).count == 4 {
    print("text has horizontal orientation")
} else if orientations.filter({ [=10=] > 4 }).count == 4 {
    print("text has vertical orientation")
} else {
    print("text has mixed orientation")
}