Javascript arrayofbyte替代?

问题描述 投票:0回答:1

所以我正在尝试将粗体文本打印到外部打印机。

在Android中,这可以通过以下方式完成,

    byte[] arrayOfByte1 = { 27, 33, 0 };
    byte[] format = { 27, 33, 0 };

    format[2] = ((byte)(0x8 | arrayOfByte1[2]));
    os.write(format);

    os.write( string.getBytes());

    format[2] = ((byte)(arrayOfByte1[2]));
    os.write(format);

我相信BOLD_ON + string + BOLD_OFF。

现在在Javascript中,我不知道如何编写等效的。

我管理的最好的是下面,只有大胆的作品,它不会关闭。

var lines = [];
    lines.push("\x1b\x40"); // Init
    lines.push("\x1b\x21\x00"); // Normal
    lines.push("This is normal text\n");
    lines.push("\x1b\x45\x01"); // Bold On
    lines.push("This is bold text\n");
    lines.push("\x1b\x45\x00"); // Bold Off
    lines.push("\x1b\x35"); // Italic On
    lines.push("This is italic text\n");
    lines.push("\x1b\x34"); // Italic Off

        for (let index = 0; index < lines.length; index++) 
        {
             printer.write(lines[i]);
        }

任何帮助赞赏。

javascript arrays byte thermal-printer
1个回答
0
投票

我绝对不确定这是否会起作用,但是你可以将这些字节发送到打印机吗?

var esc = "\u001B",
    lines = [
    ESC + "@",
    ESC + "E\u0001",
    ESC + "E\0"
];
function getBinaryRep(lines,joiner = "\n"){
    lines = lines.join(joiner);
    var blob = new Blob([lines],{type:"text/plain"}),
        reader = new FileReader;
    return new Promise(function(res,rej){
        reader.onload = function(){
            res(Array.from(new Uint8Array(this.result)));
        };
        reader.readAsArrayBuffer(blob);
    });
}

var x= getBinaryRep(lines);
x.then((x)=>console.log(x)); //[27, 64, 10, 27, 69, 1, 10, 27, 69, 0]

then内部而不是console.logging将它们发送到打印机以查看出来的内容。

© www.soinside.com 2019 - 2024. All rights reserved.