IOS自带的OCR识别功能

一、识别身份证

@interface IDCardScanViewController () <AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic, strong) AVCaptureSession *captureSession;

@end

@implementation IDCardScanViewController

  • (void)viewDidLoad {

super viewDidLoad\]; // 创建视频预览层 AVCaptureVideoPreviewLayer \*previewLayer = \[\[AVCaptureVideoPreviewLayer alloc\] initWithSession:self.captureSession\]; previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; previewLayer.frame = self.scanView.bounds; \[self.scanView.layer addSublayer:previewLayer\]; // 创建数据输出 AVCaptureMetadataOutput \*output = \[\[AVCaptureMetadataOutput alloc\] init\]; \[self.captureSession addOutput:output\]; \[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()\]; // 设置扫描支持的码类型 if (\[output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeIDCard\]) { output.metadataObjectTypes = @\[AVMetadataObjectTypeIDCard\]; } // 启动扫描 \[self.captureSession startRunning\]; } - (AVCaptureSession \*)captureSession { if (!_captureSession) { _captureSession = \[\[AVCaptureSession alloc\] init\]; // 配置摄像头输入 AVCaptureDevice \*device = \[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo\]; AVCaptureDeviceInput \*input = \[AVCaptureDeviceInput deviceInputWithDevice:device error:nil\]; \[_captureSession addInput:input\]; } return _captureSession; } - (void)captureOutput:(AVCaptureOutput \*)captureOutput didOutputMetadataObjects:(NSArray\<__kindof AVMetadataObject \*\> \*)metadataObjects fromConnection:(AVCaptureConnection \*)connection { for (AVMetadataObject \*metadata in metadataObjects) { if (\[metadata isKindOfClass:\[AVMetadataMachineReadableCodeObject class\]\]) { AVMetadataMachineReadableCodeObject \*code = (AVMetadataMachineReadableCodeObject \*)metadata; if (\[code.type isEqualToString:AVMetadataObjectTypeIDCard\]) { NSString \*result = code.stringValue; // 对扫描结果进行处理 NSLog(@"扫描结果:%@", result); } } } } 下面的方法需要iOS13以上才能支持 #import \ #import \ @interface ViewController () \ @end @implementation ViewController - (void)viewDidLoad { \[super viewDidLoad\]; // 在需要的地方调用此方法启动身份证扫描 \[self startDocumentCapture\]; } - (void)startDocumentCapture { VNDocumentCameraViewController \*documentCameraViewController = \[\[VNDocumentCameraViewController alloc\] init\]; documentCameraViewController.delegate = self; \[self presentViewController:documentCameraViewController animated:YES completion:nil\]; } #pragma mark - VNDocumentCameraViewControllerDelegate - (void)documentCameraViewController:(VNDocumentCameraViewController \*)controller didFinishWithScan:(VNDocumentCameraScan \*)scan { // 遍历扫描结果 for (NSUInteger pageIndex = 0; pageIndex \< scan.pageCount; pageIndex++) { VNPage \*page = \[scan pageAtIndex:pageIndex\]; // 检查扫描结果是否为身份证 if (\[self isIdentityCard:page\]) { // 获取身份证号码和姓名 NSString \*number = \[self identityCardNumberFromPage:page\]; NSString \*name = \[self identityCardNameFromPage:page\]; NSLog(@"身份证号码:%@", number); NSLog(@"姓名:%@", name); // 在这里进行身份证识别后的处理 } } // 关闭扫描视图控制器 \[controller dismissViewControllerAnimated:YES completion:nil\]; } - (void)documentCameraViewControllerDidCancel:(VNDocumentCameraViewController \*)controller { // 用户取消了扫描,关闭扫描视图控制器 \[controller dismissViewControllerAnimated:YES completion:nil\]; } - (void)documentCameraViewController:(VNDocumentCameraViewController \*)controller didFailWithError:(NSError \*)error { // 扫描失败,处理错误信息 NSLog(@"扫描身份证发生错误:%@", error); \[controller dismissViewControllerAnimated:YES completion:nil\]; } #pragma mark - Helper Methods - (BOOL)isIdentityCard:(VNPage \*)page { // 获取页面文本内容 NSString \*text = page.recognizedText.string; // 判断是否包含"公民身份号码"和"姓名"关键字 if (\[text containsString:@"公民身份号码"\] \&\& \[text containsString:@"姓名"\]) { return YES; } return NO; } - (NSString \*)identityCardNumberFromPage:(VNPage \*)page { // 获取页面文本内容 NSString \*text = page.recognizedText.string; // 查找身份证号码 NSRange range = \[text rangeOfString:@"公民身份号码"\]; if (range.location != NSNotFound) { NSString \*number = \[text substringFromIndex:range.location + range.length\]; number = \[number stringByReplacingOccurrencesOfString:@" " withString:@""\]; return number; } return nil; } - (NSString \*)identityCardNameFromPage:(VNPage \*)page { // 获取页面文本内容 NSString \*text = page.recognizedText.string; // 查找姓名 NSRange range = \[text rangeOfString:@"姓名"\]; if (range.location != NSNotFound) { NSString \*name = \[text substringFromIndex:range.location + range.length\]; name = \[name stringByReplacingOccurrencesOfString:@" " withString:@""\]; return name; } return nil; } @end **二、识别图片上的文字** #import \ @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { \[super viewDidLoad\]; UIImage \*image = \[UIImage imageNamed:@"test_image.jpg"\]; \[self recognizeTextInImage:image\]; } - (void)recognizeTextInImage:(UIImage \*)image { // 创建VNImageRequestHandler对象 VNImageRequestHandler \*requestHandler = \[\[VNImageRequestHandler alloc\] initWithCGImage:image.CGImage options:@{}\]; // 创建VNRecognizeTextRequest对象 VNRecognizeTextRequest \*textRequest = \[\[VNRecognizeTextRequest alloc\] initWithCompletionHandler:\^(VNRequest \* _Nonnull request, NSError \* _Nullable error) { // 处理识别结果 if (error == nil) { NSArray \*results = request.results; NSMutableString \*recognizedText = \[NSMutableString string\]; for (VNRecognizedTextObservation \*observation in results) { for (VNRecognizedText \*text in observation.topCandidates(1)) { \[recognizedText appendString:text.string\]; \[recognizedText appendString:@"\\n"\]; } } NSLog(@"识别结果:%@", recognizedText); // 在这里进行识别后的处理 } else { NSLog(@"识别出错:%@", error); } }\]; // 设置识别方式和语种 textRequest.recognitionLevel = VNRequestTextRecognitionLevelAccurate; textRequest.usesLanguageCorrection = YES; // 发送识别请求 NSError \*requestError = nil; \[requestHandler performRequests:@\[textRequest\] error:\&requestError\]; if (requestError != nil) { NSLog(@"发送识别请求出错:%@", requestError); } } @end

相关推荐
美狐美颜sdk7 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭12 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
泓博12 小时前
Objective-c把字符解析成字典
开发语言·ios·objective-c
Daniel_Coder13 小时前
Xcode 中常用图片格式详解
ios·xcode·swift
瓜子三百克14 小时前
Objective-C 路由表原理详解
开发语言·ios·objective-c
帅次14 小时前
Objective-C面向对象编程:类、对象、方法详解(保姆级教程)
flutter·macos·ios·objective-c·iphone·swift·safari
RyanGo16 小时前
iOS断点下载
ios·swift
蒙小萌199317 小时前
找工作-iOS开发-3年经验-AI协作开发
ios
丶皮蛋菌19 小时前
关于OC与Swift内存管理的解惑
ios
杂雾无尘20 小时前
掌握生死时速:苹果应用加急审核全攻略!
ios·swift·apple