react-native-scanbot-sdk
Version:
Scanbot Document and Barcode Scanner SDK React Native Plugin for Android and iOS
139 lines (138 loc) • 7.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ScanbotBarcodeCameraView = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _barcode = require("../../barcode");
var _imageRef = require("../../imageRef");
var _ScanbotBarcodeScannerViewNativeComponent = _interopRequireWildcard(require("../spec/ScanbotBarcodeScannerViewNativeComponent"));
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 && {}.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 ScanbotBarcodeCameraView = exports.ScanbotBarcodeCameraView = /*#__PURE__*/(0, _react.forwardRef)(({
onBarcodeScannerResult,
onBarcodeTap,
...props
}, forwardedRef) => {
const viewRef = (0, _react.useRef)(null);
const boundBarcodeItemOverlayViews = (0, _react.useRef)(new Map());
(0, _react.useImperativeHandle)(forwardedRef, () => {
return {
freezeCamera() {
if (viewRef.current) {
_ScanbotBarcodeScannerViewNativeComponent.Commands.freezeCamera(viewRef.current);
}
},
unfreezeCamera() {
if (viewRef.current) {
_ScanbotBarcodeScannerViewNativeComponent.Commands.unfreezeCamera(viewRef.current);
}
}
};
}, []);
const _onBarcodeScannerResult = (0, _react.useCallback)(async event => {
const result = event.nativeEvent.result;
if (result) {
const barcodeItems = _reactNative.Platform.OS === 'ios' ? JSON.parse(result) : result;
(0, _imageRef.autorelease)(() => {
onBarcodeScannerResult(barcodeItems.map(item => new _barcode.BarcodeItem(item)));
});
if (props.selectionOverlayConfig?.barcodeItemOverlayViewBinder) {
for (let barcode of barcodeItems) {
const barcodeItemUuid = barcode.text + barcode.upcEanExtension + '_' + barcode.format;
const barcodeItemNextBind = boundBarcodeItemOverlayViews.current.get(barcodeItemUuid);
if (barcodeItemNextBind === undefined || barcodeItemNextBind !== -1 && Date.now() >= barcodeItemNextBind) {
boundBarcodeItemOverlayViews.current.set(barcodeItemUuid, -1);
const bindingConfig = await props.selectionOverlayConfig.barcodeItemOverlayViewBinder(barcode);
if (bindingConfig.refreshRate !== undefined && bindingConfig.refreshRate > 0) {
boundBarcodeItemOverlayViews.current.set(barcodeItemUuid, Date.now() + bindingConfig.refreshRate);
}
if (viewRef.current != null) {
_ScanbotBarcodeScannerViewNativeComponent.Commands.bindBarcodeItemOverlayView(viewRef.current, barcodeItemUuid, JSON.stringify(bindingConfig));
}
}
}
} else {
boundBarcodeItemOverlayViews.current.clear();
}
}
}, [props.selectionOverlayConfig, boundBarcodeItemOverlayViews, onBarcodeScannerResult]);
const _onBarcodeSelectionResult = (0, _react.useCallback)(event => {
const result = event.nativeEvent.result;
if (result && onBarcodeTap) {
(0, _imageRef.autorelease)(() => {
onBarcodeTap(new _barcode.BarcodeItem(_reactNative.Platform.OS === 'ios' ? JSON.parse(result) : result));
});
}
}, [onBarcodeTap]);
return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: [styles.container, props.style]
}, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
pointerEvents: 'box-none',
style: [styles.contentContainerStyle, props.contentContainerStyle]
}, props.children), /*#__PURE__*/_react.default.createElement(_ScanbotBarcodeScannerViewNativeComponent.default
//FinderConfigs
, {
finderEnabled: props.finderConfig?.viewFinderEnabled,
finderStrokeWidth: props.finderConfig?.finderLineWidth,
finderMinPadding: props.finderConfig?.minPadding,
finderStrokeColor: props.finderConfig?.finderLineColor,
finderOverlayColor: props.finderConfig?.overlayColor,
finderInset: props.finderConfig?.finderInset,
finderRequiredAspectRatios: props.finderConfig?.requiredAspectRatio
//CameraConfigs
,
cameraZoomFactor: _reactNative.Platform.select({
ios: props.cameraConfig?.cameraZoomFactor ? parseFloat(props.cameraConfig?.cameraZoomFactor.toFixed(2)) : undefined,
android: props.cameraConfig?.cameraZoomFactor !== undefined ? (props.cameraConfig?.cameraZoomRange?.minZoom || 1) + ((props.cameraConfig?.cameraZoomRange?.maxZoom || 12) - (props.cameraConfig?.cameraZoomRange?.minZoom || 1)) * props.cameraConfig?.cameraZoomFactor : undefined
}),
cameraZoomRange: props.cameraConfig?.cameraZoomRange,
minFocusDistanceLock: props.cameraConfig?.minFocusDistanceLock,
cameraModule: props.cameraConfig?.cameraModule
//SelectionOverlayConfig
,
overlayEnabled: props.selectionOverlayConfig?.overlayEnabled,
overlayPolygonColor: props.selectionOverlayConfig?.polygonColor,
overlayStrokeColor: props.selectionOverlayConfig?.strokeColor,
overlayTextFormat: props.selectionOverlayConfig?.textFormat,
overlayTextColor: props.selectionOverlayConfig?.textColor,
overlayTextContainerColor: props.selectionOverlayConfig?.textContainerColor,
overlayLoadingTextValue: props.selectionOverlayConfig?.loadingTextValue,
overlayBarcodeItemOverlayViewBinder: props.selectionOverlayConfig?.barcodeItemOverlayViewBinder !== undefined
//Barcode Scanner Configuration
,
configFormatConfigurations: props.barcodeScannerConfiguration?.barcodeFormatConfigurations,
configExtractedDocumentFormats: props.barcodeScannerConfiguration?.extractedDocumentFormats,
configOnlyAcceptDocuments: props.barcodeScannerConfiguration?.onlyAcceptDocuments,
configReturnBarcodeImage: props.barcodeScannerConfiguration?.returnBarcodeImage,
configEngineMode: props.barcodeScannerConfiguration?.engineMode,
configAccumulation: props.barcodeScannerConfiguration?.accumulationConfig,
configOptimizedForOverlays: props.barcodeScannerConfiguration?.optimizedForOverlays
//Other
,
flashEnabled: props.flashEnabled,
scanningEnabled: props.scanningEnabled,
hardwareButtonsEnabled: props.hardwareButtonsEnabled
//View
,
style: styles.container,
ref: viewRef
//Result
,
onBarcodeScannerResult: _onBarcodeScannerResult,
onSelectBarcodeResult: _onBarcodeSelectionResult
}));
});
const styles = _reactNative.StyleSheet.create({
container: {
flex: 1
},
contentContainerStyle: {
position: 'absolute',
width: '100%',
height: '100%',
zIndex: 2
}
});
//# sourceMappingURL=ScanbotBarcodeCameraView.js.map