我正在尝试创建一个原型(prototype),以将文本文件的位图数据打印到支持 LAN 的爱普生 pos 打印机 TM-T88V。
虽然我可以毫无问题地发送文本和文本格式说明,但我不明白我必须做什么才能让我的打印机打印 Arecibo message 的数据.
前几行:
00000010101010000000000
00101000001010000000100
10001000100010010110010
10101010101010100100100
00000000000000000000000
00000000000011000000000
00000000001101000000000
00000000001101000000000
00000000010101000000000
00000000011111000000000
00000000000000000000000
11000011100011000011000
10000000000000110010000
11010001100011000011010
11111011111011111011111
00000000000000000000000
00010000000000000000010
00000000000000000000000
00001000000000000000001
消息有 73 行和 23 列,产生 1679 个图片元素。每个元素都由 1 表示黑色或 0 表示白色,并且应打印为 8x8(或 16x16)点的正方形。结果将导致
(来源:satsig.net)
根据打印机的规范:
虽然——正如我所说——连接和发送到打印机没有问题,但我只是不明白,这条指令想告诉我什么。在 Arecibo 消息的情况下会是什么
我必须向打印机发送什么数字?我需要发送每个点吗? nL,nH指定图像数据在水平方向的点数为(nL + nH × 256)
是什么意思?
这是我用于原型(prototype)制作的简单 Python 程序:
# -*- coding: utf-8 -*-
import struct
import socket
def sendInstructions(mySocket,l):
for x in l:
mySocket.send(struct.pack('h', *[x]),1)
def emphasizeOn(mySocket):
sendInstructions(mySocket,[27,33,48])
def emphasizeOff(mySocket):
sendInstructions(mySocket,[27,33,0])
def lineFeed(mySocket,number):
for i in range(number):
sendInstructions(mySocket,[0x0a,])
def paperCut(mySocket):
sendInstructions(mySocket,[29,86,0])
def sendText(mySocket,string):
mySocket.send(string.encode('UTF-8'))
def main():
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.connect(('192.168.1.15',9100))
lines = ["Hello,","World!"]
emphasizeOff(mySocket)
lineFeed(mySocket,2)
for l in lines:
if lines.index(l) == 0:
emphasizeOn(mySocket)
else:
emphasizeOff(mySocket)
sendText(mySocket,l)
lineFeed(mySocket,2)
lineFeed(mySocket,4)
paperCut(mySocket)
mySocket.close()
if __name__=="__main__":
main()
最佳答案
此命令一次生成图像的一个水平 strip 。 strip 的高度为 8 点或 24 点,具体取决于 m 的值。
nL 和 nH 是整数的低字节和高字节,指定图像水平 strip 的宽度(以点为单位)。该宽度计算为 nL + nH * 256,因此如果您希望图像宽度为 550 点,则 nH=2 和 nL=38。
参数d为位图数据;如果图像条高 8 个点,则每个字节代表条中的一列。如果 strip 高 24 点,则三个字节代表一列。
假设您在 WxH numpy 整数数组(1 或 0)中有 arecibo。您将:
data = np.zeros((W, H), dtype=np.ubyte)
## (fill in data here)
## Use m=33 since this is apparently the only mode with
## square pixels and also the highest resolution
## (unless it prints too slowly for your liking)
m = 33
nH = W // 256 ## note this is integer division, but SO's
## syntax hilighting thinks it looks like a comment.
nL = W % 256
## Divide the array into sections with shape Wx24:
for n in range(data.shape[1] // 24):
## Note that if the image height is not a multiple of 24,
## you'll have to pad it with zeros somehow.
strip = data[:, n*24:(n+1)*24]
## Convert each strip into a string of bytes:
strip = strip.reshape(W, 3, 8)
bytes = (strip * (2**np.arange(8)[np.newaxis, np.newaxis, :])).sum(axis=2) # magic
byteString = bytes.astype(np.ubyte).tostring()
## Send the command to POS
关于python - 通过 POS 打印机和图像打印向外国人开具账单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480340/