How to change the orientation of the CIDetector?

So, I'm trying to make a text detector using the CIDetector in swift. When I point my phone to a piece of text, it does not detect it. However, if I turn my phone to the side, it works and detects text. How can I change it so that it detects text with the correct camera orientation? Here is my code:

Prepare the text detector function:

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

Text Detection Function:

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
}

It works when it is oriented as left, but not for the correct one:

enter image description here

+4
source share
1 answer

I think you should use CIDetectorImageOrientation, as Apple stated in 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;
    }
}
+7

All Articles