百度智能(文本识别),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中的源码,站在巨人的肩膀上编程,也很香!