Created
October 26, 2016 13:41
-
-
Save non117/5676d1fcf3bab869d1af269098f00df1 to your computer and use it in GitHub Desktop.
OCRくん
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OCRClient.swift | |
// ReceiptManager | |
// | |
// Created by non on 2016/10/10. | |
// Copyright © 2016年 non. All rights reserved. | |
// | |
import Foundation | |
import Alamofire | |
public class OCRClient { | |
let HOST = "https://vision.googleapis.com" | |
let ANNOTATE_API = "/v1/images:annotate" | |
var api_key: String | |
init(api_key: String){ | |
self.api_key = api_key | |
} | |
// MARK: - main request method | |
func annotate(image: Data) -> AnnotatedResponse? { | |
let parameters: Parameters = [ | |
"requests": [ | |
"image": [ | |
"content": image.base64EncodedString() | |
], | |
"features": [ | |
[ | |
"type": "TEXT_DETECTION", | |
"maxResults": 2 | |
] | |
] | |
] | |
] | |
let url = HOST + ANNOTATE_API + "?key=" + api_key | |
var annotatedResonse: AnnotatedResponse? | |
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseJSON { | |
response in | |
print(response) | |
if let json = response.result.value { | |
annotatedResonse = try? AnnotatedResponse.decodeValue(json) | |
} | |
} | |
return annotatedResonse | |
} | |
// MARK: - convenience request | |
func annotate(imagePath: URL) -> AnnotatedResponse? { | |
let image = try! Data.init(contentsOf: imagePath) | |
return annotate(image: image) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://developers.mobage.jp/blog/2015/afnetworkingssl