ios - 如何改变 CIDetector 方向?

标签 ios swift cidetector

所以我尝试在 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
}

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

enter image description here

最佳答案

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

[...] use the CIDetectorImageOrientation option to specify the desired orientation for finding upright text.

例如,你需要做

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;
    }
}

关于ios - 如何改变 CIDetector 方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199572/

相关文章:

ios - 使用 CIDetector 扫描条码(一维和二维)

ios - 在惰性变量中使用 dispatch_once 或静态变量

ios - 在后台线程中读取 CGImageRef 会使应用程序崩溃

swift - FireStore swift : Accessing counts and document details for use in a table

xcode - 对 UITabBarItem 感到沮丧? UITabBarItem 仅解包选项;您的意思是使用 '!' 错误吗

ios - CIDetector 能否返回多个 CIDetectorTypeRectangle 类型的 CIFeature?

objective-c - UIImagePicker 卡住

javascript - Phonegap 无法读取/呈现 Google Maps API - "Can' t 查找变量 Google"错误

objective-c - 在 Swift 中声明具有泛型类型的变量

objective-c - 将检测到的矩形从纵向 CIImage 转换为横向 CIImage