美文网首页
iOS 13的VisionKit

iOS 13的VisionKit

作者: 红色海_ | 来源:发表于2020-03-29 19:00 被阅读0次

VisionKit是一个关于文档识别的框架,可使用摄像头拍摄文档,然后将文档转化为扫描件。

两个类 :
VNDocumentCameraViewController和VNDocumentCameraScan,
一个代理 :
VNDocumentCameraViewControllerDelegate

使用方法, 首先导入VisionKit

import <VisionKit/VisionKit.h>

然后创建
VNDocumentCameraViewController控制器。
进行一个判断,以免低版本编译失败。

if(@available(iOS 13,*)) {
//只有支持的机型才能使用,因此要判断是否支持

 if (!VNDocumentCameraViewController.supported) {
            UIAlertController* alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持!" preferredStyle:UIAlertControllerStyleAlert];

        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];

        [self presentViewController:alertVC animated:YES completion:nil];

        return;
  }

    VNDocumentCameraViewController* dcVc = [[VNDocumentCameraViewController alloc] init];

    dcVc.delegate=self;

    [self presentViewController:dcVc animated:YES completion:nil];
 }

然后需要在info.plist文件中添加NSPhotoLibraryAddUsageDescription和NSCameraUsageDescription两个key以免崩溃

紧接着遵守VNDocumentCameraViewControllerDelegate代理

#pragma mark - VNDocumentCameraViewControllerDelegate

- (void)documentCameraViewController:(VNDocumentCameraViewController *)controller didFinishWithScan:(VNDocumentCameraScan *)scan API_AVAILABLE(ios(13)){

    for(int i = 0; i < [scan pageCount]; i++) {

        UIImage* img = [scan imageOfPageAtIndex:i];

        // 可拿到扫描成功的文档图片了

        [img saveToAlbumWithCompletionBlock:nil];
    }
}

- (void)documentCameraViewControllerDidCancel:(VNDocumentCameraViewController *)controller API_AVAILABLE(ios(13)){

    [controller dismissViewControllerAnimated:YES  completion:nil];
}

- (void)documentCameraViewController:(VNDocumentCameraViewController *)controller didFailWithError:(NSError *)error API_AVAILABLE(ios(13)){

}

相关文章

网友评论

      本文标题:iOS 13的VisionKit

      本文链接:https://www.haomeiwen.com/subject/jtiuuhtx.html