代码之家  ›  专栏  ›  技术社区  ›  Robin-Hoodie

在自定义TG2480H打印机上打印徽标

  •  5
  • Robin-Hoodie  · 技术社区  · 8 年前

    我有单色的 .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()));
    });
    

    ResponseContentType 以及它可能具有的不同值。

    getBytesFromText :

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

    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…]
    

    这些似乎都是错误的,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()));
        });
    

    记录以下内容:

    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…]
    

    1 回复  |  直到 8 年前
        1
  •  2
  •   Lynn Crumbling    8 年前

    response.arrayBuffer()
    

    这将为您提供所需的正确字节数组。

    这个问题也很重要: Get Image or byte data with http

    最重要的答案是:

    var blob = new Blob([new Uint8Array(response._body)]