
SAP Fiori space on Business Application Studio
"resources": {
"css": [
{
"uri": "css/style.css"
}
],
"js": [
{
"uri": "libs/quagga.min.js"
}
]
},
createDeviceModel: function () {
var oModel = new JSONModel(Device);
oModel.setDefaultBindingMode("OneWay");
// Disable the scan barcode button by default
oModel.setProperty("/barcodeScanEnabled",false);
if(navigator && navigator.mediaDevices && navigator.mediaDevices.getUserMedia){
navigator.mediaDevices.getUserMedia({video:true}).then(function(stream){
// device supports video, which means will enable the scan button
oModel.setProperty("/barcodeScanEnabled",true);
}).catch(function(err){
// not supported, barcodeScanEnabled already default to false
});
}
return oModel;
}
<Label text="Barcode value" />
<Input id="scannedValue" placeholder="Use scan button to enter barcode" />
<Button icon="sap-icon://bar-code" text="Scan" tooltip="Scan barcode" press="onScanForValue">
</Button>
onScanForValue: function (oEvent) {
if (!this._oScanDialog) {
this._oScanDialog = new sap.m.Dialog({
title: "Scan barcode",
contentWidth: "640px",
contentHeight: "480px",
horizontalScrolling: false,
verticalScrolling: false,
stretchOnPhone: true,
content: [new sap.ui.core.HTML({
id: this.createId("scanContainer"),
content: "<div />"
})],
endButton: new sap.m.Button({
text: "Cancel",
press: function (oEvent) {
this._oScanDialog.close();
}.bind(this)
}),
afterOpen: function () {
this._initQuagga(this.getView().byId("scanContainer").getDomRef()).done(function () {
// Initialisation done, start Quagga
Quagga.start();
}).fail(function (oError) {
// Failed to initialise, show message and close dialog...this should not happen as we have
// already checked for camera device ni /model/models.js and hidden the scan button if none detected
MessageBox.error(oError.message.length ? oError.message : ("Failed to initialise Quagga with reason code " + oError.name), {
onClose: function () {
this._oScanDialog.close();
}.bind(this)
});
}.bind(this));
}.bind(this),
afterClose: function () {
// Dialog closed, stop Quagga
Quagga.stop();
}
});
this.getView().addDependent(this._oScanDialog);
}
this._oScanDialog.open();
},
_initQuagga: function (oTarget) {
var oDeferred = jQuery.Deferred();
// Initialise Quagga plugin - see https://serratus.github.io/quaggaJS/#configobject for details
Quagga.init({
inputStream: {
type: "LiveStream",
target: oTarget,
constraints: {
width: { min: 640 },
height: { min: 480 },
facingMode: "environment"
}
},
locator: {
patchSize: "medium",
halfSample: true
},
numOfWorkers: 2,
frequency: 10,
decoder: {
readers: [{
format: "code_128_reader",
config: {}
}]
},
locate: true
}, function (error) {
if (error) {
oDeferred.reject(error);
} else {
oDeferred.resolve();
}
});
if (!this._oQuaggaEventHandlersAttached) {
// Attach event handlers...
Quagga.onProcessed(function (result) {
var drawingCtx = Quagga.canvas.ctx.overlay,
drawingCanvas = Quagga.canvas.dom.overlay;
if (result) {
// The following will attempt to draw boxes around detected barcodes
if (result.boxes) {
drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
result.boxes.filter(function (box) {
return box !== result.box;
}).forEach(function (box) {
Quagga.ImageDebug.drawPath(box, { x: 0, y: 1 }, drawingCtx, { color: "green", lineWidth: 2 });
});
}
if (result.box) {
Quagga.ImageDebug.drawPath(result.box, { x: 0, y: 1 }, drawingCtx, { color: "#00F", lineWidth: 2 });
}
if (result.codeResult && result.codeResult.code) {
Quagga.ImageDebug.drawPath(result.line, { x: 'x', y: 'y' }, drawingCtx, { color: 'red', lineWidth: 3 });
}
}
}.bind(this));
Quagga.onDetected(function (result) {
// Barcode has been detected, value will be in result.codeResult.code. If requierd, validations can be done
// on result.codeResult.code to ensure the correct format/type of barcode value has been picked up
// Set barcode value in input field
this.getView().byId("scannedValue").setValue(result.codeResult.code);
// Close dialog
this._oScanDialog.close();
}.bind(this));
// Set flag so that event handlers are only attached once...
this._oQuaggaEventHandlersAttached = true;
}
return oDeferred.promise();
}
npm run start
Sample Code128-Barcode
cf login
This will take a moment and will create the MTA archive that is necessary for deployment on BTP.
As a final step right-click the mtar file within the newly created mta_archives folder and select Deploy MTA Archive.
Screenshot of the app in SAP Mobile Start
You might have realized, that the popup for camera access comes up only the first time in the browser. After that your setting is persisted and your browser won’t ask for this again. When accessing your app via SAP Mobile Start, you will be asked for camera access every time by default. If necessary, it’s possible to persist the setting in the Safari View Controller. Therefore, you need to click the aA Icon in the top of the Shell and navigate to Website Settings. Next, for Camera select Allow.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
11 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 |