检测文字方向

时间:2016-10-26 14:53:31

标签: ios swift cidetector

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

1 个答案:

答案 0 :(得分:2)

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

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: $0
    ]).count ?? 0 > 0 ? $0 : nil
}

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