iOS小技能:iOS13 證件掃描 & 文字識別API

語言: CN / TW / HK

本文正在參加「金石計劃 . 瓜分6萬現金大獎」

引言

從CSDN下載Demo原始碼:http://download.csdn.net/download/u011018979/19262418

  1. 應用場景:證件掃描、文字識別
  2. 原理:利用iOS13 VNDocumentCameraViewController的證件掃描和VNRecognizeTextRequest文字識別功能進行實現
  3. 原理文章:http://kunnan.blog.csdn.net/article/details/117414243

在這裡插入圖片描述

I 、 iOS13 證件掃描API

VisionKit的VNDocumentCameraViewController

```objectivec API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(macos, tvos, watchos) @interface VNDocumentCameraViewController : UIViewController

```

II、iOS13 文字識別API

Vision的 VNRecognizeTextRequest ```objectivec API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0)) @interface VNRecognizeTextRequest : VNImageBasedRequest

``` 效果圖:

III 案例

```objectivec

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSArray *requests; dispatch_queue_t textRecognitionWorkQueue;

NSString *resultingText;

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.

    // Parar e esconder o Indicador de atividade do OCR [self->activityIndicator stopAnimating]; self->activityIndicator.hidden = YES;

    // Solicitar que o Vision seja executado em cada página do documento digitalizado. requests = [[NSArray alloc] init];

    // Cria a fila de expedição para executar solicitações do Vision. dispatch_queue_attr_t qos = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1); textRecognitionWorkQueue = dispatch_queue_create("TextRecognitionQueue", qos);

    resultingText = @"";

    [self setupVision]; }

// Solicita o Setup do Vision, pois a solicitação pode ser reutilizada - (void)setupVision { VNRecognizeTextRequest *textRecognitionRequest = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {

    NSMutableArray *observations;
    @try {
        observations  = [[NSMutableArray alloc] init];
        for (VNRecognizedTextObservation *obs in request.results) {
            [observations addObject:(VNRecognizedTextObservation *)obs];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"As observações são de um tipo inesperado.");
    }
    @finally {
        //NSLog(@"Condição final");
    }

    // Concatena o texto reconhecido de todas as observações.
    NSInteger maximumCandidates = 1;
    for (VNRecognizedTextObservation *observation in observations) {
        VNRecognizedText *candidate = [observation topCandidates:maximumCandidates].firstObject;
        resultingText = [NSString stringWithFormat:@"%@%@",
                         resultingText,
                         candidate.string];
    }
}];
// Especifica o nível de reconhecimento
textRecognitionRequest.recognitionLevel = VNRequestTextRecognitionLevelAccurate;
requests = @[textRecognitionRequest];

}

  • (IBAction)scanReceipts:(id)sender { //Cria uma instancia da Classe de Leitura de Docs da Vision, e abre ela VNDocumentCameraViewController *documentCameraViewController = [[VNDocumentCameraViewController alloc] init]; documentCameraViewController.delegate = self;

    [self presentModalViewController:documentCameraViewController animated:YES]; }

// MARK: VNDocumentCameraViewControllerDelegate

  • (void)documentCameraViewController:(VNDocumentCameraViewController )controller didFinishWithScan:(VNDocumentCameraScan )scan { // Limpe qualquer texto existente. self->textView.text = @""; // Descartar a câmera de documentos [controller dismissModalViewControllerAnimated:YES];

    self->activityIndicator.hidden = NO; [self->activityIndicator startAnimating];

    dispatch_async(textRecognitionWorkQueue, ^{ resultingText = @""; for (int pageIndex=0; pageIndextextView.text = resultingText; [self->activityIndicator stopAnimating]; self->activityIndicator.hidden = YES; }); }); }

@end

```

see also

iOS13掃描證件&銀行卡資訊識別;身份證識別 (正反) ;矩形邊緣識別 ;自定義證件相機 (含demo原始碼)

———————————————— 版權宣告:本文為CSDN博主「#公眾號:iOS逆向」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。 原文連結:http://blog.csdn.net/z929118967/article/details/111197419