我可以使用CIDetector
(Using CIDetectorTypeText
) 在文本周围绘制矩形,但我找不到检测文本方向的方法。有谁知道如何使用 Swift 检测文本方向?
问问题
794 次
1 回答
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")
}
于 2016-10-26T16:45:01.293 回答