Skip to content

Commit

Permalink
fix(): 修复了打印宽度随意修改出现打印乱码问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zouguotao committed Apr 19, 2022
1 parent 89563c6 commit e6a0da4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 69 deletions.
11 changes: 8 additions & 3 deletions pages/blueList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bluetoothConfig = getBluetoothPageConfig();
Page({
data: {
...bluetoothConfig.data,
// 打印机纸张宽度
// 打印机纸张宽度,我用的打印几的纸张最大宽度是384,可以修改其他的
paperWidth: 384,
canvasWidth: 1,
canvasHeight: 1,
Expand Down Expand Up @@ -102,14 +102,18 @@ Page({
wx.getImageInfo({
src: tempFilePath,
success: (res) => {
const w = this.data.paperWidth;
// 打印宽度须是8的整数倍,这里处理掉多余的,使得宽度合适,不然有可能乱码
const mw = this.data.paperWidth % 8;
const w = mw === 0 ? this.data.paperWidth : this.data.paperWidth - mw;
// 等比算出图片的高度
const h = Math.floor((res.height * w) / res.width);
// 设置canvas宽高
this.setData({
img: tempFilePath,
canvasHeight: h,
canvasWidth: w,
});
//canvas 画一张图片
// 在canvas 画一张图片
ctx.fillStyle = 'rgba(255,255,255,1)';
ctx.clearRect(0, 0, w, h);
ctx.fillRect(0, 0, w, h);
Expand Down Expand Up @@ -197,6 +201,7 @@ Page({
//打印图片
printImage(
opt,
// 将图片数据转成位图数据
overwriteImageData({
threshold: threshold[0],
imageData: pix,
Expand Down
123 changes: 64 additions & 59 deletions project.config.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
{
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"urlCheck": false,
"scopeDataCheck": false,
"coverView": true,
"es6": true,
"postcss": true,
"compileHotReLoad": false,
"preloadBackgroundData": false,
"minified": true,
"autoAudits": false,
"newFeature": true,
"uglifyFileName": true,
"uploadWithSourceMap": true,
"useIsolateContext": true,
"nodeModules": true,
"enhance": false,
"useCompilerModule": false,
"userConfirmedUseCompilerModuleSwitch": false,
"showShadowRootInWxmlPanel": true,
"checkInvalidKey": true,
"checkSiteMap": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
}
},
"compileType": "miniprogram",
"libVersion": "2.11.3",
"appid": "wxb7c0c09c309a2698",
"projectname": "%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E8%93%9D%E7%89%99%E6%89%93%E5%8D%B0demo",
"debugOptions": {
"hidedInDevtools": []
},
"isGameTourist": false,
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"condition": {
"search": {
"current": -1,
"list": []
},
"conversation": {
"current": -1,
"list": []
},
"game": {
"currentL": -1,
"list": []
},
"miniprogram": {
"current": -1,
"list": []
}
}
"description": "项目配置文件",
"packOptions": {
"ignore": []
},
"setting": {
"urlCheck": false,
"es6": true,
"enhance": false,
"postcss": true,
"preloadBackgroundData": false,
"minified": true,
"newFeature": true,
"coverView": true,
"nodeModules": true,
"autoAudits": false,
"showShadowRootInWxmlPanel": true,
"scopeDataCheck": false,
"uglifyFileName": true,
"checkInvalidKey": true,
"checkSiteMap": true,
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"enableEngineNative": false,
"useIsolateContext": true,
"useCompilerModule": false,
"userConfirmedUseCompilerModuleSwitch": false,
"userConfirmedBundleSwitch": false,
"packNpmManually": false,
"packNpmRelationList": [],
"minifyWXSS": true
},
"compileType": "miniprogram",
"libVersion": "2.14.1",
"appid": "wxb7c0c09c309a2698",
"projectname": "%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E8%93%9D%E7%89%99%E6%89%93%E5%8D%B0demo",
"debugOptions": {
"hidedInDevtools": []
},
"isGameTourist": false,
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"condition": {
"search": {
"list": []
},
"conversation": {
"list": []
},
"game": {
"currentL": -1,
"list": []
},
"miniprogram": {
"list": []
}
}
}
24 changes: 17 additions & 7 deletions wx-weapp-tool/bluetoolth.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,28 @@ export function printImage(opt = {}, imageInfo = {}) {
let arr = imageInfo.array,
width = imageInfo.width;
const writeArray = [];
const h = arr.length / width;
const xl = width % 256;
const xh = width / 256;
const xh = (width - xl) / 256;
const yl = h % 256;
const yh = (h - yl) / 256;
//分行发送图片数据
const command = []
.concat(printCommand.clear)
.concat(printCommand[printAlign])
.concat([29, 118, 48, 0, xl, xh, 1, 0]);
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));
}
.concat([29, 118, 48, 0, xl, xh, yl, yh]);

// 分段逐行打印
// .concat([29, 118, 48, 0, xl, xh, 1, 0]);
// 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(command.concat(arr)));

const len = writeArray.length;
const print = (options, writeArray) => {
if (writeArray.length) {
Expand Down

0 comments on commit e6a0da4

Please sign in to comment.