在Custom TG2480H打印机上打印徽标

Rob*_*die 5 printing bytebuffer bitmap typescript angular

我正在使用Custom TG2480-H打印机打印收据.除了徽标外,我已收到所有收据.

我有一个.bmp代表徽标的单色文件,并且具有正确的尺寸.

我正在使用不同的响应类型检索文件:

this.http.get('/assets/images/brand/logo-bnw.bmp', {
  responseType: ResponseContentType.Blob
}).subscribe((response: Response) => {
  console.log('Blob bytes', this.kiowarePrintService.getBytesFromText(response.text()));
});
this.http.get('/assets/images/brand/logo-bnw.bmp', {
  responseType: ResponseContentType.Text
}).subscribe((response: Response) => {
  console.log('Text bytes', this.kiowarePrintService.getBytesFromText(response.text()));
});
this.http.get('/assets/images/brand/logo-bnw.bmp', {
  responseType: ResponseContentType.ArrayBuffer
}).subscribe((response: Response) => {
  console.log('ArrayBuffer bytes', this.kiowarePrintService.getBytesFromText(response.text()));
});
Run Code Online (Sandbox Code Playgroud)

这是关于ResponseContentType的文档以及它可以具有的不同值.

这是以下代码getBytesFromText:

getBytesFromText(text: string) {
    const bytes: number[] = [];
    for (let i = 0; i < text.length; i++) {
      bytes.push(text.charCodeAt(i));
    }
    return bytes;
  }
Run Code Online (Sandbox Code Playgroud)

将响应转换为字节会打印以下值:

Blob bytes (2) [123, 125]

Text bytes (951) [66, 77, 65533, 3, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 40, 0, 0, 0, 120, 0, 0, 0, 56, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 65533, 3, 0, 0, 65533, 14, 0, 0, 65533, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533…]

ArrayBuffer bytes (479) [19778, 958, 0, 0, 0, 62, 0, 40, 0, 120, 0, 56, 0, 1, 1, 0, 0, 896, 0, 3780, 0, 3780, 0, 0, 0, 0, 0, 0, 0, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 255, 65535, 65535, 65535, 65535, 65535…]
Run Code Online (Sandbox Code Playgroud)

这些似乎都是错误的,Blob字节的数量太低,而其他两个都包含大于字节的数字(> 255).

我已经尝试将这些字节发送到打印机,并提出以下内容:

编辑:试图添加'content-type': 'image/bmp'标题:

    const headers: Headers = new Headers({'Content-Type': 'image/bmp'});
    const options: RequestOptions = new RequestOptions({headers: headers});
    this.http.get('/assets/images/brand/logo-bnw.bmp', options).subscribe((response: Response) => {
      console.log('image/bmp bytes', this.kiowarePrintService.getBytesFromText(response.text()));
    });
Run Code Online (Sandbox Code Playgroud)

记录下来:

image/bmp bytes (951) [66, 77, 65533, 3, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 40, 0, 0, 0, 120, 0, 0, 0, 56, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 65533, 3, 0, 0, 65533, 14, 0, 0, 65533, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65533, 0, 65533, 65533, 65533, 65533, 65533, 65533…]
Run Code Online (Sandbox Code Playgroud)

看起来与文本响应相同

Lyn*_*ing 2

您对所有返回类型使用response.text()。对于二进制数据,您应该将其保留为 blob。您可以使用以下方法访问底层字节而无需转换为文本

response.arrayBuffer()
Run Code Online (Sandbox Code Playgroud)

这应该会给你你所期望的正确的字节数组。

这个问题也相关:Get Image or byte data with http

最佳答案采用以下方法:

var blob = new Blob([new Uint8Array(response._body)]
Run Code Online (Sandbox Code Playgroud)