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
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)
#### FITWAVE - frontend

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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