收藏
回答

小程序打印图片,打印机打印出来是乱码 怎么办?求大佬解答,全网没有教程

wx.createSelectorQuery()
        .select('#secondCanvas')
        .fields({ nodetruesizetrue })
        .exec(
            (res) => {
                console.log('1111111111111111111')
                console.log(res)
                // Canvas 对象
                const canvas = res[0].node
                // Canvas 画布的实际绘制宽高
                const renderWidth = res[0].width
                const renderHeight = res[0].height
                // Canvas 绘制上下文
                const ctx = canvas.getContext('2d')
        
                // 初始化画布大小
                const dpr = wx.getSystemInfoSync().pixelRatio
                canvas.width = renderWidth * dpr
                canvas.height = renderHeight * dpr
                ctx.scale(dpr, dpr)
// 若干绘制调用



// 这里绘制完成
                    console.log('draw done')
                const img = canvas.createImage()


                img.onload = () => {
                    ctx.drawImage(
                        img,0,0)
                }
                img.src='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png'
                wx.downloadFile({
                    url'https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png',
                    successfunction(res{
                      // 下载成功后将文件转为临时路径
                      if (res.statusCode === 200) {
                        wx.setStorageSync('tempFilePath', res.tempFilePath);
                      }
                    }
                  });


                        this.setData({
                            img: wx.getStorageSync('tempFilePath'),
                            canvasHeight: canvas.height,
                            canvasWidth:  canvas.width,
                        });
        const { canvasWidth, canvasHeight, deviceId, threshold } = this.data;


                            const pix =res[0].node;
                            const opt = {
                                deviceId,
                                ...this.character,
                                onProgress(percentage) => {
                                    console.log('percentage', percentage);
                                    this.setData({
                                        percentage,
                                    });
                                },
                                lasterSuccess() => {
                                    console.log('laster success');
                                    this.setData({
                                        printingfalse,
                                        percentage0,
                                    });
                                    wx.showModal({
                                        title'提示',
                                        content'数据已发送完,请检查打印的内容是否正常',
                                        showCancelfalse,
                                        confirmText'好的',
                                    });
                                },
                            };
                            this.setData({
                                printingtrue,
                            });
                            console.log(threshold[0])
                            console.log(pix)


                            console.log(opt)
                            //打印图片
                            printImage(
                                opt,
                                overwriteImageData({
                                    threshold: threshold[0],
                                    imageData: pix,
                                    width: canvasWidth,
                                    height: canvasHeight,
                                }),
                            );
                 
            }
回答关注问题邀请回答
收藏

1 个回答

  • °   村东头小伙
    ° 村东头小伙
    04-25

    楼主找到解决办法了吗?我也碰到了,解决了给我个demo,谢谢

    04-25
    有用
    回复
登录 后发表内容