Skip to content

Instantly share code, notes, and snippets.

@non117
Created October 26, 2016 13:41
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 non117/5676d1fcf3bab869d1af269098f00df1 to your computer and use it in GitHub Desktop.
Save non117/5676d1fcf3bab869d1af269098f00df1 to your computer and use it in GitHub Desktop.
OCRくん
//
// 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)
}
}
@applideveloper
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment