[Misc] printer - LanceaKing xp0int Posted on May 20 2019 在476包里找到一个控制信号: #### 476 ``` 2MFG:4BARCODE;CMD:TSPL2;MDL:3B-363B;CLS:PRINTER; ``` 打印机用的是TSPL2语言。 674、675包是输入的程序: #### 674 ``` SIZE 47.5 mm, 80.1 mm GAP 3 mm, 0 mm DIRECTION 0,0 REFERENCE 0,0 OFFSET 0 mm SET PEEL OFF SET CUTTER OFF SET PARTIAL_CUTTER OFF ``` #### 675 ``` SET TEAR ON CLS BITMAP 138,75,26,48,1,…… BITMAP 130,579,29,32,1,…… BAR 348, 439, 2, 96 BAR 292, 535, 56, 2 …… BAR 152, 351, 16, 2 BAR 152, 351, 2, 16 PRINT 1,1 ``` 查文档分析程序: https://www.mediaform.de/fileadmin/support/handbuecher/Armilla/Handbuecher/TSC\_TSPL\_TSPL2\_Programming.pdf 下面是重要的命令: #### SIZE 纸张的大小,1mm等于8像素或12像素,经测试是8像素。 #### BITMAP 将BMP图像打印出来,前四个参数分别是打印位置X、Y和图像长宽,第五个参数是模式,不需要管,第六个参数是图像数据,注意是二值,长是字节数,宽是像素数。 #### BAR 画长方形,前四个参数也是打印位置X、Y和长宽。 接下来用PIL模拟打印过程,最后要旋转180°: #### solve.py ``` from PIL import Image, ImageDraw W_mm, H_mm = 47.5, 80.1 W, H = int(W_mm * 8), int(H_mm * 8) img = Image.new('1', (W, H), 1) fp = open('675.bin', 'rb') print(fp.readline().strip()) # SET TEAR ON print(fp.readline().strip()) # CLS bitmap_line = fp.readline().strip() cmd, params = bitmap_line.split(b' ', 1) X, Y, W, H, mode, data = params.split(b',', 5) X, Y, W, H, mode = map(int, (X, Y, W, H, mode)) print(cmd, X, Y, W, H, mode) img.paste(Image.frombytes('1', (W * 8, H), data), (X, Y)) bitmap_line = fp.readline().strip() cmd, params = bitmap_line.split(b' ', 1) X, Y, W, H, mode, data = params.split(b',', 5) X, Y, W, H, mode = map(int, (X, Y, W, H, mode)) print(cmd, X, Y, W, H, mode) img.paste(Image.frombytes('1', (W * 8, H), data), (X, Y)) draw = ImageDraw.Draw(img) bar_line = fp.readline().strip() while bar_line != b'PRINT 1,1': cmd, params = bar_line.split(b' ', 1) X, Y, W, H = map(int, params.split(b', ', 3)) draw.rectangle([(X, Y), (X + W, Y + H)], fill=0) print(cmd, X, Y, W, H) bar_line = fp.readline().strip() fp.close() img.rotate(180).save('output.png') ``` #### output.png ![output.png](https://leanote.com/api/file/getImage?fileId=5ce216c7ab6441493700541d) 打赏还是打残,这是个问题 赏 Wechat Pay Alipay [Misc] draw - Donek1 [Pwn] manynotes - cpt.shao
没有帐号? 立即注册