中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
使用Scan Kit(统一扫码服务),通过接数组生成码图
微信扫码分享
// 通过接数组生成码图 Promise方式 Button('生成乘车码').genButton() .onClick(() => { this.pixelMap = undefined; let content = this.mContext; let pattern = /^[0-9a-fA-F]+$/; if (content && !pattern.test(content)) { promptAction.showToast({ message: '乘车码是通过字节数组生成,请输入十六进字符串表示的字节数组', duration: 2000 }); return; } if (content.length % 2 !== 0) { content = '0' + content; } let contentBuffer: ArrayBuffer = buffer.from(content, 'hex').buffer; let options: generateBarcode.CreateOptions = { scanType: this.mScanType, height: Number(this.mHeight), width: Number(this.mWidth), margin: Number(this.mMargin), level: this.mLevel, backgroundColor: this.mBackgroundColor, pixelMapColor: this.mPixelMapColor, } try { generateBarcode.createBarcode(contentBuffer, options).then((result: image.PixelMap) => { this.pixelMap = result; hilog.info(0x0001, TAG, 'Succeeded in creating barCode.'); }).catch((error: BusinessError) => { showError(error); hilog.error(0x0001, TAG, `catch Failed to create barCode. Code: ${error.code}, message: ${error.message}`); }) } catch (error) { showError(error); hilog.error(0x0001, TAG, `000 Failed to create barCode. Code: ${error.code}, message: ${error.message}`); } })