Skip to content

Commit

Permalink
fix(scanner): scanner component added Camera click
Browse files Browse the repository at this point in the history
  • Loading branch information
opensrc0 committed Mar 5, 2024
1 parent 38bab9a commit ffd46e2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
100 changes: 100 additions & 0 deletions __app/component/Scanner/CameraClick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React from 'react';
import { handleSuccess, handleError } from '../services/handler';

function CameraClick({
disbaleToast,
successCb,
successMsg,
failureCb,
failureMsg,
children,
zIndex,
top,
bottom,
left,
right,
color,
position,
}) {
const onClickImage = async (event) => {
const file = event.target.files[0];
if ('BarcodeDetector' in globalThis) {
const WindowBarcodeDetector = window.BarcodeDetector;
const barcodeDetector = new WindowBarcodeDetector();

createImageBitmap(file)
.then((image) => barcodeDetector.detect(image))
.then((results) => {
const barcode = results[0];
if (barcode) {
handleSuccess({
disbaleToast,
msgType: 'SUCCESS',
msg: successMsg,
successCb,
data: { barCodeValue: barcode.rawValue, barCodeType: barcode.format },
});
} else {
handleError({ disbaleToast, msgType: 'INVALID_IMAGE', msg: failureMsg.invalidImage, failureCb });
}
})
.catch((error) => {
handleError({ disbaleToast, msgType: 'UNABLE_TO_SCAN', msg: failureMsg.unableToScan || error, failureCb });
});
}
};

return (
<label
htmlFor="click-photo"
style={{
top,
bottom,
left,
right,
zIndex,
color,
position,
}}
>
{children || 'Camera'}
<input
id="click-photo"
type="file"
accept="*"
capture="filesystem"
onChange={onClickImage}
onSelect={onClickImage}
style={{ display: 'none' }}
/>
</label>
);
}

CameraClick.propTypes = {

};

CameraClick.defaultProps = {
disbaleToast: false,
successCb: () => {},
failureCb: () => {},
successMsg: '',
failureMsg: {
unSupported: '',
streaming: '',
barCodeDetection: '',
invalidImage: '',
flash: '',
unableToScan: '',
},
zIndex: 9,
color: 'white',
top: 'auto',
bottom: '18%',
left: 'auto',
right: '25%',
position: 'absolute',
};

export default CameraClick;
6 changes: 3 additions & 3 deletions __app/component/Scanner/PhoneGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function PhoneGallery({

return (
<label
htmlFor="upload-photo"
htmlFor="upload-gallery-photo"
style={{
top,
bottom,
Expand All @@ -60,9 +60,9 @@ function PhoneGallery({
>
{children || 'Gallery'}
<input
id="upload-photo"
id="upload-gallery-photo"
type="file"
accept="image/png,image/jpeg"
accept="image/*"
onChange={onSelectGalleryImage}
onSelect={onSelectGalleryImage}
style={{ display: 'none' }}
Expand Down
2 changes: 2 additions & 0 deletions __app/component/Scanner/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CameraClose from './CameraClose';
import CameraScanBox from './CameraScanBox';
import CameraFacing from './CameraFacing';
import PhoneGallery from './PhoneGallery';
import CameraClick from './CameraClick';

export default {
StartCamera,
Expand All @@ -12,4 +13,5 @@ export default {
CameraScanBox,
CameraFacing,
PhoneGallery,
CameraClick,
};
1 change: 0 additions & 1 deletion __app/component/Scanner/StartCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ function StartCamera({
};

const allClear = () => {
console.log('unmount');
cancelAnimationFrame(videoUnmount);
stopStreaming();
clearTimeout(unmoutRenderLoop);
Expand Down

0 comments on commit ffd46e2

Please sign in to comment.