cancel
Showing results for 
Search instead for 
Did you mean: 

sap.ndc.BarcodeScanner can not fetch camera of pad

Troy_Li
Explorer
0 Kudos

Hello All,

I am trying to implement bar code scanner using standard sap library "sap.ndc.BarcodeScanner".

But i am not able to fetch camera and scan it. Instead only Input Field is displaying.

Can anyone Please help me regarding this?

kedarT
Active Contributor
0 Kudos

Hi Edward,

Please provide the code which is not working.

Troy_Li
Explorer
0 Kudos

Hi Kedar,

Thanks for you response!

My code:

onscan.txt

the button of scan in view.xml and the function onScan in controller.js

<Button type="Accept" text="{i18n>scan}" press="onScan" icon="sap-icon://bar-code" id="scanBtn" enabled="true" />

onScan: function(){
				flag = false;
				var that = this;
				var oData = this.getView().getModel('data');
				var oI18n = this.getView().getModel("i18n").getResourceBundle();
				//if (sap.ui.Device.os.android) {
					jQuery.sap.require("sap.ndc.BarcodeScanner");
					sap.ndc.BarcodeScanner.scan(
					   function (sResult) {
						   if (sResult.cancelled == false) {
							   var result = sResult.text.split(',');
							   if (result.length != 6) {
								   that.onScanError();
								   that.fShowMessageBox('error', oI18n.getText("codeError") + sResult.text);
								   return;
							   }
							   oData.setProperty("/materialNumber", result[0]);
							   oData.setProperty("/quantity", result[2]);
							   oData.setProperty("/batch", result[5]);
							   //set filter array
							   var aFilter = [];
							   //set material number filter
							   aFilter[0] = new sap.ui.model.Filter({
								   path: "materialNumber",
								   operator: sap.ui.model.FilterOperator.EQ,
								   value1: oData.getProperty("/materialNumber"),
								   value2: ""
							   });
							   //set login language
							   aFilter[1] = new sap.ui.model.Filter({
								   path: "language",
								   operator: sap.ui.model.FilterOperator.EQ,
								   value1: oData.getProperty("/language"),
								   value2: ""
							   });
							   //set plant
							   aFilter[2] = new sap.ui.model.Filter({
								   path: "plant",
								   operator: sap.ui.model.FilterOperator.EQ,
								   value1: defaultData.plant,
								   value2: ""
							   });
							   //get material description and unit
								oModel.read("/EntitySets/", {
									filters: aFilter,
									success: function(oERP, oResponse) {
										//check result: success
										if (oERP.results[0].msgty == 'S') {
											//set model
											oData.setProperty("/materialDesc", oERP.results[0].materialDesc);
											oData.setProperty("/unit", oERP.results[0].unit);
											flag = true;
											that.getView().byId('quantityid').focus();
											if (oERP.results[0].batchFlag == 'X') {
												that.getView().byId('batchid').setEnabled(true);
											} else if (oERP.results[0].batchFlag == '') {
												that.getView().byId('batchid').setEnabled(false);
												that.getView().byId('batchid').setValue("");
												that.getView().byId('batchlabelid').setRequired(false);
											}
										//check result: error
										} else {
											//error message output
											that.onScanError();
											that.fShowMessageBox('error', oERP.results[0].msg);
										}
									},
									error: function(oError) {
										//error message output
										that.onScanError();
										that.fShowMessageBox('error', oError.message);
									}
								});
						   }else{
							   that.onScanError();
							   that.fShowMessageBox('error', oI18n.getText("scanCancel"));
						   }
					   },
					   function (Error) {
						   that.onScanError();
						   that.fShowMessageBox('error', Error);
						   return;
					   },
					   function (mParams) {
					      alert("Value entered: " + mParams.newValue);
					   }
					);
				}else{
					that.onScanError();
					that.fShowMessageBox('error', oI18n.getText("scanError"));
				}
			}

Accepted Solutions (0)

Answers (2)

Answers (2)

andy_tan1
Participant
0 Kudos

Hi,

The sap.ndc.BarcodeScanner is declared under the namespace sap.ndc. Here the "ndc" stands for Native Device Capabilities. Those APIs will use the cordova plugins to invoke the native devices functions, for example the Camera. The sap.ndc.BarcodeScanner will check the availability of cordova plugins. If the app is opened via browser, then BarcodeScanner will simply open a dialog and ask end user to manually enter the barcode. If the app is opened via a cordova application or SAP Fiori Client (which is built on top of cordova), then it will invoke device camera.

mosilva
Explorer
0 Kudos

Hi,

Were you able to solve this problem? I have the same issue.

Thanks, Marcos.