1、图片识别文字
导入框架
#import <Vision/Vision.h>
#import <VisionKit/VisionKit.h>
- (void)documentCameraViewController:(UIImage *)image {
if (@available(iOS 13.0, *)) {
VNRecognizeTextRequest *request = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
return;
}
NSLog(@"开始识别");
int i =1;
for (VNRecognizedTextObservation *observation in request.results) {
NSString *text = [observation topCandidates:1].firstObject.string;
NSLog(@"识别片段%d -> 识别结果:%@",i, text);
i++;
}
NSLog(@"识别结束");
}];
request.recognitionLevel = VNRequestTextRecognitionLevelAccurate; // 可以选择 accurate 或 fast 两种模式
NSArray<NSString *> *customWords = @[@"卡密", @"卡号"];
[request setCustomWords:customWords];
// 设置识别区域和识别语言
request.regionOfInterest = CGRectMake(0, 0, 1, 1); // 全图识别
request.recognitionLanguages = @[@"zh-Hans"]; // 指定识别语言为中文
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCGImage:image.CGImage options:@{}];
[handler performRequests:@[request] error:nil];
} else {
// Fallback on earlier versions
}
}