Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoBinData.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
#import <CkoHttpResponse.h>

BOOL success = NO;

// 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.
CkoBinData *imageData = [[CkoBinData alloc] init];
// This image file contains some text...
success = [imageData LoadFile: @"qa_data/jpg/text.jpg"];
if (success != YES) {
    NSLog(@"%@",@"Failed to load image file.");
    return;
}

// Create the above JSON.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateBd: @"requests[0].image.content" encoding: @"base64" bd: imageData];
[json UpdateString: @"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

CkoHttp *http = [[CkoHttp alloc] init];
http.SessionLogFilename = @"c:/aaworkarea/sessionLog.txt";

CkoStringBuilder *sb = [[CkoStringBuilder alloc] init];
NSString *url = @"https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpJson: @"POST" url: url json: json contentType: @"application/json" response: resp];
if (success == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%@%d",@"status = ",[resp.StatusCode intValue]);

// A 200 response status indicate success.
if ([resp.StatusCode intValue] != 200) {
    NSLog(@"%@",resp.BodyStr);
    NSLog(@"%@",@"Failed.");
    return;
}

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
[resp GetBodySb: sbResponseBody];
[sbResponseBody WriteFile: @"qa_output/textDetectResponse.json" charset: @"utf-8" emitBom: NO];
[json LoadSb: 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:

int i;
int count_i;
NSString *fullTextAnnotationText = 0;
int j;
int count_j;
NSString *locale = 0;
NSString *description = 0;
int k;
int count_k;
int x;
int y;
int width;
int height;
NSString *languageCode = 0;
NSString *blockType = 0;
int i1;
int count_i1;
int j1;
int count_j1;
int k1;
int count_k1;
NSString *text = 0;
NSString *propertyDetectedBreakType = 0;
int i2;
int count_i2;
CkoJsonObject *json1 = [[CkoJsonObject alloc] init];
CkoJsonObject *json2 = [[CkoJsonObject alloc] init];

i = 0;
count_i = [[json SizeOfArray: @"responses"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    fullTextAnnotationText = [json StringOf: @"responses[i].fullTextAnnotation.text"];
    j = 0;
    count_j = [[json SizeOfArray: @"responses[i].textAnnotations"] intValue];
    while (j < count_j) {
        json.J = [NSNumber numberWithInt: j];
        locale = [json StringOf: @"responses[i].textAnnotations[j].locale"];
        description = [json StringOf: @"responses[i].textAnnotations[j].description"];
        k = 0;
        count_k = [[json SizeOfArray: @"responses[i].textAnnotations[j].boundingPoly.vertices"] intValue];
        while (k < count_k) {
            json.K = [NSNumber numberWithInt: k];
            x = [[json IntOf: @"responses[i].textAnnotations[j].boundingPoly.vertices[k].x"] intValue];
            y = [[json IntOf: @"responses[i].textAnnotations[j].boundingPoly.vertices[k].y"] intValue];
            k = k + 1;
        }

        j = j + 1;
    }

    j = 0;
    count_j = [[json SizeOfArray: @"responses[i].fullTextAnnotation.pages"] intValue];
    while (j < count_j) {
        json.J = [NSNumber numberWithInt: j];
        width = [[json IntOf: @"responses[i].fullTextAnnotation.pages[j].width"] intValue];
        height = [[json IntOf: @"responses[i].fullTextAnnotation.pages[j].height"] intValue];
        k = 0;
        count_k = [[json SizeOfArray: @"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages"] intValue];
        while (k < count_k) {
            json.K = [NSNumber numberWithInt: k];
            languageCode = [json StringOf: @"responses[i].fullTextAnnotation.pages[j].property.detectedLanguages[k].languageCode"];
            k = k + 1;
        }

        k = 0;
        count_k = [[json SizeOfArray: @"responses[i].fullTextAnnotation.pages[j].blocks"] intValue];
        while (k < count_k) {
            json.K = [NSNumber numberWithInt: k];
            blockType = [json StringOf: @"responses[i].fullTextAnnotation.pages[j].blocks[k].blockType"];
            [json ObjectOf2: @"responses[i].fullTextAnnotation.pages[j].blocks[k]" jsonObj: json1];

            i1 = 0;
            count_i1 = [[json1 SizeOfArray: @"property.detectedLanguages"] intValue];
            while (i1 < count_i1) {
                json1.I = [NSNumber numberWithInt: i1];
                languageCode = [json1 StringOf: @"property.detectedLanguages[i].languageCode"];
                i1 = i1 + 1;
            }

            [json ObjectOf2: @"responses[i].fullTextAnnotation.pages[j].blocks[k]" jsonObj: json1];

            i1 = 0;
            count_i1 = [[json1 SizeOfArray: @"boundingBox.vertices"] intValue];
            while (i1 < count_i1) {
                json1.I = [NSNumber numberWithInt: i1];
                x = [[json1 IntOf: @"boundingBox.vertices[i].x"] intValue];
                y = [[json1 IntOf: @"boundingBox.vertices[i].y"] intValue];
                i1 = i1 + 1;
            }

            [json ObjectOf2: @"responses[i].fullTextAnnotation.pages[j].blocks[k]" jsonObj: json1];

            i1 = 0;
            count_i1 = [[json1 SizeOfArray: @"paragraphs"] intValue];
            while (i1 < count_i1) {
                json1.I = [NSNumber numberWithInt: i1];
                j1 = 0;
                count_j1 = [[json1 SizeOfArray: @"paragraphs[i].property.detectedLanguages"] intValue];
                while (j1 < count_j1) {
                    json1.J = [NSNumber numberWithInt: j1];
                    languageCode = [json1 StringOf: @"paragraphs[i].property.detectedLanguages[j].languageCode"];
                    j1 = j1 + 1;
                }

                j1 = 0;
                count_j1 = [[json1 SizeOfArray: @"paragraphs[i].boundingBox.vertices"] intValue];
                while (j1 < count_j1) {
                    json1.J = [NSNumber numberWithInt: j1];
                    x = [[json1 IntOf: @"paragraphs[i].boundingBox.vertices[j].x"] intValue];
                    y = [[json1 IntOf: @"paragraphs[i].boundingBox.vertices[j].y"] intValue];
                    j1 = j1 + 1;
                }

                j1 = 0;
                count_j1 = [[json1 SizeOfArray: @"paragraphs[i].words"] intValue];
                while (j1 < count_j1) {
                    json1.J = [NSNumber numberWithInt: j1];
                    k1 = 0;
                    count_k1 = [[json1 SizeOfArray: @"paragraphs[i].words[j].property.detectedLanguages"] intValue];
                    while (k1 < count_k1) {
                        json1.K = [NSNumber numberWithInt: k1];
                        languageCode = [json1 StringOf: @"paragraphs[i].words[j].property.detectedLanguages[k].languageCode"];
                        k1 = k1 + 1;
                    }

                    k1 = 0;
                    count_k1 = [[json1 SizeOfArray: @"paragraphs[i].words[j].boundingBox.vertices"] intValue];
                    while (k1 < count_k1) {
                        json1.K = [NSNumber numberWithInt: k1];
                        x = [[json1 IntOf: @"paragraphs[i].words[j].boundingBox.vertices[k].x"] intValue];
                        y = [[json1 IntOf: @"paragraphs[i].words[j].boundingBox.vertices[k].y"] intValue];
                        k1 = k1 + 1;
                    }

                    k1 = 0;
                    count_k1 = [[json1 SizeOfArray: @"paragraphs[i].words[j].symbols"] intValue];
                    while (k1 < count_k1) {
                        json1.K = [NSNumber numberWithInt: k1];
                        text = [json1 StringOf: @"paragraphs[i].words[j].symbols[k].text"];
                        propertyDetectedBreakType = [json1 StringOf: @"paragraphs[i].words[j].symbols[k].property.detectedBreak.type"];
                        [json1 ObjectOf2: @"paragraphs[i].words[j].symbols[k]" jsonObj: json2];

                        i2 = 0;
                        count_i2 = [[json2 SizeOfArray: @"property.detectedLanguages"] intValue];
                        while (i2 < count_i2) {
                            json2.I = [NSNumber numberWithInt: i2];
                            languageCode = [json2 StringOf: @"property.detectedLanguages[i].languageCode"];
                            i2 = i2 + 1;
                        }

                        [json1 ObjectOf2: @"paragraphs[i].words[j].symbols[k]" jsonObj: json2];

                        i2 = 0;
                        count_i2 = [[json2 SizeOfArray: @"boundingBox.vertices"] intValue];
                        while (i2 < count_i2) {
                            json2.I = [NSNumber numberWithInt: i2];
                            x = [[json2 IntOf: @"boundingBox.vertices[i].x"] intValue];
                            y = [[json2 IntOf: @"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;
}

NSLog(@"%@",@"Success.");