Fix barcode scanner

This commit is contained in:
= 2021-01-30 12:35:53 +01:00
parent 049e2fb57e
commit 9856768d11
8 changed files with 45 additions and 111 deletions

View File

@ -1,70 +1 @@
# Getting Started with Create React App #### FITWAVE - frontend
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `yarn start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `yarn build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `yarn build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

View File

@ -5,6 +5,7 @@ import {
DialogContent, DialogContent,
Grid, Grid,
Button, Button,
Typography,
DialogActions, DialogActions,
IconButton, IconButton,
Dialog, Dialog,
@ -63,25 +64,29 @@ const AddProductToMealDialog = ({ mealId, mealLabel }) => {
<BarcodeScanner /> <BarcodeScanner />
</Grid> </Grid>
<List> <List>
{products.map(({ id, verified, eco, salt, brand, servingCapacity, barcode, label, unit, calories, fat, carbohydrates, protein }) => ( {products.length > 0 ? (
<ProductItem products.map(({ id, verified, eco, salt, brand, servingCapacity, barcode, label, unit, calories, fat, carbohydrates, protein }) => (
selected={false} <ProductItem
key={id} selected={false}
id={id} key={id}
verified={verified} id={id}
eco={eco} verified={verified}
label={label} eco={eco}
unit={unit} label={label}
servingCapacity={servingCapacity} unit={unit}
macronutrients={{ servingCapacity={servingCapacity}
salt, macronutrients={{
calories, salt,
fat, calories,
carbohydrates, fat,
protein, carbohydrates,
}} protein,
/> }}
))} />
))
) : (
<Typography variant="caption">Products not found</Typography>
)}
</List> </List>
<DialogActions> <DialogActions>
<Button onClick={handleClose} color="primary"> <Button onClick={handleClose} color="primary">

View File

@ -59,7 +59,7 @@ const Scanner = ({ handleClose, setBarcode }) => {
start: 0.1, start: 0.1,
end: 0.9, end: 0.9,
threshold: 160, threshold: 160,
quality: 0.9, quality: 0.45,
delay: 100, delay: 100,
video: '#barcodevideo', video: '#barcodevideo',
canvas: '#barcodecanvas', canvas: '#barcodecanvas',
@ -87,9 +87,9 @@ const Scanner = ({ handleClose, setBarcode }) => {
} }
const stop = () => { const stop = () => {
elements.video.removeEventListener('canplay', play)
elements.video.pause(); elements.video.pause();
localMediaStream.getTracks()[0].stop(); localMediaStream.getTracks()[0].stop();
elements.video.removeEventListener('canplay', play)
} }
const init = () => { const init = () => {
@ -106,11 +106,11 @@ const Scanner = ({ handleClose, setBarcode }) => {
elements.video.srcObject = stream; elements.video.srcObject = stream;
localMediaStream = stream; localMediaStream = stream;
}, (error) => { }, (error) => {
console.log(error); // console.log(error);
}); });
} }
elements.video.addEventListener('canplay', play, false) elements.video.addEventListener('canplay', play)
} }
const takeSnapshot = (canvasElement, videoElement) => { const takeSnapshot = (canvasElement, videoElement) => {
@ -177,7 +177,7 @@ const Scanner = ({ handleClose, setBarcode }) => {
} }
console.log("startIndex: " + startIndex ); // console.log("startIndex: " + startIndex );
// return if no starting sequence found // return if no starting sequence found
if (startIndex === 0) { if (startIndex === 0) {
return; return;
@ -186,7 +186,7 @@ const Scanner = ({ handleClose, setBarcode }) => {
// discard leading and trailing patterns // discard leading and trailing patterns
pixelBars = pixelBars.slice(startIndex, startIndex + 3 + 24 + 5 + 24 + 3); pixelBars = pixelBars.slice(startIndex, startIndex + 3 + 24 + 5 + 24 + 3);
console.log("pixelBars: " + pixelBars ); // console.log("pixelBars: " + pixelBars );
// calculate relative widths // calculate relative widths
const ref = (pixelBars[0] + pixelBars[1] + pixelBars[2]) / 3; const ref = (pixelBars[0] + pixelBars[1] + pixelBars[2]) / 3;
@ -201,8 +201,8 @@ const Scanner = ({ handleClose, setBarcode }) => {
const analyzePattern = (bars) => { const analyzePattern = (bars) => {
console.clear(); // console.clear();
console.log("analyzing"); // console.log("analyzing");
// determine parity first digit and reverse sequence if necessary // determine parity first digit and reverse sequence if necessary
const first = normalize(bars.slice(3, 3 + 4), 7); const first = normalize(bars.slice(3, 3 + 4), 7);
if (!isOdd(Math.round(first[1] + first[3]))) { if (!isOdd(Math.round(first[1] + first[3]))) {
@ -225,7 +225,7 @@ const Scanner = ({ handleClose, setBarcode }) => {
normalize(bars.slice(52, 52 + 4), 7) normalize(bars.slice(52, 52 + 4), 7)
] ]
console.log("digits: " + digits); // console.log("digits: " + digits);
// determine parity and reverse if necessary // determine parity and reverse if necessary
const parities = []; const parities = [];
@ -261,17 +261,17 @@ const Scanner = ({ handleClose, setBarcode }) => {
} }
} }
console.log("result: " + result); // console.log("result: " + result);
// check digit // check digit
const checkDigit = check[parities.join('')]; const checkDigit = check[parities.join('')];
console.log("quality: " + quality); // console.log("quality: " + quality);
// output // output
if(quality < config.quality) { if(quality < config.quality) {
const barcode = checkDigit + result.join('') const [barcode] = (checkDigit + result.join('')).match(/\d+/g)
console.log(barcode) console.log(barcode)
setBarcode(barcode)
onClose() onClose()
setBarcode(barcode)
} }
} }

View File

@ -29,7 +29,7 @@ const BarcodeScanner = () => {
<IconButton onClick={handleOpen}> <IconButton onClick={handleOpen}>
<CropFreeIcon /> <CropFreeIcon />
</IconButton> </IconButton>
{isScannerOpen ? ( {isScannerOpen && !barcode ? (
<Scanner handleClose={handleClose} setBarcode={setBarcode} /> <Scanner handleClose={handleClose} setBarcode={setBarcode} />
) : null} ) : null}
</React.Fragment> </React.Fragment>

View File

@ -49,9 +49,9 @@ export const searchProductByBarcodeAction = ({ barcode }) => ({
barcode, barcode,
}) })
export const searchProductByBarcodeSuccessAction = ({ product }) => ({ export const searchProductByBarcodeSuccessAction = ({ products }) => ({
type: SEARCH_PRODUCT_BY_BARCODE_SUCCESS, type: SEARCH_PRODUCT_BY_BARCODE_SUCCESS,
product, products,
}) })
export const searchProductByBarcodeErrorAction = ({ error }) => ({ export const searchProductByBarcodeErrorAction = ({ error }) => ({

View File

@ -125,7 +125,7 @@ const homePageReducer = produce((draft, action) => {
break; break;
case SEARCH_PRODUCT_BY_BARCODE_SUCCESS: case SEARCH_PRODUCT_BY_BARCODE_SUCCESS:
draft.product = action.product; draft.products = action.products;
draft.isLoading = false; draft.isLoading = false;
break; break;

View File

@ -99,8 +99,6 @@ export function* searchProductByBarcode() {
const requestURL = `${api.products}?barcode=${barcode}`; const requestURL = `${api.products}?barcode=${barcode}`;
console.log(`${api.products}?barcode=${barcode}`)
const requestParameters = { const requestParameters = {
method: 'GET', method: 'GET',
headers: { headers: {
@ -109,8 +107,8 @@ export function* searchProductByBarcode() {
}; };
try { try {
const { product } = yield call(request, requestURL, requestParameters); const products = yield call(request, requestURL, requestParameters);
yield put(searchProductByBarcodeSuccessAction({product})); yield put(searchProductByBarcodeSuccessAction({ products }));
} catch (error) { } catch (error) {
yield put(searchProductByBarcodeErrorAction({error: error.message})); yield put(searchProductByBarcodeErrorAction({error: error.message}));
} }

View File

@ -25,7 +25,7 @@ const makeSelectProduct = () =>
createSelector(selectHomePageDomain, (substate) => substate.product); createSelector(selectHomePageDomain, (substate) => substate.product);
const makeSelectBarcode = () => const makeSelectBarcode = () =>
createSelector(selectHomePageDomain, (substate) => substate.barocde); createSelector(selectHomePageDomain, (substate) => substate.barcode);
const makeSelectMealLabel = () => const makeSelectMealLabel = () =>
createSelector(selectHomePageDomain, (substate) => substate.form.label); createSelector(selectHomePageDomain, (substate) => substate.form.label);