开发者

Google docs OCR implementation iphone

开发者 https://www.devze.com 2023-03-28 01:05 出处:网络
I am trying to integrate google docs OCR feature in my iPhone app. Below is what google docs documentation says.

I am trying to integrate google docs OCR feature in my iPhone app. Below is what google docs documentation says.

To perform OCR on a .pdf, .开发者_JS百科jpg, .png, or .gif file, include the ocr=true parameter when uploading a file:

POST /feeds/default/private/full?ocr=true
GData-Version: 3.0
Authorization: <your authorization header here>
Content-Length: 1984
Content-Type: image/png
Slug: OCRd Doc

... png contents here ...

Now, I am using the following code to make that HTTP post request.

    responseData = [[NSMutableData data] retain];

    NSURL *url = [NSURL URLWithString:@"http://docs.google.com/feeds/default/private/full?ocr=true"];
    UIImage *img = [UIImage imageNamed:@"Submit-top.png"];

    NSData *data = UIImagePNGRepresentation(img);
    int a = [data length];
    NSString *imgLength = [NSString stringWithFormat:@"%d" ,a];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPBody:data];


    [request setHTTPMethod:@"POST"];
    [request addValue:@"3.0" forHTTPHeaderField:@"GData-Version:"];
    [request addValue:auth.accessToken forHTTPHeaderField:@"Authorization:"];
    [request addValue:imgLength forHTTPHeaderField:@"Content-Length:"];
    [request addValue:@"image/png" forHTTPHeaderField:@"Content-Type:"];
    [request addValue:@"OCRd Doc" forHTTPHeaderField:@"Slug:"];




    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);

but I get a response with status code 400.


Instead of using:

[request addValue:@"3.0" forHTTPHeaderField:@"GData-Version:"];

Use:

[request setValue:@"3.0" forHTTPHeaderField:@"GData-Version"];

Note the change from addValue: to setValue: and the removal of the colon in the forHTTPHeaderField: string.

Do the same for the rest of your addValue: lines.

addValue: appends whereas setValue: replaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号