|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { Platform, NativeModules } from 'react-native'; |
| 4 | + |
| 5 | +import { RecognizerResultState } from './recognizer' |
| 6 | + |
| 7 | +const BlinkInputNative = Platform.select({ |
| 8 | + ios: NativeModules.BlinkInputIos, |
| 9 | + android: NativeModules.BlinkInputAndroid |
| 10 | +}) |
| 11 | + |
| 12 | +/** |
| 13 | + * This exposes the appropriate native BlinkInput module module as a JS module, based on |
| 14 | + * detected platform: Android or iOS. This has functions: |
| 15 | + * -> 'scanWithCamera' which takes the following parameters: |
| 16 | + * 1. Object overlaySettings: instance of OverlaySettings, contains settings for desired camera overlay |
| 17 | + * 2. RecognizerCollection recognizerCollection: object containing recognizers to use for scanning |
| 18 | + * 3. String license: BlinkInput base64 license key bound to application ID for Android or iOS. To obtain |
| 19 | + * valid license key, please visit http://microblink.com/login or |
| 20 | + * contact us at http://help.microblink.com |
| 21 | + * |
| 22 | + * OR |
| 23 | + * |
| 24 | + * Object license: containing: |
| 25 | + * - mandatory parameter 'licenseKey': base64 license key bound to application ID |
| 26 | + * for Android or iOS. To obtain valid license key, please visit |
| 27 | + * http://microblink.com/login or contact us at http://help.microblink.com |
| 28 | + * - optioanl parameter 'licensee' when license for multiple apps is used |
| 29 | + * - optional flag 'showTimeLimitedLicenseKeyWarning' which indicates |
| 30 | + * whether warning for time limited license key will be shown |
| 31 | + * in format |
| 32 | + * { |
| 33 | + * licenseKey: '<base64iOSLicense or base64AndroidLicense>', |
| 34 | + * licensee: String, |
| 35 | + * showTimeLimitedLicenseKeyWarning: Boolean |
| 36 | + * } |
| 37 | + * -> 'captureDocument' which takes following parameters: |
| 38 | + * 1. DocumentCaptureRecognizer documentCaptureRecognizer: object containing settings for document capture recognizer |
| 39 | + * 2. String license: the same as 'license' for 'scanWithCamera' function |
| 40 | + */ |
| 41 | +class BlinkInputWrapper { |
| 42 | + async scanWithCamera(overlaySettings, recognizerCollection, license) { |
| 43 | + try { |
| 44 | + var bla = NativeModules; |
| 45 | + console.log(bla); |
| 46 | + var licenseObject = license; |
| 47 | + if (typeof license === 'string' || license instanceof String) { |
| 48 | + licenseObject = { licenseKey: license }; |
| 49 | + } |
| 50 | + const nativeResults = await BlinkInputNative.scanWithCamera(overlaySettings, recognizerCollection, licenseObject); |
| 51 | + if (nativeResults.length != recognizerCollection.recognizerArray.length) { |
| 52 | + console.log("INTERNAL ERROR: native plugin returned wrong number of results!"); |
| 53 | + return []; |
| 54 | + } else { |
| 55 | + let results = []; |
| 56 | + for (let i = 0; i < nativeResults.length; ++i) { |
| 57 | + // native plugin must ensure types match |
| 58 | + // recognizerCollection.recognizerArray[i].result = recognizerCollection.recognizerArray[i].createResultFromNative(nativeResults[i]); |
| 59 | + |
| 60 | + // unlike Cordova, ReactNative does not allow mutation of user-provided recognizers, so we need to |
| 61 | + // return results and let user handle them manually. |
| 62 | + let result = recognizerCollection.recognizerArray[i].createResultFromNative(nativeResults[i]); |
| 63 | + if (result.resultState != RecognizerResultState.empty) { |
| 64 | + results.push(result); |
| 65 | + } |
| 66 | + } |
| 67 | + return results; |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + console.log(error); |
| 71 | + return []; |
| 72 | + } |
| 73 | + } |
| 74 | + async captureDocument(documentCaptureRecognizer, license) { |
| 75 | + try { |
| 76 | + var licenseObject = license; |
| 77 | + if (typeof license === 'string' || license instanceof String) { |
| 78 | + licenseObject = { licenseKey: license }; |
| 79 | + } |
| 80 | + const nativeDocumentCaptureResult = await BlinkInputNative.captureDocument(documentCaptureRecognizer, licenseObject); |
| 81 | + |
| 82 | + return new DocumentCaptureResult(nativeDocumentCaptureResult); |
| 83 | + } catch (error) { |
| 84 | + console.log(error); |
| 85 | + return []; |
| 86 | + } |
| 87 | + } |
| 88 | + async scanWithFieldByField(fieldByFieldCollection, license) { |
| 89 | + try { |
| 90 | + var licenseObject = license; |
| 91 | + if (typeof license === 'string' || license instanceof String) { |
| 92 | + licenseObject = { licenseKey: license }; |
| 93 | + } |
| 94 | + const nativeFieldByFieldResult = await BlinkInputNative.scanWithFieldByField(fieldByFieldCollection, licenseObject); |
| 95 | + |
| 96 | + let results = []; |
| 97 | + for (let i = 0; i < nativeFieldByFieldResult.length; ++i) { |
| 98 | + let result = new FieldByFieldResult(nativeFieldByFieldResult[i]); |
| 99 | + results.push(result); |
| 100 | + } |
| 101 | + return results; |
| 102 | + } catch (error) { |
| 103 | + console.log(error); |
| 104 | + return []; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export var BlinkInput = new BlinkInputWrapper(); |
| 110 | + |
| 111 | +import { Recognizer } from './recognizer' |
| 112 | + |
| 113 | +/** |
| 114 | + * Represents a collection of recognizer objects. |
| 115 | + */ |
| 116 | +export class RecognizerCollection { |
| 117 | + /** |
| 118 | + * |
| 119 | + * @param recognizerArray Array of recognizer objects that will be used for recognition. Must not be empty! |
| 120 | + */ |
| 121 | + constructor(recognizerArray) { |
| 122 | + /** Array of recognizer objects that will be used for recognition */ |
| 123 | + this.recognizerArray = recognizerArray; |
| 124 | + /** |
| 125 | + * Whether or not it is allowed for multiple recognizers to process the same image. |
| 126 | + * If not, then first recognizer that will be successful in processing the image will |
| 127 | + * end the processing chain and other recognizers will not get the chance to process |
| 128 | + * that image. |
| 129 | + */ |
| 130 | + this.allowMultipleResults = false; |
| 131 | + /** Number of miliseconds after first non-empty result becomes available to end scanning with a timeout */ |
| 132 | + this.milisecondsBeforeTimeout = 10000; |
| 133 | + |
| 134 | + if (!(this.recognizerArray instanceof Array)) { |
| 135 | + throw new Error("recognizerArray must be array of Recognizer objects!"); |
| 136 | + } |
| 137 | + // ensure every element in array is Recognizer |
| 138 | + for (var i = 0; i < this.recognizerArray.length; ++i) { |
| 139 | + if (!(this.recognizerArray[i] instanceof Recognizer )) { |
| 140 | + throw new Error( "Each element in recognizerArray must be instance of Recognizer" ); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +import { Parser } from './parser' |
| 147 | + |
| 148 | +/** |
| 149 | + * Represents a collection of scane elements |
| 150 | + */ |
| 151 | +export class FieldByFieldCollection { |
| 152 | + /** |
| 153 | + * |
| 154 | + * @param fieldByFieldElementArray Array of field by field elements objects that will be used for recognition. Must not be empty! |
| 155 | + */ |
| 156 | + constructor(fieldByFieldElementArray) { |
| 157 | + /** Array of recognizer objects that will be used for recognition */ |
| 158 | + this.fieldByFieldElementArray = fieldByFieldElementArray; |
| 159 | + |
| 160 | + if (!(this.fieldByFieldElementArray instanceof Array)) { |
| 161 | + throw new Error("recognizerArray must be array of Recognizer objects!"); |
| 162 | + } |
| 163 | + // ensure every element in array is Recognizer |
| 164 | + for (var i = 0; i < this.fieldByFieldElementArray.length; ++i) { |
| 165 | + if (!(this.fieldByFieldElementArray[i] instanceof FieldByFieldElement )) { |
| 166 | + throw new Error( "Each element in fieldByFieldElementArray must be instance of FieldByFieldElement" ); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +export class FieldByFieldElement { |
| 173 | + constructor(identifier, parser, localizedTitle, localizedTooltip) { |
| 174 | + /** |
| 175 | + * Unique name of the element |
| 176 | + */ |
| 177 | + this.identifier = identifier; |
| 178 | + |
| 179 | + /** |
| 180 | + Parser object which is reponsible scanning the text. |
| 181 | + */ |
| 182 | + this.parser = parser; |
| 183 | + |
| 184 | + /** |
| 185 | + * Localized title (used in the Pivot control) |
| 186 | + */ |
| 187 | + this.localizedTitle = localizedTitle; |
| 188 | + |
| 189 | + /** |
| 190 | + * Localized tooltip (used in the tooltip label above the viewfinder) |
| 191 | + */ |
| 192 | + this.localizedTooltip = localizedTooltip; |
| 193 | + |
| 194 | + if (!(this.parser instanceof Parser)) { |
| 195 | + throw new Error("Parser must be instance of Parser!"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +export class FieldByFieldResult { |
| 201 | + constructor(nativeFieldByFieldResult) { |
| 202 | + /** |
| 203 | + * Unique name of the element |
| 204 | + */ |
| 205 | + this.identifier = nativeFieldByFieldResult.identifier; |
| 206 | + |
| 207 | + /** |
| 208 | + Value of parser object after scanning. |
| 209 | + */ |
| 210 | + this.value = nativeFieldByFieldResult.value; |
| 211 | + } |
| 212 | +} |
| 213 | +import { DocumentCaptureRecognizerResult } from './recognizers/documentCaptureRecognizer' |
| 214 | +/** |
| 215 | + * Represents a result of 'captureDocument' functionality. |
| 216 | + */ |
| 217 | +export class DocumentCaptureResult { |
| 218 | + constructor(nativeDocumentCaptureResult) { |
| 219 | + this.documentCaptureRecognizerResult = new DocumentCaptureRecognizerResult(nativeDocumentCaptureResult.documentCaptureRecognizerResult); |
| 220 | + this.capturedFullImage = nativeDocumentCaptureResult.capturedFullImage; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +export { RecognizerResultState } from './recognizer' |
| 225 | +export * from './types' |
| 226 | + |
| 227 | +// export overlays that can be used |
| 228 | +export * from './overlays/barcodeOverlays' |
| 229 | + |
| 230 | +// export recognizers that can be used and their results |
| 231 | +export * from './recognizers/successFrameGrabberRecognizer' |
| 232 | +export * from './recognizers/barcodeRecognizer' |
| 233 | +export * from './recognizers/documentCaptureRecognizer' |
| 234 | +export * from './recognizers/pdf417Recognizer' |
| 235 | +export * from './recognizers/simNumberRecognizer' |
| 236 | +// export * from './recognizers/blinkInputRecognizer' |
| 237 | + |
| 238 | +export { ParserResultState } from './parser' |
| 239 | +export { ProcessorResultState } from './processor' |
| 240 | + |
| 241 | +// export parsers that can be used and their results |
| 242 | +export * from './parsers/amountParser' |
| 243 | +export * from './parsers/dateParser' |
| 244 | +export * from './parsers/emailParser' |
| 245 | +export * from './parsers/ibanParser' |
| 246 | +export * from './parsers/licensePlatesParser' |
| 247 | +export * from './parsers/rawParser' |
| 248 | +export * from './parsers/topUpParser' |
| 249 | +export * from './parsers/vinParser' |
| 250 | +export * from './parsers/regexParser' |
| 251 | + |
| 252 | +// // export processors that can be used and their results |
| 253 | +// export * from './processors/parserGroupProcessor' |
| 254 | +// |
| 255 | + |
0 commit comments