Sample code for 30+ languages & platforms
Swift

Google Cloud Vision Text Detection

See more HTTP Misc Examples

Demonstrates calling the Google Cloud Vision for text detection (performs Optical Character Recognition). "Detects and extracts text within an image with support for a broad range of languages. It also features automatic language identification." See https://cloud.google.com/vision/docs/detecting-text

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Build the following request:

    // {
    //   "requests": [
    //     {
    //       "image": {
    //         "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
    //       },
    //       "features": [
    //         {
    //           "type": "TEXT_DETECTION"
    //         }
    //       ]
    //     }
    //   ]
    // }

    // Use this online tool to generate the code from sample JSON: 
    // Generate Code to Create JSON

    // Load an image file.
    let imageData = CkoBinData()!
    // This image file contains some text...
    success = imageData.loadFile(path: "qa_data/jpg/text.jpg")
    if success != true {
        print("Failed to load image file.")
        return
    }

    // Create the above JSON.
    let json = CkoJsonObject()!
    json.updateBd(jsonPath: "requests[0].image.content", encoding: "base64", bd: imageData)
    json.updateString(jsonPath: "requests[0].features[0].type", value: "TEXT_DETECTION")

    // Send the following POST with the HTTP request body containing the above JSON.
    // POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY

    let http = CkoHttp()!
    http.sessionLogFilename = "c:/aaworkarea/sessionLog.txt"

    let sb = CkoStringBuilder()!
    var url: String? = "https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY"
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: url, json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("status = \(resp.statusCode.intValue)")

    // A 200 response status indicate success.
    if resp.statusCode.intValue != 200 {
        print("\(resp.bodyStr!)")
        print("Failed.")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    sbResponseBody.writeFile(path: "qa_output/textDetectResponse.json", charset: "utf-8", emitBom: false)
    json.loadSb(sb: sbResponseBody)

    // The response is a JSON document like this:

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "responses": [
    //     {
    //       "textAnnotations": [
    //         {
    //           "locale": "en",
    //           "description": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n",
    //           "boundingPoly": {
    //             "vertices": [
    //               {
    //                 "x": 17,
    //                 "y": 14
    //               },
    // 	...
    //             ]
    //           }
    //         ],
    //         "text": "Chilkat is a cross-language, cross-platform\nAPl providing 90+ classes for many Internet\nprotocols, formats, and algorithms.\n"
    //       }
    //     }
    //   ]
    // }

    // The parsing code generated from the online tool:

    var i: Int
    var count_i: Int
    var fullTextAnnotationText: String?
    var j: Int
    var count_j: Int
    var locale: String?
    var description: String?
    var k: Int
    var count_k: Int
    var x: Int
    var y: Int
    var width: Int
    var height: Int
    var languageCode: String?
    var blockType: String?
    var i1: Int
    var count_i1: Int
    var j1: Int
    var count_j1: Int
    var k1: Int
    var count_k1: Int
    var text: String?
    var propertyDetectedBreakType: String?
    var i2: Int
    var count_i2: Int
    let json1 = CkoJsonObject()!
    let json2 = CkoJsonObject()!

    i = 0
    count_i = json.size(ofArray: "responses").intValue
    while i < count_i {
        json.i = i
        fullTextAnnotationText = json.string(of: "responses[i].fullTextAnnotation.text")
        j = 0
        count_j = json.size(ofArray: "responses[i].textAnnotations").intValue
        while j < count_j {
            json.j = j
            locale = json.string(of: "responses[i].textAnnotations[j].locale")
            description = json.string(of: "responses[i].textAnnotations[j].description")
            k = 0
            count_k = json.size(ofArray: "responses[i].textAnnotations[j].boundingPoly.vertices").intValue
            while k < count_k {
                json.k = k
                x = json.int(of: "responses[i].textAnnotations[j].boundingPoly.vertices[k].x").intValue
                y = json.int(of: "responses[i].textAnnotations[j].boundingPoly.vertices[k].y").intValue
                k = k + 1
            }

            j = j + 1
        }

        j = 0
        count_j = json.size(ofArray: "responses[i].fullTextAnnotation.pages").intValue
        while j < count_j {
            json.j = j
            width = json.int(of: "responses[i].fullTextAnnotation.pages[j].width").intValue
            height = json.int(of: "responses[i].fullTextAnnotation.pages[j].height").intValue
            k = 0
            count_k = json.size(ofArray: "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages").intValue
            while k < count_k {
                json.k = k
                languageCode = json.string(of: "responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode")
                k = k + 1
            }

            k = 0
            count_k = json.size(ofArray: "responses[i].fullTextAnnotation.pages[j].blocks").intValue
            while k < count_k {
                json.k = k
                blockType = json.string(of: "responses[i].fullTextAnnotation.pages[j].blocks[k].blockType")
                json.objectOf2(jsonPath: "responses[i].fullTextAnnotation.pages[j].blocks[k]", jsonObj: json1)

                i1 = 0
                count_i1 = json1.size(ofArray: "property.detectedLanguages").intValue
                while i1 < count_i1 {
                    json1.i = i1
                    languageCode = json1.string(of: "property.detectedLanguages[i].languageCode")
                    i1 = i1 + 1
                }

                json.objectOf2(jsonPath: "responses[i].fullTextAnnotation.pages[j].blocks[k]", jsonObj: json1)

                i1 = 0
                count_i1 = json1.size(ofArray: "boundingBox.vertices").intValue
                while i1 < count_i1 {
                    json1.i = i1
                    x = json1.int(of: "boundingBox.vertices[i].x").intValue
                    y = json1.int(of: "boundingBox.vertices[i].y").intValue
                    i1 = i1 + 1
                }

                json.objectOf2(jsonPath: "responses[i].fullTextAnnotation.pages[j].blocks[k]", jsonObj: json1)

                i1 = 0
                count_i1 = json1.size(ofArray: "paragraphs").intValue
                while i1 < count_i1 {
                    json1.i = i1
                    j1 = 0
                    count_j1 = json1.size(ofArray: "paragraphs[i].property.detectedLanguages").intValue
                    while j1 < count_j1 {
                        json1.j = j1
                        languageCode = json1.string(of: "paragraphs[i].property.detectedLanguages[j].languageCode")
                        j1 = j1 + 1
                    }

                    j1 = 0
                    count_j1 = json1.size(ofArray: "paragraphs[i].boundingBox.vertices").intValue
                    while j1 < count_j1 {
                        json1.j = j1
                        x = json1.int(of: "paragraphs[i].boundingBox.vertices[j].x").intValue
                        y = json1.int(of: "paragraphs[i].boundingBox.vertices[j].y").intValue
                        j1 = j1 + 1
                    }

                    j1 = 0
                    count_j1 = json1.size(ofArray: "paragraphs[i].words").intValue
                    while j1 < count_j1 {
                        json1.j = j1
                        k1 = 0
                        count_k1 = json1.size(ofArray: "paragraphs[i].words[j].property.detectedLanguages").intValue
                        while k1 < count_k1 {
                            json1.k = k1
                            languageCode = json1.string(of: "paragraphs[i].words[j].property.detectedLanguages[k].languageCode")
                            k1 = k1 + 1
                        }

                        k1 = 0
                        count_k1 = json1.size(ofArray: "paragraphs[i].words[j].boundingBox.vertices").intValue
                        while k1 < count_k1 {
                            json1.k = k1
                            x = json1.int(of: "paragraphs[i].words[j].boundingBox.vertices[k].x").intValue
                            y = json1.int(of: "paragraphs[i].words[j].boundingBox.vertices[k].y").intValue
                            k1 = k1 + 1
                        }

                        k1 = 0
                        count_k1 = json1.size(ofArray: "paragraphs[i].words[j].symbols").intValue
                        while k1 < count_k1 {
                            json1.k = k1
                            text = json1.string(of: "paragraphs[i].words[j].symbols[k].text")
                            propertyDetectedBreakType = json1.string(of: "paragraphs[i].words[j].symbols[k].property.detectedBreak.type")
                            json1.objectOf2(jsonPath: "paragraphs[i].words[j].symbols[k]", jsonObj: json2)

                            i2 = 0
                            count_i2 = json2.size(ofArray: "property.detectedLanguages").intValue
                            while i2 < count_i2 {
                                json2.i = i2
                                languageCode = json2.string(of: "property.detectedLanguages[i].languageCode")
                                i2 = i2 + 1
                            }

                            json1.objectOf2(jsonPath: "paragraphs[i].words[j].symbols[k]", jsonObj: json2)

                            i2 = 0
                            count_i2 = json2.size(ofArray: "boundingBox.vertices").intValue
                            while i2 < count_i2 {
                                json2.i = i2
                                x = json2.int(of: "boundingBox.vertices[i].x").intValue
                                y = json2.int(of: "boundingBox.vertices[i].y").intValue
                                i2 = i2 + 1
                            }

                            k1 = k1 + 1
                        }

                        j1 = j1 + 1
                    }

                    i1 = i1 + 1
                }

                k = k + 1
            }

            j = j + 1
        }

        i = i + 1
    }

    print("Success.")

}