diva-mobile-vision-camera-mrz-scanner
Version:
VisionCamera Frame Processor Plugin to detect and read MRZ data from passports using MLKit Text Recognition.
211 lines (207 loc) • 9.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _divaMobileVisionCameraMrzScanner = require("diva-mobile-vision-camera-mrz-scanner");
var _mrzParser = require("../util/mrzParser");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const MRZScanner = _ref => {
let {
style,
skipButtonEnabled,
skipButton,
onSkipPressed,
skipButtonStyle,
cameraProps,
onData,
skipButtonText,
mrzFinalResults,
enableMRZFeedBack,
numberOfQAChecks,
enableBoundingBox,
mrzFeedbackCompletedColor,
mrzFeedbackUncompletedColor,
mrzFeedbackContainer,
mrzFeedbackTextStyle,
isActiveCamera,
languages
} = _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: {
height: 60,
position: 'absolute',
top: '60%',
backgroundColor: 'rgba(200,200,200,0.8)',
width: '100%',
textAlignVertical: 'center'
},
feedbackText: {
color: 'white',
fontSize: 10,
justifyContent: 'center',
width: '33.3%',
textAlignVertical: 'center'
},
flexRow: {
flexDirection: 'row',
alignSelf: 'center',
height: 30,
justifyContent: 'flex-start'
},
givenNamesQAList: {
color: statusCheck(givenNamesQAList.length)
},
lastNamesQAList: {
color: statusCheck(lastNamesQAList.length)
},
nationalityQAList: {
color: statusCheck(nationalityQAList.length)
},
idNumberQAList: {
color: statusCheck(idNumberQAList.length)
}
});
return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
testID: "scanDocumentView",
style: _reactNative.StyleSheet.absoluteFill
}, /*#__PURE__*/_react.default.createElement(_divaMobileVisionCameraMrzScanner.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);
}
}
}
},
languages: languages,
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 && languages ? /*#__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]
}, `${languages[0].NAME} ${givenNamesQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.feedbackText, styles.lastNamesQAList, mrzFeedbackTextStyle]
}, `${languages[0].LAST_NAME} ${lastNamesQAList.length} / ${numQAChecks}`)), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: styles.flexRow
}, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.feedbackText, styles.nationalityQAList, mrzFeedbackTextStyle]
}, `${languages[0].NATIONALITY} ${nationalityQAList.length} / ${numQAChecks}`), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
style: [styles.feedbackText, styles.idNumberQAList, mrzFeedbackTextStyle]
}, `${languages[0].PASSPORT_NO} ${idNumberQAList.length} / ${numQAChecks}`))) : undefined);
};
var _default = exports.default = MRZScanner;
//# sourceMappingURL=MRZScanner.js.map