【问题标题】:How to change CIDetector orientation?如何更改 CIDetector 方向?
【发布时间】:2018-09-20 14:17:27
【问题描述】:

所以我尝试在 Swift 中使用 CIDetector 制作文本检测器。当我将手机指向一段文字时,它没有检测到它。但是,如果我将手机转到一边,它就会工作并检测到文本。如何更改它以使其在正确的相机方向上检测文本?这是我的代码:

准备文本检测功能:

func prepareTextDetector() -> CIDetector {
    let options: [String: AnyObject] = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorAspectRatio: 1.0]
    return CIDetector(ofType: CIDetectorTypeText, context: nil, options: options)
}

文字检测功能:

func performTextDetection(image: CIImage) -> CIImage? {
    if let detector = detector {
        // Get the detections
        let features = detector.featuresInImage(image)
        for feature in features as! [CITextFeature] {
            resultImage = drawHighlightOverlayForPoints(image, topLeft: feature.topLeft, topRight: feature.topRight,
                                                        bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight)                
            imagex = cropBusinessCardForPoints(resultImage!, topLeft: feature.topLeft, topRight: feature.topRight, bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight)
        }
    }
    return resultImage
}

它在像左侧那样定向时有效,但不适用于右侧:

【问题讨论】:

    标签: ios swift cidetector


    【解决方案1】:

    您必须使用CIDetectorImageOrientation,正如Apple 在其CITextFeature documentation 中所述:

    [...] 使用CIDetectorImageOrientation 选项指定所需的 查找直立文本的方向。

    例如,你需要做的

    let features = detector.featuresInImage(image, options: [CIDetectorImageOrientation : 1]) 
    

    1 是 exif 编号,取决于方向,可以像这样计算

    func imageOrientationToExif(image: UIImage) -> uint {
        switch image.imageOrientation {
        case UIImageOrientation.Up:
            return 1;
        case UIImageOrientation.Down:
            return 3;
        case UIImageOrientation.Left:
            return 8;
        case UIImageOrientation.Right:
            return 6;
        case UIImageOrientation.UpMirrored:
            return 2;
        case UIImageOrientation.DownMirrored:
            return 4;
        case UIImageOrientation.LeftMirrored:
            return 5;
        case UIImageOrientation.RightMirrored:
            return 7;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2017-07-23
      相关资源
      最近更新 更多