Ios 检测文本方向

Ios 检测文本方向,ios,swift,cidetector,Ios,Swift,Cidetector,我能够使用CIDetector(使用CIDetectorTypeText)在文本周围绘制矩形,但我找不到检测文本方向的方法。有人知道如何使用Swift检测文本方向吗?这将打印出识别的文本方向 guard let image = CIImage(image: imageView.image!) else { return } let detector = CIDetector(ofType: CIDetectorTypeText, context: nil, options: [ CID

我能够使用
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: $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")
}
guard let image=CIImage(image:imageView.image!)否则{return}
let detector=CIDetector(ofType:CIDetectorTypeText,上下文:nil,选项:[
CIDetectorAccuracy:CIDetectorAccuracy高
])
设方向=[Int](1..0?$0:nil
}
if orientations.filter({$0<5}).count==4{
打印(“文本具有水平方向”)
}else if orientations.filter({$0>4}).count==4{
打印(“文本具有垂直方向”)
}否则{
打印(“文本具有混合方向”)
}

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

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")
}
guard let image=CIImage(image:imageView.image!)否则{return}
let detector=CIDetector(ofType:CIDetectorTypeText,上下文:nil,选项:[
CIDetectorAccuracy:CIDetectorAccuracy高
])
设方向=[Int](1..0?$0:nil
}
if orientations.filter({$0<5}).count==4{
打印(“文本具有水平方向”)
}else if orientations.filter({$0>4}).count==4{
打印(“文本具有垂直方向”)
}否则{
打印(“文本具有混合方向”)
}

您可以使用CIDetectorImageOrientation并将文本检测为图像(如果它不是图像)。我认为
CIDetectorImageOrientation
只能用于设置方向,请参见@Callam将它们一起使用,但它是输入,而不是输出。如果文本不是图像,则可以使用CIDetectorImageOrientation并将其检测为图像已经有一个了。我想
CIDetectorImageOrientation
只能用于设置方向,请看@Callam一起使用它们,但这是输入,而不是输出。这不是很好,因为它仍然检测垂直文本,即使给定了水平值。这不是很好,因为它仍然检测垂直文本,例如即使给定了水平值。