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 <Vision/Vision.h>

#import <VisionKit/VisionKit.h>

@interface ViewController () <VNDocumentCameraViewControllerDelegate>

@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 <Vision/Vision.h>

@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

相关推荐
恋猫de小郭14 小时前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
网安墨雨18 小时前
iOS应用网络安全之HTTPS
web安全·ios·https
福大大架构师每日一题20 小时前
37.1 prometheus管理接口源码讲解
ios·iphone·prometheus
BangRaJun2 天前
LNCollectionView-替换幂率流体
算法·ios·设计
刘小哈哈哈2 天前
iOS 多个输入框弹出键盘处理
macos·ios·cocoa
靴子学长2 天前
iOS + watchOS Tourism App(含源码可简单复现)
mysql·ios·swiftui
一如初夏丿2 天前
xcode15 报错 does not contain ‘libarclite‘
ios·xcode
杨武博2 天前
ios 混合开发应用白屏问题
ios
BangRaJun3 天前
LNCollectionView
android·ios·objective-c
二流小码农3 天前
鸿蒙元服务项目实战:终结篇之备忘录搜索功能实现
android·ios·harmonyos