UNPKG

vision-camera-mrz-scanner

Version:

VisionCamera Frame Processor Plugin to detect and read MRZ data from passports using MLKit Text Recognition.

250 lines (246 loc) 12.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _visionCameraMrzScanner = require("vision-camera-mrz-scanner"); var _mrzParser = require("../util/mrzParser"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const MRZScanner = _ref => { let { style, skipButtonEnabled, skipButton, onSkipPressed, skipButtonStyle, cameraProps, onData, skipButtonText, mrzFinalResults, enableMRZFeedBack, numberOfQAChecks, enableBoundingBox, mrzFeedbackCompletedColor, mrzFeedbackUncompletedColor, mrzFeedbackContainer, mrzFeedbackTextStyle, isActiveCamera } = _ref; //***************************************************************************************** // setting up the state //***************************************************************************************** const numQAChecks = numberOfQAChecks ?? 3; const [scanSuccess, setScanSuccess] = (0, _react.useState)(false); const [docMRZQAList, setDocMRZQAList] = (0, _react.useState)([]); const [docTypeQAList, setDocTypeQAList] = (0, _react.useState)([]); const [issuingCountryQAList, setIssuingCountryQAList] = (0, _react.useState)([]); const [givenNamesQAList, setGivenNamesQAList] = (0, _react.useState)([]); const [lastNamesQAList, setLastNamesQAList] = (0, _react.useState)([]); const [idNumberQAList, setIdNumberQAList] = (0, _react.useState)([]); const [nationalityQAList, setNationalityQAList] = (0, _react.useState)([]); const [dobQAList, setDobQAList] = (0, _react.useState)([]); const [genderQAList, setGenderQAList] = (0, _react.useState)([]); const [docExpirationDateQAList, setDocExpirationDateQAList] = (0, _react.useState)([]); const [isActive, setIsActive] = (0, _react.useState)(true); const [additionalInformationQAList, setAdditionalInformationQAList] = (0, _react.useState)([]); (0, _react.useEffect)(() => { return () => { setIsActive(false); }; }, []); /** * If all elements in list match element, add the new element. * If not, empty the list, then add the new element to the list. * @param list * @param element */ const mrzQACheck = (list, element) => { let newList = [...list]; for (let i = 0; i < list.length; i++) { if (list[i] !== element) { newList = []; } } newList.push(element); return newList; }; /** * Returns true if all QALists are full (their sizes are >= numberOfPreviousMRZsToCompareTo). * If one or more of them are not full, it updates them with the most recently captured field that pertains to them. * @param numberOfPreviousMRZsToCompareTo * @param mrzResults */ const currentMRZMatchesPreviousMRZs = (numberOfPreviousMRZsToCompareTo, mrzResults) => { if (docMRZQAList.length >= 1 && docTypeQAList.length >= numberOfPreviousMRZsToCompareTo && issuingCountryQAList.length >= numberOfPreviousMRZsToCompareTo && givenNamesQAList.length >= numberOfPreviousMRZsToCompareTo && lastNamesQAList.length >= numberOfPreviousMRZsToCompareTo && idNumberQAList.length >= numberOfPreviousMRZsToCompareTo && nationalityQAList.length >= numberOfPreviousMRZsToCompareTo && dobQAList.length >= numberOfPreviousMRZsToCompareTo && genderQAList.length >= numberOfPreviousMRZsToCompareTo && docExpirationDateQAList.length >= numberOfPreviousMRZsToCompareTo && issuingCountryQAList.length >= numberOfPreviousMRZsToCompareTo) { return true; } if (givenNamesQAList.length < numberOfPreviousMRZsToCompareTo) { setGivenNamesQAList(mrzQACheck(givenNamesQAList, mrzResults.givenNames)); } if (lastNamesQAList.length < numberOfPreviousMRZsToCompareTo) { setLastNamesQAList(mrzQACheck(lastNamesQAList, mrzResults.lastNames)); } if (idNumberQAList.length < numberOfPreviousMRZsToCompareTo) { setIdNumberQAList(mrzQACheck(idNumberQAList, mrzResults.idNumber)); } if (nationalityQAList.length < numberOfPreviousMRZsToCompareTo) { setNationalityQAList(mrzQACheck(nationalityQAList, mrzResults.nationality)); } if (dobQAList.length < numberOfPreviousMRZsToCompareTo) { setDobQAList(mrzQACheck(dobQAList, mrzResults.dob)); } if (genderQAList.length < numberOfPreviousMRZsToCompareTo) { setGenderQAList(mrzQACheck(genderQAList, mrzResults.gender)); } if (issuingCountryQAList.length < numberOfPreviousMRZsToCompareTo) { setIssuingCountryQAList(mrzQACheck(issuingCountryQAList, mrzResults.issuingCountry)); } if (docTypeQAList.length < numberOfPreviousMRZsToCompareTo) { setDocTypeQAList(mrzQACheck(docTypeQAList, mrzResults.docType)); } if (docExpirationDateQAList.length < numberOfPreviousMRZsToCompareTo) { setDocExpirationDateQAList(mrzQACheck(docExpirationDateQAList, mrzResults.docExpirationDate)); } if (additionalInformationQAList.length < numberOfPreviousMRZsToCompareTo) { setAdditionalInformationQAList(mrzQACheck(additionalInformationQAList, mrzResults.additionalInformation)); } if (docMRZQAList.length < 1) { setDocMRZQAList(mrzQACheck(docMRZQAList, mrzResults.docMRZ)); } return false; }; const statusCheck = (completedQAChecks, numOfChecks) => { if (numOfChecks === undefined) { numOfChecks = numQAChecks; } if (completedQAChecks === numOfChecks) { return mrzFeedbackCompletedColor ?? 'rgba(53,94,59,1.0)'; } else { return mrzFeedbackUncompletedColor ?? 'white'; } }; const styles = _reactNative.StyleSheet.create({ feedbackContainer: { position: 'absolute', top: 55, alignItems: 'flex-start', backgroundColor: 'rgba(200,200,200,0.8)', width: '100%', textAlignVertical: 'center' }, feedbackText: { color: 'white', fontSize: 10, textAlign: 'center', width: '33.3%', paddingTop: 10, textAlignVertical: 'center', height: '100%' }, flexRow: { flexDirection: 'row' }, givenNamesQAList: { color: statusCheck(givenNamesQAList.length) }, lastNamesQAList: { color: statusCheck(lastNamesQAList.length) }, dobQAList: { color: statusCheck(dobQAList.length) }, nationalityQAList: { color: statusCheck(nationalityQAList.length) }, idNumberQAList: { color: statusCheck(idNumberQAList.length) }, issuingCountryQAList: { color: statusCheck(issuingCountryQAList.length) }, docExpirationDateQAList: { color: statusCheck(docExpirationDateQAList.length) }, additionalInformationQAList: { color: statusCheck(additionalInformationQAList.length) }, docMRZQAList: { color: statusCheck(docMRZQAList.length, 1) }, genderQAList: { color: statusCheck(genderQAList.length), paddingBottom: 10 }, docTypeQAList: { color: statusCheck(docTypeQAList.length), paddingBottom: 10 } }); return /*#__PURE__*/_react.default.createElement(_reactNative.View, { testID: "scanDocumentView", style: _reactNative.StyleSheet.absoluteFill }, /*#__PURE__*/_react.default.createElement(_visionCameraMrzScanner.MRZCamera, { onData: lines => { if (onData) { onData(lines); } else { const mrzResults = (0, _mrzParser.parseMRZ)(lines); if (mrzResults) { if (currentMRZMatchesPreviousMRZs(numQAChecks, mrzResults)) { setScanSuccess(true); setIsActive(false); mrzFinalResults(mrzResults); } } } }, scanSuccess: scanSuccess, skipButtonText: skipButtonText, style: [style ? style : _reactNative.StyleSheet.absoluteFill], skipButtonEnabled: skipButtonEnabled, skipButtonStyle: skipButtonStyle, skipButton: skipButton, onSkipPressed: onSkipPressed, cameraProps: cameraProps, enableBoundingBox: enableBoundingBox, isActiveCamera: isActiveCamera ?? isActive // if isActiveCamera is not defined, use the internal state }), enableMRZFeedBack ? /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [styles.feedbackContainer, mrzFeedbackContainer] }, /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.flexRow }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.givenNamesQAList, mrzFeedbackTextStyle] }, `Given name ${givenNamesQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.lastNamesQAList, mrzFeedbackTextStyle] }, `Last name ${lastNamesQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.dobQAList, mrzFeedbackTextStyle] }, `DOB ${dobQAList.length} / ${numQAChecks}`)), /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.flexRow }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.nationalityQAList, mrzFeedbackTextStyle] }, `Nationality ${nationalityQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.idNumberQAList, mrzFeedbackTextStyle] }, `ID Number ${idNumberQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.issuingCountryQAList, mrzFeedbackTextStyle] }, `Issuing Country ${issuingCountryQAList.length} / ${numQAChecks}`)), /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.flexRow }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.docExpirationDateQAList, mrzFeedbackTextStyle] }, `Expiration Date ${docExpirationDateQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.additionalInformationQAList, mrzFeedbackTextStyle] }, `Additional Info ${additionalInformationQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.docMRZQAList, mrzFeedbackTextStyle] }, `DocMRZ ${docMRZQAList.length} / ${1}`)), /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.flexRow }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.genderQAList, mrzFeedbackTextStyle] }, `Gender ${genderQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, { style: [styles.feedbackText, styles.docTypeQAList, mrzFeedbackTextStyle] }, `DocType ${docTypeQAList.length} / ${numQAChecks}`))) : undefined); }; var _default = MRZScanner; exports.default = _default; //# sourceMappingURL=MRZScanner.js.map