最好的方法来探索插件的用法是检查我们的演示应用程序在这里。
下载后,导航到demo应用的根文件夹并运行demo应用:
cd cmbSDK_Nativescript/demo tns运行android #或ios
简而言之,在你的项目中使用我们的插件的步骤如下:
从'cmbsdk-nativescript'导入{CMBReader, CMBReaderConstants};
开放home-view-model.ts从演示应用程序检查此代码。我们的演示应用程序中的所有代码都有简短的描述。
this.cmbReader.startScanning ()
重要的
使用cmbSDK原生脚本插件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()方法的结果作为Promise返回,它将返回连接尝试的结果:
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 ()
当我们使用camera设备作为条形码阅读器时,设置摄像头的行为方式:
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,h x,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 ().
this.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
重置我们可以使用的配置选项resetConfig
this. cmbread . resetconfig () .then(result => {}) .catch(err => {console.log(err);});
设置cmbScanner结果解析器。这个API必须在readerDevice被加载和连接时被调用
例子:
this.cmbReader.setParser (CMBReaderConstants.PARSERS.AUTO);
/* @return resolve promise with result depends of commandString reject promise with error message if there is some problem while execute command */
最后,可以将所有方法替换为向READER设备发送DMCC字符串。为此,我们可以使用API方法sendCommand.它可以用命令字符串来完全控制Reader。可以找到更多关于命令字符串的信息在这里或在这里
这样使用:
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。