Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffdonthemic/e35758017695d541f8ef to your computer and use it in GitHub Desktop.
Save jeffdonthemic/e35758017695d541f8ef to your computer and use it in GitHub Desktop.
Swift HP IDOL OnDemand
$ curl -v -X POST -d apikey=YOUR-HP-IDOL-ONDEMAND-APIKEY \
-d url=http://ecx.images-amazon.com/images/I/41FmUAMH4mL._SY300_.jpg \
https://api.idolondemand.com/1/api/sync/ocrdocument/v1
< HTTP/1.1 200 OK
<
{
"text_block": [
{
"text": "TESLA\nWAS\nTHE 4,, ,gé;)\nMAN",
"left": 0,
"top": 0,
"width": 300,
"height": 300
}
]
import Foundation
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
/**
Paste all the code from the following file
- https://github.com/lingoer/SwiftyJSON/blob/master/SwiftyJSON/SwiftyJSON.swift
**/
let apiKey = "YOUR-HP-IDOL-ONDEMAND-APIKEY"
let ocrImageUrl = "http://ecx.images-amazon.com/images/I/41FmUAMH4mL._SY300_.jpg"
let endpoint: NSURL = NSURL(string: "https://api.idolondemand.com/1/api/sync/ocrdocument/v1")
let request = NSMutableURLRequest(URL: endpoint)
request.HTTPMethod = "POST"
let payload = "apikey=\(apiKey)&url=\(ocrImageUrl)".dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: payload, completionHandler: {data, response, error -> Void in
if let value = data {
var error:NSError? = nil
if let jsonObject : AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) {
let json = JSONValue(jsonObject)
if let ocr_results = json["text_block"][0]["text"].string {
println(ocr_results)
}
}else{
println(error)
}
}
})
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment