You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查看GRF文件?ZPL格式TXT文件预览工具及代码咨询

查看ZPL图形文件(~DGR指令)的工具与代码方法

你看到的~DGR:DEMO.GRF,124236,102,:Z64:是ZPL的图形存储指令,指令后通常跟着Base64编码的图形数据,要查看文件内容可以用以下方式:

一、桌面工具

  • Zebra Design Studio:官方的ZPL设计工具,直接导入文件就能渲染预览图形,完美支持解析~DGR这类图形指令。
  • 本地ZPL预览工具:多数桌面版ZPL查看器都支持解码ZPL中的Base64图形数据,加载文件后即可显示图形。

二、Python代码实现

通过代码解码Base64数据并生成图像,步骤如下:

import base64
from PIL import Image
import numpy as np

# 读取目标TXT文件
with open("你的文件名称.txt", "r") as f:
    zpl_content = f.read()

# 提取指令后的Base64编码部分
z64_data = zpl_content.split(":Z64:")[1].strip()

# 解码Base64
decoded_bytes = base64.b64decode(z64_data)

# 解析ZPL参数:每行102字节,总字节数124236
bytes_per_row = 102
total_bytes = 124236
image_width = bytes_per_row * 8  # 每个字节对应8个像素点
image_height = total_bytes // bytes_per_row

# 转换为像素数组并反转黑白(ZPL中0代表黑色,1代表白色)
pixel_array = np.unpackbits(np.frombuffer(decoded_bytes, dtype=np.uint8))
pixel_array = pixel_array.reshape((image_height, image_width))
pixel_array = 255 - (pixel_array * 255)

# 生成并预览图像
preview_img = Image.fromarray(pixel_array.astype(np.uint8), mode="L")
preview_img.save("图形预览.png")
preview_img.show()

三、本地HTML预览页面

自己写一个简单的网页,用JS解码并渲染图形,无需联网:

<!DOCTYPE html>
<html>
<body>
  <textarea id="zplContent" rows="10" cols="80">粘贴你的ZPL文件内容</textarea>
  <button onclick="showPreview()">生成预览</button>
  <canvas id="previewCanvas"></canvas>

  <script>
    function showPreview() {
      const zplText = document.getElementById('zplContent').value;
      const z64Part = zplText.split(':Z64:')[1].trim();
      const decodedStr = atob(z64Part);
      
      // 对应ZPL参数:每行102字节,总字节124236
      const bytesPerRow = 102;
      const totalBytes = 124236;
      const canvasWidth = bytesPerRow * 8;
      const canvasHeight = totalBytes / bytesPerRow;

      const canvas = document.getElementById('previewCanvas');
      canvas.width = canvasWidth;
      canvas.height = canvasHeight;
      const ctx = canvas.getContext('2d');
      const imgData = ctx.createImageData(canvasWidth, canvasHeight);

      let pixelPos = 0;
      for (let i = 0; i < decodedStr.length; i++) {
        const byteValue = decodedStr.charCodeAt(i);
        // 遍历字节的每一位,转换为像素
        for (let bit = 7; bit >= 0; bit--) {
          const isBlack = (byteValue >> bit) & 1;
          // ZPL黑白逻辑反转,设置RGB值
          imgData.data[pixelPos] = isBlack ? 255 : 0;
          imgData.data[pixelPos + 1] = isBlack ? 255 : 0;
          imgData.data[pixelPos + 2] = isBlack ? 255 : 0;
          imgData.data[pixelPos + 3] = 255;
          pixelPos += 4;
        }
      }

      ctx.putImageData(imgData, 0, 0);
    }
  </script>
</body>
</html>

将代码保存为.html文件,本地打开后粘贴ZPL内容,点击按钮即可预览图形。

内容的提问来源于stack exchange,提问作者Rian Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 14:05:24