Developer's Documentation for free mobile OCR SDK

Documentation Menu

Capture a Custom Data Field: iOS

This section contains a step-by-step guide to creating an application that captures a single field which conforms to a certain regular expression. This could be a date, some code, etc. The more specific the data is, the easier it would be to capture it. In this guide we will capture an alphanumeric code, for example: X6Yz647a.

See also Build your application with the OCR library for iOS.

Implementing the delegates

  1. Implement a delegate conforming to the RTRDataCaptureServiceDelegate protocol. The delegate will handle messages from the data capture service. Here are the recommendations on what its methods should do:
  2. Implement a delegate that adopts the AVCaptureVideoDataOutputSampleBufferDelegate protocol. Instantiate an AVCaptureSession object, add video input and output and set the video output delegate. When the delegate receives a video frame via the captureOutput:didOutputSampleBuffer:fromConnection: method, pass this frame on to the data capture service by calling the addSampleBuffer: method of the RTRDataCaptureService object.
    We recommend using the AVCaptureSessionPreset1280x720 preset for your AVCaptureSession.
    Also note that your video output must be configured to use the kCVPixelFormatType_32BGRA video pixel format.

Loading the library and setting up the service

  1. Create an RTREngine object using the sharedEngineWithLicenseData: method. The method requires an NSData object containing your license data. For example, you can use dataWithContentsOfFile: to create a data object, then pass this object to the sharedEngineWithLicenseData: method.
  2. Use the createDataCaptureServiceWithDelegate:profile: method of the RTREngine object to create a background recognition service. The profile parameter should be left empty.
    Only one instance of the service per application is necessary: multiple threads will be started internally.
  3. Call the configureDataCaptureProfile method of the RTRDataCaptureService object to create an RTRDataCaptureProfileBuilder object. Create a data scheme builder using the addScheme: method. The scheme builder will allow you to specify a human-readable name for the scheme and to add field definitions.
  4. Use the addField: method to create a new field builder. Use setName: to add a human-readable field name and setRegEx: to specify the regular expression that should match the field data. In the case of an alphanumeric code the regEx parameter to setRegEx: is @"([a-zA-Z]+[0-9]+|[0-9]+[a-zA-Z]+)[0-9a-zA-Z]*"
    noteNote: The regular expression syntax is derived from ICU regular expressions, with some minor features not supported. See the Regular Expressions section in the ICU User Guide.

    An alphanumeric code needs no additional check besides the regular expression. However, there is the option of implementing a block which would perform additional validation after the data has passed the regular expression check, for example, calculate the field's checksum (see the setPredicateBlock: method).

  5. Call the checkAndApply method of the RTRDataCaptureProfileBuilder object to submit the profile for use in the data capture service. If an error is returned at this stage, it is probable the regular expression has mistakes in the syntax, please check it again.
  6. We recommend also calling the setAreaOfInterest: method to specify the rectangular area on the frame where the field is likely to be found. For example, your application may show a highlighted rectangle in the UI into which the end user will try to fit the page they are capturing. The best result is achieved when the area of interest does not touch the boundaries of the frame but has a margin of at least half the size of a typical printed character.

Processing

  1. Process the messages sent by the service to the RTRDataCaptureServiceDelegate delegate object. The result will be delivered via the onBufferProcessedWithDataScheme:dataFields:resultStatus: method:
    • An RTRDataScheme object. Its id property should return the same identifier you specified in the custom profile.
      warningImportant! If nil is passed instead of a valid RTRDataScheme object, the data scheme has not yet been matched, which may mean that there is no data of the required type in the area of interest. In this case, the results are not usable.
    • An array containing, in this case, one RTRDataField object which represents the extracted field. It provides the identifier and the human-readable name for the field, the field text, and its location.
    • The result stability status, which indicates if the result is available and if it is likely to be improved by adding further frames. Use it to determine whether the application should stop processing and display the result to the user. We do not recommend using the result until the stability level has reached at least RTRResultStabilityAvailable and the data scheme has been matched.
  2. Save the results. Call the stopTasks method to stop processing and clean up image buffers. The data capture service keeps its configuration settings (the custom profile, the area of interest) and necessary resources. The processing will start automatically on the new call to the addSampleBuffer: method.

See the description of classes and methods in the API Reference section.