一、识别身份证
@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