decode barcode

This commit is contained in:
ppanek 2020-01-18 10:17:25 +01:00
parent bb33c13a29
commit 728ea66ed1
1 changed files with 147 additions and 0 deletions

147
app/controllers/scanner.js Normal file
View File

@ -0,0 +1,147 @@
import Controller from '@ember/controller';
import { action, set } from '@ember/object';
import cv from 'cv';
import Quagga from 'quagga';
import { task, timeout, waitForEvent } from 'ember-concurrency';
import { defer } from 'rsvp';
import { storageFor } from 'ember-local-storage';
import { inject as service } from '@ember/service';
import PersonalDataValidation from '../validations/personal-data';
import lookupValidator from 'ember-changeset-validations';
import Changeset from 'ember-changeset';
import { A } from '@ember/array';
import CvWrapper from '../modules/cv-wrapper';
export default class Scanner extends Controller {
@storageFor('products') products;
@service modal;
cvWrapper = new CvWrapper();
init() {
this.personalData = {
sex: 'male'
};
this.currentCalculation = A();
super.init(...arguments);
}
@action
snap(dataUri) {
set(this, 'dataUri', dataUri);
}
@action
err(e) {
console.log("ee", e);
}
@action
registerCamera(cameraRef) {
this.halt = false;
this.code = null;
this.cameraRef = cameraRef;
this.scan.perform();
}
@action
stop() {
this.halt = true;
}
@action
addProduct(product) {
product.code = this.code;
this.products.pushObject(product);
this.currentCalculation.pushObject(product);
this.modal.close('addNew');
}
@action
clickCalculate() {
let dataset = this.calculate(this.currentCalculation);
let changeset = new Changeset(this.personalData, lookupValidator(PersonalDataValidation), PersonalDataValidation);
changeset.validate().then(() => {
if (changeset.get('isValid') && this.currentCalculation.length > 0) {
this.transitionToRoute('summary', { queryParams: {
...dataset,
sex: this.personalData.sex,
height: changeset.get('height'),
age: changeset.get('age'),
weight: changeset.get('weight')
}})
}
});
}
@action
onDelete(product) {
this.currentCalculation.removeObject(product);
}
calculate(dataset) {
return dataset.reduce((acc, curr) => {
acc.p += (curr.quantity * curr.p) / 100;
acc.c += (curr.quantity * curr.c) / 100;
acc.f += (curr.quantity * curr.f) / 100;
acc.kcal += (curr.quantity * curr.kcal) / 100;
return acc
}, { p: 0, c: 0, f: 0, kcal: 0 });
}
processFoundCode(code) {
let product = this.products.find(p => p.code === code)
if (product) {
this.currentCalculation.pushObject(product);
} else if (code) {
this.modal.open('addNew');
}
}
@(task(function*() {
let img = document.getElementById('imageToProcess');
while(!this.halt && !this.code) {
this.cameraRef.snap();
yield waitForEvent(img, 'load')
this.cvWrapper.findBarcode(img);
let img = document.getElementById('canvasOutput');
let result = yield this.decodeBarcodeFromImg(img);
if (result && result.codeResult && result.codeResult.code) {
this.code = result.codeResult.code;
}
}
this.processFoundCode(this.code);
}))
scan;
decodeBarcodeFromImg(img) {
let config = {
inputStream: {
size: 800,
singleChannel: false
},
locator:
{
patchSize: "medium",
halfSample: true
},
decoder: {
readers: [{
format: 'ean_reader',
config: {}
}]
},
locate: true,
src: img.toDataURL()
};
let deferred = defer();
Quagga.decodeSingle(config, (r) => {
deferred.resolve(r);
});
return deferred.promise;
}
}