Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let bitmapRep = NSBitmapImageRep(cgImage: image)
- let imageData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])! as Data
- let imageString = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
- let url = URL(string: "https://api.ocr.space/parse/image")!
- let session = URLSession.shared
- var request = URLRequest(url: url)
- request.httpMethod = "POST"
- request.httpBody = bodyData.data(using: String.Encoding.utf8)
- request.addValue("##########", forHTTPHeaderField: "apikey")
- let task: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
- if let response = response {
- print("RESPONSE: ", response)
- } else {
- print("ERROR: No response")
- }
- if let error = error {
- print("ERROR: ", error)
- } else {
- print("No error")
- }
- do {
- let dictionary = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
- for (key, value) in dictionary {
- print("Key: ", key)
- print("Value: ", value)
- }
- if let parsedResults = dictionary["ParsedResults"] as? [[String: Any]] {
- if let parsedResult = parsedResults.first {
- if let parsedText = parsedResult["ParsedText"] as? String {
- print("PARSED TEXT ", parsedText)
- } else {
- print("ERROR: Could not read parsedText")
- }
- } else {
- print("ERROR: Could not read first element of parsedResult")
- }
- } else {
- print("ERROR: Could not read parsedResult")
- }
- } catch let error {
- print("ERROR: Could not serialize jSON Data: (error.localizedDescription)")
- }
- })
- task.resume()
Add Comment
Please, Sign In to add comment