探索插件用法的最好方法是查看我们的演示应用程序。您可以从下载我们的演示应用程序在这里
下载后,导航到demo应用的根文件夹并运行demo应用:
cd cmbSDK_Nativescript/demo tns运行android #或ios
简而言之,在你的项目中使用我们的插件的步骤如下:
从'cmbsdk-nativescript'导入{CMBReader, CMBReaderConstants};
开放home-view-model.ts从演示应用程序检查此代码。我们的演示应用程序中的所有代码都有简短的描述。
this.cmbReader.startScanning ()
重要的
将cmbSDK nativescript插件与MX设备一起使用是免费的,但如果您想使用摄像头设备(使用智能手机摄像头进行扫描),则需要从获得许可证CMBDN.
Reader仍然可以在没有许可证的情况下工作,但是结果被随机地用*字符掩盖了。
免费注册,您可以获得30天试用许可证密钥。
获得密钥后,有两种方法可以在应用程序中使用。
this.cmbReader.registerSDK(“SKD_KEY”);
/* @return解析承诺
要启动并运行扫描仪,首先要做的是调用loadScanner ()方法。它需要一个设备类型作为输入参数。此方法未连接到读卡器设备。我们需要打电话connect ()在实际连接到阅读器设备的承诺中
this. cmbread . loadscanner (CMBReaderConstants.DEVICE_TYPE.MXReader) .then(result => {this. cmbread .connect() .then(result => {this.updateUIByConnectionState(CMBReaderConstants.CONNECTION_STATE.Connected);}) .catch(err => {console.log(err);this.updateUIByConnectionState (CMBReaderConstants.CONNECTION_STATE.Disconnected);});});});
/* @return resolve promise if connection succeeded reject promise with error message if there is some problem while connection */
connect()方法的结果作为承诺返回,它将返回连接尝试的结果:
this.cmbReader.connect() .then(result => {this.updateUIByConnectionState(CMBReaderConstants.CONNECTION_STATE.Connected);}) .catch(err => {console.log(err);this.updateUIByConnectionState (CMBReaderConstants.CONNECTION_STATE.Disconnected);});
事件监听器用于ReaderDevice的连接状态,即CMBReaderConstants.EVENTS.ConnectionStateChanged事件将在下面进行更详细的解释。
/* @return resolve promise if disconnect succeeded; / /如果断开连接时出现问题,拒绝承诺并返回错误消息*/
正如有connect (),有一个断开连接()相反的方法connect ():
this.cmbReader.disconnect ();
类似于connect (),断开连接()也引发了CMBReaderConstants.EVENTS.ConnectionStateChanged事件
/*使用CMBReaderConstants解析承诺。当我们检查当前连接状态时,如果有问题,则拒绝承诺并返回错误消息*/
/* @return resolve promise if scanning is started/stopped reject promise with error message if there is some problem while start/stop scanning process ex: if readerDevice not initialized) */
startScanning()和stopScanning()方法触发CMBReaderConstants.EVENTS.ScanningStateChanged事件,该事件在扫描程序启动时返回true,在扫描程序停止时返回false。
启动扫描仪并扫描条形码后,扫描结果触发CMBReaderConstants.EVENTS.ReadResultReceived事件
如果需要获取当前连接状态,getConnectionState()可以使用
this.cmbReader.getConnectionState() .then(connectionState => {if (connectionState == CMBReaderConstants.CONNECTION_STATE.Connected) {// reader is connected}}) .catch(err => {console.log(err);});
/* @return resolve promise with result true if symbolology is enabled or false if symbol is disabled reject promise with error message if there is some problem while we try to enable the symbol */ . /如果我们尝试启用symbol,如果有一些问题,拒绝承诺和错误消息*/
一旦与Reader建立了连接,我们就可以通过调用setSymbologyEnabled ().它期望启用符号的int值和ON/OFF的布尔值。
this.cmbReader.setSymbologyEnabled (CMBReaderConstants.SYMBOLOGY。.then(result => {if (result == true) console.log("启用了DataMatrix ");else console.log("DataMatrix NOT enabled");}) .catch(err => {console.log(err);});
/* @return解析promise,如果启用symbol,结果为true;如果禁用symbol,结果为false;如果检查symbol状态时出现问题,则拒绝promise,并提供错误消息
要检查是否启用了符号,我们使用isSymbologyEnabled ().
this. cmbread . issymbologyenabled (cmbreaderconstants . symbologydatamatrix) .then(result => {if (result == true) console.log("启用的DataMatrix ");else console.log("DataMatrix NOT enabled");}) .catch(err => {console.log(err);});
/* @return return promise with true if light is enabled or false if light is disabled reject promise with error message if there is some problem while we check light status */
如果我们想在默认情况下启用flash,我们可以使用setLightsOn ()并检查它是否被启用isLightsOn ()
要设置将摄像头设备用作条形码阅读器时摄像头的行为,请使用:
this.cmbReader.setCameraMode (CMBReaderConstants.CAMERA_MODE.NoAimer);/**使用无瞄准器的相机。预览打开,照明可用。CMBReader.CAMERA_MODE。NoAimer = 0,使用基本瞄准器(例如,StingRay)。预览关闭,照明不可用。CMBReader.CAMERA_MODE。被动瞄准器= 1,使用相机与主动瞄准器(如MX-100)。预览关闭,照明可用。CMBReader.CAMERA_MODE。ActiveAimer = 2, Use mobile device front camera. Preview is on, illumination is not available. CMBReader.CAMERA_MODE.FrontCamera = 3 */
注意:在我们调用之前应该调用它loadScanner ()让它生效如果扫描程序已经加载,在扫描程序加载之后调用它不会有任何作用。
设置被覆盖的预览选项。
这个函数期望一个整数,它是我们想要启用的所有预览选项的或结果。
不返回值。
应在loadScanner ()(cameraMode一样())\
例子:
this.cmbReader.setPreviewOptions (CMBReaderConstants.CAMERA_PREVIEW_OPTION。NoZoomBtn | CMBReaderConstants.CAMERA_PREVIEW_OPTION.NoIllumBtn);
设置相机覆盖模式。您需要在调用loadScanner之前执行此操作,否则它将无法正常工作,只能在MX Mobile上使用
例子:
this.cmbReader.setPreviewOverlayMode (CMBReaderConstants.OVERLAY_MODE.OM_CMB);
/*@params x,y,w,hx,y:左上角位置w,h:整个容器前面矩形的宽度和高度*/
仅用于手机摄像头,设置摄像头预览屏幕的大小和位置。
例子:
this.cmbReader.setPreviewContainerPositionAndSize(0、0100、50);//将设置预览为0,0和100%宽50%高
仅用于手机摄像头,设置摄像头预览开始在全屏而不是部分视图。
例子:
this.cmbReader.setPreviewContainerFullScreen ();//当startscan被调用时,将设置相机预览以全屏开始
/* @return promise with result true if image or imagegraphics is enabled, reject promise with error message if there is some problem while enabling image result */
启用/禁用作为映像使用返回的结果类型
this.cmbReader.enableImage(真正的);
同样适用于enableImageGrapics ().
此.cmbReader.enableImageGraphics(错误);
/* @return resolve promise with the charge in percentage reject promise with error message if there is some problem while checking battery level */
显示连接设备的电池电量的辅助方法。这样使用:
this. cmbread . getdevicebatterylevel () .then(result => {console.log(result);}) .catch(err => {console.log(err);});
/* @return resolve promise if reset is successful
重置我们可以使用的配置选项重置配置
this. cmbread . resetconfig () .then(result => {}) .catch(err => {console.log(err);});
设置扫描结果分析器。在加载并连接readerDevice时必须调用此API
例子:
this.cmbReader.setParser (CMBReaderConstants.PARSERS.AUTO);
/*@return resolve promise with result取决于commandString拒绝承诺,如果在执行命令时出现问题,则返回错误消息*/
最后,所有的方法都可以用向读卡器设备发送DMCC字符串来代替。为此,我们可以使用我们的API方法sendCommand. 它可用于使用命令字符串完全控制读卡器。\n可以找到有关命令字符串的详细信息在这里或在这里
这样使用:
this.cmbReader。sendCommand("SET SYMBOL.POSTNET OFF") .then(result => { console.log(result); }) .catch(err => { console.log(err); });
Nativescript的cmbReader对象扩展了Observable对象,并且有可以被监听和在应用程序中使用的事件。这些事件应该在loadScanner被调用之前设置。
this.cmbReader.on (CMBReaderConstants.EVENTS。ConnectionStateChanged, (args: any) => {});
以下是活动列表:
每当收到扫描结果时,就会触发此事件。结果是具有以下结构的JSON对象:
/** * results - json数组。模式在这里你会发现如果你用多码主要结果(所有部分结果一起合并在一个readString)和所有其他部分结果* subResults - json数组的所有部分结果(如果单代码模式uset这个数组将空的)* xml字符串表示的完整结果从读者设备以xml格式*和结果subResults是json数组,包含以下结构:*条形码符号的象征——整数表示检测到* symbologyString -条码符号的字符串表示检测到* readString -字符串表示的条形码* goodRead bool表明如果条形码成功扫描* xml格式的xml字符串表示的部分结果* imageGraphics -字符串代表svg图像上检测到帧*图像- base64字符串包含图像上检测到帧* parsedText -字符串表示结果* parsedJSON——解析文本字符串表示解析json格式的文本从结果* isGS1 - bool表明如果条形码是否GS1 * /
当ReaderDevice的可用性发生变化时(例如:当MX移动终端连接或断开电缆,或打开或关闭电缆时)将触发此事件。结果是一个包含可用性信息的数字。
ReaderDevice的连接状态更改时触发此事件。结果是一个包含连接信息的数字。
当ReaderDevice的扫描器状态改变时,会触发此事件。结果是一个布尔值,如果开始扫描则为true,如果停止扫描则为false。