1. 程式人生 > >百度OCR文字識別企業版 Object-C 識別兩種型別 程式碼示例

百度OCR文字識別企業版 Object-C 識別兩種型別 程式碼示例

前言:

關於OCR,也沒有什麼可說的、縱觀國內,做的好的掃描SDK基本都是收費的,比如名片掃描王只對企業開發,個人開發者想做也是難啊,吐槽下國內公司環境只認可企業,對個人開發者可謂非常苛刻,不說了,因為專案中用到掃描圖片識別文字的功能,查詢最後選擇百度介面,可惜也是收費的,無奈啊,先就這樣吧,另外附百度介面地址:百度OCR文字識別企業版 ;

目標:

專案是iOS 版本,開發語言是Objective-C,不過看到官方給出的程式碼示例,這裡也就不吐槽了,筆者總結官方的 2 和 3這兩種模式:

2. 上傳圖片原檔案;

3. 圖片URl型別;

附圖:


程式碼示例:

1. 上傳圖片

- (void)baiduOCR_ImageType_2 {
    
    NSString *apikey = @"7467580227e0XXXXXX"; //apikey 替換你自己的
    NSString *urlStr = @"http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid";
    
    NSDictionary *parameters = @{@"fromdevice": @"iOS", @"clientip": @"10.10.10.0",
                                 @"detecttype":@"LocateRecognize",@"imagetype":@"2"};
    
    NSData *imageData = UIImagePNGRepresentation(_postImage);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"www.codertopic.com.png" mimeType:@"image/png"];
    } error: nil];
    
    [request setValue:apikey forHTTPHeaderField:@"apikey"];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSLog(@"Success %@", responseObject);
                 
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Failure %@", error.description);
                 
                 [SVProgressHUD dismissWithError:error.description];
             }];
    
    
    [operation start];
    
}

2. 圖片URl
</pre><p><pre name="code" class="objc">-(void)baiduOCR_ImageType_3{
    NSString *apikey = @"7467580227e0ea1f3de17a437543038d"; //apikey 替換你自己的
    NSString *urlStr = @"http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid";
    
    NSDictionary *parameters = @{@"fromdevice": @"iOS", @"clientip": @"10.10.10.0",
                                 @"detecttype":@"LocateRecognize",@"imagetype":@"3",
                                 @"image":@"http://www.codertopic.com/wp-content/uploads/2016/01/ad-codertopic.png"};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:parameters constructingBodyWithBlock:nil error:nil];
    [request setValue:apikey forHTTPHeaderField:@"apikey"];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSLog(@"Success %@", responseObject);
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Failure %@", error.description);
             }];

    
    [operation start];
}

綜上兩種模式均可返回,不過識別度還是非常的粗糙,不過撮合用吧,百度,還是一個好公司,如果能免費或支援公益貼吧的話。

另若有朋友需要swift版本的話,可留言告知,有時間給大家補上。