2016-03-24 4 views
4

Так что я пытаюсь сделать текстовый детектор, используя 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

ответ

7

Думаю, вы должны использовать 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; 
    } 
} 
Смежные вопросы