繁体   English   中英

如何更改CIDetector方向?

[英]How to change CIDetector orientation?

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

它的定向方式与左侧相同,但不适用于正确的方向:

在此输入图像描述

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

[...]使用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;
    }
}

暂无
暂无

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

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