123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869 |
- /**
- * @export
- * @param {string} name 微信api的名称 ,如 uniAsyncPromise("getSystemInfo",options)
- * @param {object} options 除了success 和 fail 的其他参数
- * @returns
- */
- export function uniAsyncPromise(name, options) {
- return new Promise((resolve, reject) => {
- uni[name]({
- ...(options || {}),
- success: (res) => {
- resolve(res);
- // console.log('成功')
- num = 0
- },
- fail: (err) => {
- reject(err, '查询失败原因');
- }
- });
- });
- }
- //微信小程序向蓝牙打印机发送数据进行打印的坑:
- //小程序api向蓝牙打印机发送数据打印,发送的任何内容都应该要转成二进制数据,而且蓝牙打印的文本编码是GBK的,发送中文需转成GBK编码再转成二进制数据发送
- //发送打印机指令也要转成二进制数据发送
- //蓝牙打印机一次接收的二级制数据有限制,不同的系统不同的蓝牙设备限制可能不同,微信建议一次20个字节,需做递归分包发送
- //发送完要打印的内容后,一定要发送一个打印的指令才能顺利打印 (有些指令就不需要)
- //一、初始化蓝牙、开始检索蓝牙设备
- // { allowDuplicatesKey: true, interval: 500}
- export function openBlue() {
- return uniAsyncPromise('openBluetoothAdapter')
- }
- export function startBluetoothDevicesDiscovery(option) {
- // console.log('开始蓝牙扫描');
- uniAsyncPromise('startBluetoothDevicesDiscovery', option).then((res) => {
- // console.log('正在搜寻蓝牙设备', res);
- });
- }
- export function getConnectedBluetoothDevices(option) {
- // console.log('开始获取已连接设备');
- return uniAsyncPromise('getConnectedBluetoothDevices', option)
- }
- //二、
- /**
- *
- *
- * @export
- * @param {function} getDevices uni.getBluetoothDevices的监听回调函数
- */
- export function onfindBlueDevices(getDevices) {
- //监听寻找到新设备的事件
- uni.onBluetoothDeviceFound((devices) => {
- //获取在蓝牙模块生效期间所有已发现的蓝牙设备
- uniAsyncPromise('getBluetoothDevices').then((res) => {
- getDevices && getDevices(res.devices);
- });
- });
- }
- /**
- * @export
- * @param {function} stopBlueDevicesDiscovery 关闭蓝牙扫描
- */
- export function stopBlueDevicesDiscovery() {
- //监听寻找到新设备的事件
- console.log('停止蓝牙扫描');
- return uniAsyncPromise('stopBluetoothDevicesDiscovery').then((res) => {
- console.log('停止搜寻蓝牙设备', res);
- });
- }
- //三、连接蓝牙设备
- /**
- * @export
- * @param {function} createBLEConnection
- * @param {number} deviceId 蓝牙设备id
- */
- export function createBLEConnection(deviceId, sucess, fail) {
- //连接蓝牙设备
- console.log('连接蓝牙设备', deviceId);
- uniAsyncPromise("createBLEConnection", {
- deviceId
- })
- .then(res => {
- //连接成功可选择停止搜索蓝牙
- //stopBlueDevicesDiscovery();
- console.log('连接成功');
- sucess && sucess({
- res: res,
- });
- })
- .catch(res => {
- console.log('连接设备异常' + res);
- fail && fail({
- res: res,
- });
- })
- /*.finally(res=>{
- console.log('连接成功');
- sucess && sucess({
- res: res,
- });
- });*/
- }
- export function closeBLEConnection(deviceId) {
- console.log('断开蓝牙设备', deviceId);
- uniAsyncPromise("closeBLEConnection", {
- deviceId
- })
- .then(res => {
- console.log('BLEDisconnect complete', res);
- })
- .catch(res => {
- console.log('断开设备异常' + res);
- })
- /*.finally(res=>{
- console.log('BLEDisconnect complete', res);
- }); */
- }
- //四、连接成功后, 获取蓝牙设备的service服务
- // uniAsyncPromise("getBLEDeviceServices",{deviceId:""}).then(res=>{})
- export function getBLEDeviceServices(deviceId, success, fail) {
- console.log('获取ServiceId', deviceId);
- //加延迟避免取不到service
- setTimeout(() => {
- uniAsyncPromise("getBLEDeviceServices", {
- deviceId: deviceId
- })
- .then(res => {
- console.log('服务', res);
- success && success({
- serviceId: res.services,
- });
- })
- .catch((res) => {
- //getBLEDeviceServices(deviceId, success, fail);
- console.log('获取ServiceId异常' + res);
- fail && fail({
- res: res,
- });
- });
- }, 1000)
- }
- //五、获取的service服务可能有多个,递归获取特征值(最后要用的是能读,能写,能监听的那个值的uuid作为特征值id)
- /**
- *
- *
- * @export
- * @param {number} deviceId 蓝牙设备id
- * @param {array} services uniAsyncPromise("getBLEDeviceServices",{deviceId:""}).then(res=>{})获取的res.services
- * @param {function} success 成功取得有用特征值uuid的回调函数
- */
- export function getDeviceCharacteristics(deviceId, services, success, fail) {
- //services = services.slice(0);
- console.log('获取Characteristics', deviceId, services);
- if (services.length) {
- const serviceId = services.shift().uuid;
- console.log('ServceID ', serviceId);
- uniAsyncPromise('getBLEDeviceCharacteristics', {
- deviceId,
- serviceId,
- })
- .then((res) => {
- console.log('getBLEDeviceCharacteristics', deviceId, serviceId, res);
- let finished = false;
- let write = false;
- let notify = false;
- let indicate = false;
- var readId;
- var writeId;
- //有斑马品牌的一款打印机中res.characteristics的所有uuid都是相同的,找所有的properties存在(notify || indicate) && write这种情况就说明这个uuid是可用的(不确保所有的打印机都能用这种方式取得uuid,在主要测试得凯盛诺打印机res.characteristic只有一个uuid,所以也能用这个方式)
- for (var i = 0; i < res.characteristics.length; i++) {
- if (!notify) {
- notify = res.characteristics[i].properties.notify;
- if (notify) readId = res.characteristics[i].uuid;
- }
- if (!indicate) {
- indicate = res.characteristics[i].properties.indicate;
- if (indicate) readId = res.characteristics[i].uuid;
- }
- if (!write) {
- write = res.characteristics[i].properties.write;
- writeId = res.characteristics[i].uuid;
- }
- if ((notify || indicate) && write) {
- /* 获取蓝牙特征值uuid */
- success &&
- success({
- serviceId,
- writeId: writeId,
- readId: readId,
- });
- finished = true;
- break;
- }
- }
- if (!finished) {
- getDeviceCharacteristics(deviceId, services, success, fail);
- }
- })
- .catch((res) => {
- getDeviceCharacteristics(deviceId, services, success, fail);
- });
- } else {
- fail && fail();
- }
- }
- //六、启动notify 蓝牙监听功能 然后使用 uni.onBLECharacteristicValueChange用来监听蓝牙设备传递数据
- /**
- * @export
- * @param {object} options
- * {
- deviceId,//蓝牙设备id
- serviceId,//服务id
- characteristicId,//可用特征值uuid
- }
- * @param {function} onChange 监听蓝牙设备传递数据回调函数
- */
- export function onGetBLECharacteristicValueChange(options, onChange = function() {}) {
- console.log('deviceId ', options.deviceId);
- console.log('serviceId ', options.serviceId);
- console.log('characteristicId ', options.characteristicId);
- uniAsyncPromise('notifyBLECharacteristicValueChange', {
- state: true,
- ...options,
- }).then((res) => {
- console.log('onBLECharacteristicValueChange ');
- uni.onBLECharacteristicValueChange(onChange);
- });
- }
- //七、发送数据(递归分包发送)
- /**
- * @export
- * @param {object} options
- * {
- deviceId,
- serviceId,
- characteristicId,
- value [ArrayBuffer],
- lasterSuccess,
- onceLength
- }
- */
- var num = 0
- var number = 0
- var totalNum = 0
- var totalValue
- export function sendDataToDevice(options) {
- if (number == 0) {
- uni.showLoading({
- title: '打印中,请稍等',
- mask: true,
- });
- number++
- totalNum = options.value.byteLength;
- }
- // console.log(options, '查询学习学习')
- let byteLength = options.value.byteLength;
- let totalValue = options.value
- // console.log('當前', byteLength, totalNum, '初始数值')
- //这里默认一次20个字节发送
- const speed = options.onceLength; //20;
- if (byteLength > 0) {
- setTimeout(() => {
- uniAsyncPromise('writeBLECharacteristicValue', {
- ...options,
- value: options.value.slice(0, byteLength > speed ? speed : byteLength),
- })
- .then((res) => {
- // console.log('打印继续=================', res, speed, byteLength);
- if (byteLength > speed) {
- num = 0
- sendDataToDevice({
- ...options,
- value: options.value.slice(speed, byteLength),
- });
- } else {
- num = 0
- uni.showToast({
- title: '打印完成',
- icon: 'none',
- duration: 2000
- })
- number = 0
- }
- })
- .catch((res) => {
- if (num != 15) {
- var data = {
- ...options,
- }
- sendDataToDevice({
- ...data,
- value: totalValue,
- // value: options.value.slice(speed,parseInt(byteLength)+parseInt(speed)*5),
- });
- console.log(res, '失败重新打印原因', options.value.slice(speed, byteLength), speed,
- byteLength);
- num++
- } else {
- num = 0
- uni.showToast({
- title: '打印失败请检查蓝牙连接状态或打印机状态',
- icon: 'none',
- duration: 2000
- })
- number = 0
- }
- });
- }, 100)
- }
- }
- export function charToArrayBuffer(str) {
- var out = new ArrayBuffer(str.length);
- var uint8 = new Uint8Array(out);
- var strs = str.split('');
- for (var i = 0; i < strs.length; i++) {
- uint8[i] = strs[i].charCodeAt();
- }
- return uint8;
- }
- export function charToArray(str) {
- var arr = [];
- var strs = str.split('');
- for (var i = 0; i < strs.length; i++) {
- arr[i] = strs[i].charCodeAt();
- }
- return arr;
- }
- //打印二维码
- /**
- * @export
- * @param {object} options
- * {
- deviceId,
- serviceId,
- characteristicId,
- value,//ArrayBuffer:二维码的数据
- }
- */
- export function printQR(options) {
- //打印二维码的十进制指令data:
- let data = [29, 107, 97, 7, 4, options.value.byteLength, 0];
- sendDataToDevice({
- ...options,
- value: new Uint8Array(data).buffer,
- lasterSuccess: () => {
- //指令发送成功后,发送二维码的数据
- sendDataToDevice(options);
- },
- });
- }
- function grayPixle(pix) {
- return pix[0] * 0.299 + pix[1] * 0.587 + pix[2] * 0.114;
- }
- export function overwriteImageData(data) {
- let sendWidth = data.width,
- sendHeight = data.height;
- const threshold = data.threshold || 180;
- let sendImageData = new ArrayBuffer((sendWidth * sendHeight) / 8);
- sendImageData = new Uint8Array(sendImageData);
- let pix = data.imageData;
- const part = [];
- let index = 0;
- for (let i = 0; i < pix.length; i += 32) {
- //横向每8个像素点组成一个字节(8位二进制数)。
- for (let k = 0; k < 8; k++) {
- const grayPixle1 = grayPixle(pix.slice(i + k * 4, i + k * 4 + (4 - 1)));
- //阈值调整
- if (grayPixle1 > threshold) {
- //灰度值大于threshold位 白色 为第k位0不打印
- part[k] = 0;
- } else {
- part[k] = 1;
- }
- }
- let temp = 0;
- for (let a = 0; a < part.length; a++) {
- temp += part[a] * Math.pow(2, part.length - 1 - a);
- }
- //开始不明白以下算法什么意思,了解了字节才知道,一个字节是8位的二进制数,part这个数组存的0和1就是二进制的0和1,传输到打印的位图数据的一个字节是0-255之间的十进制数,以下是用权相加法转十进制数,理解了这个就用上面的for循环替代了
- // const temp =
- // part[0] * 128 +
- // part[1] * 64 +
- // part[2] * 32 +
- // part[3] * 16 +
- // part[4] * 8 +
- // part[5] * 4 +
- // part[6] * 2 +
- // part[7] * 1;
- sendImageData[index++] = temp;
- }
- return {
- array: Array.from(sendImageData),
- width: sendWidth / 8,
- height: sendHeight,
- };
- }
- /**
- * printImage
- * @param {object} opt
- * {
- deviceId,//蓝牙设备id
- serviceId,//服务id
- characteristicId,//可用特征值uuid
- lasterSuccess , //最后完成的回调
- }
- */
- export function printImage(opt = {}, imageInfo = {}) {
- let arr = imageInfo.array,
- width = imageInfo.width;
- const writeArray = [];
- const xl = width % 256;
- const xh = width / 256;
- //分行发送图片数据,用的十进制指令
- const command = [29, 118, 48, 0, xl, xh, 1, 0]; //1D 76 30 00 w h
- const enter = [13, 10];
- for (let i = 0; i < arr.length / width; i++) {
- const subArr = arr.slice(i * width, i * width + width);
- const tempArr = command.concat(subArr);
- writeArray.push(new Uint8Array(tempArr));
- }
- writeArray.push(new Uint8Array(enter));
- //console.log(writeArray);
- const print = (options, writeArray) => {
- if (writeArray.length) {
- // console.log("send");
- sendDataToDevice({
- ...options,
- value: writeArray.shift().buffer,
- lasterSuccess: () => {
- if (writeArray.length) {
- print(options, writeArray);
- } else {
- options.lasterSuccess && options.lasterSuccess();
- }
- },
- });
- }
- };
- // console.log("start print");
- print(opt, writeArray);
- }
- /* 16hex insert 0 */
- function Hex2Str(num) {
- if (num.toString(16).length < 2) return "0" + num.toString(16);
- else
- return num.toString(16);
- }
- /*****CPCL指令接口****/
- /**
- * 配置项如下
- * 设置打印纸张的范围
- * width: 标签纸的宽度,单位像素點
- * height: 标签纸的高度,单位像素點
- * 8像素=1mm
- * printNum: 打印张数,默认为1
- * rotation:页面整体旋转 1-90度 2-180度 3-270度 其他-不旋转
- */
- export function CreatCPCLPage(width, height, printNum, rotation = 0, offset = 0) {
- var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n';
- strCmd += "PAGE-WIDTH " + width + '\n';
- if (rotation == 1)
- strCmd += "ZPROTATE90\n";
- else if (rotation == 2)
- strCmd += "ZPROTATE180\n";
- else if (rotation == 3)
- strCmd += "ZPROTATE270\n";
- else
- strCmd += "ZPROTATE0\n";
- return strCmd;
- }
- // * 打印文字
- // * x: 文字方块左上角X座标,单位dot
- // * y: 文字方块左上角Y座标,单位dot
- // * fontName,fontSize: 字体,取值: 參考文檔
- // * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
- // * content: 文字内容
- // */
- export function addCPCLText(x, y, fontName, fontSize, rotation, content) {
- //console.log(fontName,fontSize,rotation, content);
- var strCmd = '';
- if (rotation == 1) {
- strCmd += 'T90 ';
- }
- if (rotation == 2) {
- strCmd += 'T180 ';
- }
- if (rotation == 3) {
- strCmd += 'T270 ';
- } else {
- strCmd += 'T ';
- }
- strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
- return strCmd;
- };
- /**
- * 打印文字
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * fontName,fontSize: 字体,取值: 參考文檔
- * strCmd += blesdk.addCPCLText(0,0,'4','244',0,'速购速递APP'); //改变默认字体
- * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
- * content: 文字内容
- */
- export function addCPCLTextil(x, y, fontName, fontSize, rotation, content) {
- //console.log(fontName,fontSize,rotation, content);
- const groupedArray = groupStrings(content);
- function groupStrings(str, size = 16) {
- const result = [];
- for (let i = 0; i < str.length; i += size) {
- result.push(str.substr(i, size));
- }
- return result;
- }
- var strCmd = '';
- for (var i in groupedArray) {
- if (rotation == 1) {
- strCmd += 'T90 ';
- } else if (rotation == 2) {
- strCmd += 'T180 ';
- } else if (rotation == 3) {
- strCmd += 'T270 ';
- } else if (rotation == 4) {
- strCmd += 'VT ';
- } else {
- strCmd += 'T ';
- }
- strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + (parseInt(y) + parseInt(i * 30)) + ' ' + groupedArray[i] +
- '\n';
- }
- return strCmd;
- };
- /**
- * 长文本打印
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * fontName,fontSize: 字体,取值: 參考文檔
- * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
- * content: 文字内容
- */
- export function LongText(x, y, fontName, fontSize, rotation, content, nu = 16) {
- var num = parseInt(content.length / nu)
- var arr = content.split('')
- var abb = []
- for (var n = 0; n <= num; n++) {
- var str = '';
- for (var i in arr) {
- if (i >= n * nu && i <= (n + 1) * nu) {
- str += arr[i]
- }
- }
- abb.push(str)
- }
- var strCmd = '';
- for (var i in abb) {
- // console.log(i, num)
- if (rotation == 1) {
- strCmd += 'T90 ';
- }
- if (rotation == 2) {
- strCmd += 'T180 ';
- }
- if (rotation == 3) {
- strCmd += 'T270 ';
- } else {
- strCmd += 'T ';
- }
- if (i != num && num != 0) {
- var acc = abb[i].split('')
- acc.pop()
- abb[i] = acc.join('')
- }
- strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + (parseInt(y) + parseInt(i * 30)) + ' ' + abb[i] + '\n';
- }
- return strCmd;
- };
- /**
- * 文本串联
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * fontName,fontSize: 字体,取值: 參考文檔
- * offset: 文本相对起始位置的偏置单位值
- * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
- * content: 文字内容
- */
- export function CascAde(x, y, fontName, fontSize, offset, content) {
- //console.log(fontName,fontSize,rotation, content);
- var strCmd = '';
- strCmd += 'CONCAT ';
- strCmd += x + ' ' + y + ' ' + '\n' + fontName + ' ' + fontSize + ' ' + '20' + ' ' + content + '\n' + '<ENDCONCAT>';
- // strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
- // console.log(strCmd, '14xxx擦拭发顺丰')
- return strCmd;
- };
- /**
- * 打印一维码
- *
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * codeType: 条码类型,取值为128、UPCA、UPCA2、UPCA5、UPCE、UPCE2、UPC5、EAN13、EAN13+2、EAN13+5、
- * EAN8、EAN8+2、EAN8+5、39、39C、F39、F39C、93、CODABAR、CODABAR16、ITF、I2OF5
- * h: 条码高度,单位dot
- * rotation: 顺时针旋转角度,取值如下:
- * - 0 不旋转
- * - 1 顺时针旋转90度
- *
- * narrow: 窄条码比例因子(dot) 取值: 參考文檔
- * wide: 宽条码比例因子(dot) 取值: 參考文檔
- * content: 文字内容
- *
- */
- export function addCPCLBarCode(x, y, codeType, h, rotation, narrow, wide, content) {
- var strCmd = '';
- if (rotation == 0)
- strCmd += 'B ';
- else
- strCmd += 'VB ';
- strCmd += codeType + ' ' + narrow + ' ' + wide + ' ' + h + ' ' + x + ' ' + y + ' ' + content + '\n'
- return strCmd;
- };
- /**
- * 打印二维码
- *
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * level: 错误纠正能力等级,取值为L(7%)、M(15%)、Q(25%)、H(30%)
- * ver: 1-10 版本,根据内容调整以获取合适容量
- * scale: 1-10 放大倍数
- * content: 文字内容
- *
- */
- export function addCPCLQRCode(x, y, level, ver, scale, content) {
- var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n';
- strCmd += 'ENDQR\n';
- return strCmd;
- };
- /**
- * 放大指令
- * scaleX: 横向放大倍数 1,2,3等整数
- * scaleY: 纵向放大倍数 1,2,3等整数
- */
- export function addCPCLSETMAG(scaleX, scaleY) {
- var strCmd = 'SETMAG ' + scaleX + ' ' + scaleY + '\n';
- return strCmd;
- };
- /**
- * 对齐指令 0-左对齐 1-右对齐 2-居中
- */
- export function addCPCLLocation(set) {
- var strCmd = '';
- if (set == 1) {
- strCmd += 'RIGHT\n';
- } else if (set == 2) {
- strCmd += 'CENTER\n';
- } else {
- strCmd += 'LEFT\n';
- }
- return strCmd;
- };
- /**
- * 反白线 x0,y0,x1,y1,width
- */
- export function addCPCLInverseLine(x0, y0, x1, y1, width) {
- var strCmd = 'IL ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
- return strCmd;
- };
- /**
- * 画线 x0,y0,x1,y1,width
- */
- export function addCPCLLine(x0, y0, x1, y1, width) { //下划线
- var strCmd = 'L ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
- return strCmd;
- };
- /**
- * 画框 x0,y0,x1,y1,width
- */
- export function addCPCLBox(x0, y0, x1, y1, width) {
- var strCmd = 'BOX ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
- return strCmd;
- };
- /**
- * 字体加粗
- */
- export function addCPCLSETBOLD(bold) {
- var strCmd = 'SETBOLD ' + bold + '\n';
- return strCmd;
- };
- /**
- * 字体下划线
- */
- export function addCPCLUNDERLINE(c) {
- var strCmd = 'UNDERLINE ';
- if (c) strCmd += 'ON\n';
- else if (c) strCmd += 'OFF\n';
- return strCmd;
- };
- /**
- * 水印打印灰度等级 0-255
- */
- export function addCPCLBACKGROUND(level) {
- var strCmd = 'BACKGROUND ';
- if (level > 255 || level < 0) level = 255;
- strCmd += level + '\n';
- return strCmd;
- };
- /**
- * 打印水印文字
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * fontName,fontSize: 字体,取值: 參考文檔
- * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
- * content: 文字内容
- */
- export function addCPCLBKVText(x, y, fontName, fontSize, rotation, content) {
- //console.log(fontName,fontSize,rotation, content);
- var strCmd = '';
- if (rotation == 1) {
- strCmd += 'BKT90 ';
- }
- if (rotation == 2) {
- strCmd += 'BKT180 ';
- }
- if (rotation == 3) {
- strCmd += 'BKT270 ';
- } else {
- strCmd += 'BKT ';
- }
- strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
- return strCmd;
- };
- /**
- * 标签缝隙定位指令
- */
- export function addCPCLGAP() {
- var strCmd = 'GAP-SENSE\nFORM\n';
- return strCmd;
- };
- /**
- * 标签右黑标检测指令
- */
- export function addCPCLSENSE() {
- var strCmd = 'BAR-SENSE\nFORM\n';
- return strCmd;
- };
- /**
- * 标签左黑标检测指令
- */
- export function addCPCLSENSELEFT() {
- var strCmd = 'BAR-SENSE LEFT\nFORM\n';
- return strCmd;
- };
- /**
- * 打印指令
- */
- export function addCPCLPrint() {
- var strCmd = 'PRINT\n';
- return strCmd;
- };
- /**
- * 图片打印指令
- * x: 文字方块左上角X座标,单位dot
- * y: 文字方块左上角Y座标,单位dot
- * data{
- threshold,//0/1提取的灰度级
- width,//图像宽度
- height,//图像高度
- imageData , //图像数据
- }
- */
- export function addCPCLImageCmd(x, y, data) {
- console.log(data.threshold, data.width, data.height, data.imageData,
- '查询==================================================')
- var strImgCmd = '';
- // const threshold = data.threshold || 180;
- const threshold = 180;
- let myBitmapWidth = data.width,
- myBitmapHeight = data.height;
- let len = parseInt((myBitmapWidth + 7) / 8); //一行的数据长度
- //console.log('len=',len);
- //console.log('myBitmapWidth=',myBitmapWidth);
- //console.log('myBitmapHeight=',myBitmapHeight);
- let ndata = 0;
- let i = 0;
- let j = 0;
- let sendImageData = new ArrayBuffer(len * myBitmapHeight);
- sendImageData = new Uint8Array(sendImageData);
- // let pix = data.imageData;
- let pix = 'https://pos.zdhtmf.com/uploads/20210810/8bf7551773bdb72fbb7f4be9ac608ea9.png';
- for (i = 0; i < myBitmapHeight; i++) {
- for (j = 0; j < len; j++) {
- sendImageData[ndata + j] = 0;
- }
- for (j = 0; j < myBitmapWidth; j++) {
- const grayPixle1 = grayPixle(pix.slice((i * myBitmapWidth + j) * 4, (i * myBitmapWidth + j) * 4 + 3));
- if (grayPixle1 < threshold)
- sendImageData[ndata + parseInt(j / 8)] |= (0x80 >> (j % 8));
- }
- ndata += len;
- }
- console.log('sendImageData=', sendImageData);
- //CPCL指令图片数据
- strImgCmd += 'EG ' + len + ' ' + myBitmapHeight + ' ' + x + ' ' + y +
- ' 00d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
- // console.log(strImgCmd,'查询1111111123455455');
- // for (i = 0; i < sendImageData.length; i++) {
- // strImgCmd += Hex2Str(sendImageData[i]);
- // console.log(strImgCmd,'查询休息下')
- // }
- // strImgCmd += '\n';
- // console.log(strImgCmd);
- return strImgCmd;
- }
- /**
- * toast显示捕获的蓝牙异常
- */
- export function catchToast(err) {
- const errMsg = {
- 10000: '未初始化蓝牙模块',
- 10001: '蓝牙未打开',
- 10002: '没有找到指定设备',
- 10003: '连接失败',
- 10004: '没有找到指定服务',
- 10005: '没有找到指定特征值',
- 10006: '当前连接已断开',
- 10007: '当前特征值不支持此操作',
- 10008: '系统上报异常',
- 10009: '系统版本低于 4.3 不支持BLE'
- };
- let coode = err.errCode ? err.errCode.toString() : '';
- let msg = errMsg[coode];
- plus.nativeUI.toast(msg || coode, {
- align: 'center',
- verticalAlign: 'center'
- });
- }
|