2016-03-24 3 views
4

私は迅速にCIDetectorを使ってテキスト検出器を作ろうとしています。私は、私の電話をテキストで指していると、それを検出しません。しかし、私の側に電話を回すと、それは動作し、テキストを検出します。正しいカメラ方向でテキストを検出できるように変更するにはどうすればよいですか?ここに私のコードは次のとおりです。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 
} 

それは正しいもののために、左1と同様に重視する場合に動作しますが、ない:

enter image description here

答えて

7

だと思いますが、CIDetectorImageOrientation、AppleはそのCITextFeature documentationに述べたように:

[...]直立テキストを見つけるために必要な の向きを指定するCIDetectorImageOrientationオプションを使用します。

たとえば、あなたは

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; 
    } 
} 
のように計算することができ向きに依存
let features = detector.featuresInImage(image, options: [CIDetectorImageOrientation : 1]) 

を行う必要があります

関連する問題