简体   繁体   English

检测文字方向

[英]Detecting text orientation

I am able to draw rectangles around text with the CIDetector (Using CIDetectorTypeText ), but I can't find a way to detect text orientation. 我可以使用CIDetector (使用CIDetectorTypeText )在文本周围绘制矩形,但是我找不到检测文本方向的方法。 Does anyone know how to detect text orientation with Swift? 有谁知道如何使用Swift检测文本方向?

This will print out the recognized text orientations. 这将打印出识别的文本方向。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM