Google docs OCR实现iphone

时间:2011-08-15 17:26:36

标签: iphone ocr google-docs

我正在尝试在我的iPhone应用中集成google docs OCR功能。以下是谷歌文档文档所说的内容。

要在.pdf,.jpg,.png或.gif文件上执行OCR,请在上传文件时包含ocr = true参数:

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 ...

现在,我使用以下代码发出HTTP post请求。

    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);

但我收到状态代码为400的回复。

1 个答案:

答案 0 :(得分:2)

而不是使用:

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

使用:

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

请注意从addValue:setValue:的更改以及forHTTPHeaderField:字符串中冒号的删除。

对其余addValue:行执行相同操作。

附加

addValue:,而setValue:替换。