2015-06-30 22 views
0

我有一個基於Swift的應用程序,目前實現Tesseract OCR框架(類似於本教程中的表格:http://www.raywenderlich.com/93276/implementing-tesseract-ocr-ios)。所以,現在我想將文本從的UITextField的號碼分別iOS中的Tesseract(Swift) - 如何在UITextField中分離文本和數字?

Subtotal 155.60 
Tax 14.02 
Total 169.82 

:所以在拍照和採用正方體,我得到一個UITextField對象下面的輸出。我正考慮在包含所有價格格式([0.01 0.02等])的矩陣的矩陣中使用Swift中內置的「包含」函數,但這隻會返回布爾值(How to have a textfield scan for all values in an array individually in swift?)。有沒有人有任何建議如何做到這一點?乾杯!

正方體實施

func performImageRecognition(image: UIImage)   
    // 0 

    // 1 
    let tesseract = G8Tesseract() 

    // 2 
    tesseract.language = "eng" 

    // 3 
    tesseract.engineMode = .TesseractCubeCombined 

    // 4 
    tesseract.pageSegmentationMode = .Auto 

    // 5 
    tesseract.maximumRecognitionTime = 60.0 

    // 6 
    tesseract.image = image.g8_blackAndWhite() 
    tesseract.recognize() 

    // 7 
    textView.text = tesseract.recognizedText 
    textView.editable = true 

回答

0

聽起來像是你可能要考慮使用正則表達式

func seperate (text: String) -> (text: String?, value: String?) { 

    // You might want to do an extra check here to ensure the whole string is valid 
    // i.e., nothing in between the two parts of the string 

    let textMatch = text.rangeOfString("^([A-Z]|[a-z])+", options: .RegularExpressionSearch) 
    let priceMatch = text.rangeOfString("[0-9]*.[0-9]{2}$", options: .RegularExpressionSearch) 
    // You might want to adjust regex to handle price edge cases, such as 15 (rather than 15.00) etc 

    if let textMatch = textMatch, priceMatch = priceMatch { 
     let textValue = text.substringWithRange(textMatch) 
     let priceValue = text.substringWithRange(priceMatch) 
     return(textValue, priceValue) 
    } else { 
     return (nil, nil) 
    } 

} 

seperate("Subtotal 155.60") // -> Subtotal, 155.60 
相關問題