From 1f568de828d36cf17c182e27e9fb3b48ed6e7551 Mon Sep 17 00:00:00 2001 From: Himanshu Gupta Date: Sat, 18 May 2024 13:02:24 +0530 Subject: [PATCH] added via props in Scanner Image Upload --- __app/component/Scanner/Scanner.js | 1 - __app/component/Scanner/ScannerClose.js | 31 ++--- __app/component/Scanner/ScannerFlash.js | 27 +--- __app/component/Scanner/ScannerImageUpload.js | 130 ++++++++++++++++++ __app/component/Scanner/index.js | 1 + 5 files changed, 147 insertions(+), 43 deletions(-) create mode 100644 __app/component/Scanner/ScannerImageUpload.js diff --git a/__app/component/Scanner/Scanner.js b/__app/component/Scanner/Scanner.js index 7bed684..ff6d34d 100644 --- a/__app/component/Scanner/Scanner.js +++ b/__app/component/Scanner/Scanner.js @@ -173,7 +173,6 @@ function Scanner({ return isBrowserSupport() && (
- { React.Children.map(children, (child) => React.cloneElement(child, { zIndex, diff --git a/__app/component/Scanner/ScannerClose.js b/__app/component/Scanner/ScannerClose.js index 872dcdf..f39568f 100644 --- a/__app/component/Scanner/ScannerClose.js +++ b/__app/component/Scanner/ScannerClose.js @@ -1,16 +1,17 @@ import React from 'react'; function ScannerClose({ - onClose, - children, zIndex, allClear, - top, - bottom, - left, - right, - color, - position, + color = 'white', + top = 'auto', + bottom = '25%', + left = 'auto', + right = '72%', + position = 'absolute', + + onClose = () => {}, + children, }) { const setClose = () => { onClose(); @@ -39,20 +40,6 @@ function ScannerClose({ ); } -ScannerClose.propTypes = { - -}; - -ScannerClose.defaultProps = { - color: 'white', - top: 'auto', - bottom: '25%', - left: 'auto', - right: '72%', - position: 'absolute', - onClose: () => {}, -}; - export { ScannerClose }; export default ScannerClose; diff --git a/__app/component/Scanner/ScannerFlash.js b/__app/component/Scanner/ScannerFlash.js index 4aa5d97..e5071da 100644 --- a/__app/component/Scanner/ScannerFlash.js +++ b/__app/component/Scanner/ScannerFlash.js @@ -1,15 +1,15 @@ import React from 'react'; function ScannerFlash({ + zIndex, toggleFlash, + color = 'white', + top = 'auto', + bottom = '25%', + left = 'auto', + right = '5%', + position = 'absolute', children, - zIndex, - top, - bottom, - left, - right, - color, - position, }) { return (
{ + const file = event.target.files[0]; + + if ('BarcodeDetector' in globalThis) { + const WindowBarcodeDetector = globalThis.BarcodeDetector; + const barcodeDetector = new WindowBarcodeDetector(); + + createImageBitmap(file) + .then((image) => barcodeDetector.detect(image)) + .then((results) => { + const barcode = results[0]; + if (barcode) { + handleSuccess({ + msgType: 'SUCCESSFUL', + msg: successMsg, + successCb, + data: { barCodeValue: barcode.rawValue, barCodeType: barcode.format }, + }); + } else { + return handleError({ msgType: 'INVALID_IMAGE', msg: failureMsg.invalidImage, failureCb }); + } + return true; + }) + .catch((error) => handleError({ msgType: 'UNABLE_TO_SCAN', msg: failureMsg.unableToScan || JSON.stringify(error), failureCb })); + } + }; + + const processBy = () => { + const attr = {}; + if (via === 'gallery') { + attr.accept = 'image/*'; + } + + if (via === 'camera') { + attr.accept = '*'; + attr.capture = 'filesystem'; + } + + if (via === 'phone') { + attr.accept = 'image/png,image/jpeg'; + attr.capture = 'filesystem'; + } + + return attr; + }; + + return ( + + ); +} + +ScannerImageUpload.defaultProps = { + successCb: () => {}, + failureCb: () => {}, + successMsg: '', + failureMsg: { + unSupported: '', + streaming: '', + barCodeDetection: '', + invalidImage: '', + flash: '', + unableToScan: '', + }, + via: 'gallery', + zIndex: 9, + color: 'white', + top: 'auto', + bottom: '18%', + left: 'auto', + right: '64%', + position: 'absolute', +}; + +export { ScannerImageUpload }; + +export default ScannerImageUpload; + +// +// + +// const generateImage = async () => { +// const resp = await fetch('https://i.postimg.cc/br85dpb4/Untitled-1.png'); +// if (!resp.ok) { +// return console.error('Network error', resp.status); +// } +// const blob = await resp.blob(); +// console.log(blob); + +// const bmp = await createImageBitmap(blob); +// return bmp; +// }; diff --git a/__app/component/Scanner/index.js b/__app/component/Scanner/index.js index 08c7e17..bd014c0 100644 --- a/__app/component/Scanner/index.js +++ b/__app/component/Scanner/index.js @@ -5,5 +5,6 @@ export * from './ScannerFacing'; export * from './ScannerGallery'; export * from './ScannerCamera'; export * from './ScannerClose'; +export * from './ScannerImageUpload'; export { default } from './Scanner';