百度智能(文本識別),API傳圖OC代碼與SDK使用

 

百度智能中的文本識別中的身份證識別,有API方式和SDK方式

API方式

百度智能(文本識別),百度API傳圖沒有提供OC的示例,這裏提供一下

- (void)OCTest:(NSString*)token {

   
    //***********post請求********************//
    NSString *testURL =[NSString stringWithFormat:@"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=%@",token];
    testURL = [testURL  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:testURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //設置請求方法
    request.HTTPMethod = @"POST";
    //設置請求體
    UIImage *testImage = [UIImage imageNamed:@"test.jpg"];
    NSData *imageData = UIImagePNGRepresentation(testImage);
    NSString *baseStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
   NSString *urlEncode=  (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                          NULL,
                                                                          (__bridge CFStringRef)baseStr,
                                                                          NULL,
                                                                          (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                       CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
    
    
    NSLog(@"urlEncode is %@",urlEncode);

    NSString *str =[NSString stringWithFormat:@"image=%@&id_card_side=%@",urlEncode,@"front"];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    
    request.HTTPBody = data;
    NSURLSession *session  = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask =  [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //
        if (error) {
            NSLog(@"error is %@",error);
        }else
        {
            NSLog(@"data is %@",data);
           NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            NSLog(@"解析得出的dic is %@",dic);
        }
    }];
    [dataTask resume];
    
}

如果UI沒設計拍照UI,這裏建議大家使用SDK,穩定性好,效率高。

SDK方式

真機運行報錯的話,把lib中的模擬器都移掉。

真機運行報錯、Archive/IPA/Upload AppStore 時報錯"Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]..."

爲了方便開發者調試,AipBase.framework合併了模擬器和真機架構,上線前,使用lipo工具移除相關架構即可,參考:

cd lib
# 使用lipo -info 可以查看包含的架構
lipo -info AipBase.framework/AipBase  # Architectures in the fat file: AipBase are: i386 x86_64 armv7 armv7s arm64
# 移除x86_64, i386
lipo -remove x86_64 AipBase.framework/AipBase -o AipBase.framework/AipBase
lipo -remove i386 AipBase.framework/AipBase -o AipBase.framework/AipBase
lipo -remove x86_64 AipOcrSdk.framework/AipOcrSdk -o AipOcrSdk.framework/AipOcrSdk
lipo -remove i386 AipOcrSdk.framework/AipOcrSdk -o AipOcrSdk.framework/AipOcrSdk
# 再次查看
lipo -info AipBase.framework/AipBase # Architectures in the fat file: AipBase are: armv7 armv7s arm64

如果想修改SDK的UI,可以使用AipOrcSdk中的源碼,站在巨人的肩膀上編程,也很香!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章