2012-07-14 143 views
14

我想创建一个原型打印到我的LAN启用epson pos打印机TM-T88V文本文件的位图数据。通过POS打印机和图像打印帐单外国人

虽然我没有问题发送文本和文本格式说明,但我不明白,我必须做什么来使我的打印机打印Arecibo message的数据。

前几行:

00000010101010000000000 
00101000001010000000100 
10001000100010010110010 
10101010101010100100100 
00000000000000000000000 
00000000000011000000000 
00000000001101000000000 
00000000001101000000000 
00000000010101000000000 
00000000011111000000000 
00000000000000000000000 
11000011100011000011000 
10000000000000110010000 
11010001100011000011010 
11111011111011111011111 
00000000000000000000000 
00010000000000000000010 
00000000000000000000000 
00001000000000000000001 

该消息具有73行和导致1679的图像元素23名的列。这些元素中的每一个都由1代表黑色或0代表白色,并且应该打印为8x8(或16x16)点的正方形。其结果必然导致

Arecibo message http://www.satsig.net/seti/message-to-gliese-581.gif

从打印机的规格:

enter image description here

虽然 - 正如我所说的 - 连接和发送到打印机是没有问题的,我只是不明白,这条指令要告诉我什么。在Arecibo消息的情况下,将会发生什么?

我必须向打印机发送什么号码?我需要发送每一个点吗? nL, nH specify the number of dots of the image data in the horizontal direction as (nL + nH × 256).是什么意思?

这是我的简单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() 
+0

而这是什么都与结算外星人呢? – LarsH 2012-07-14 01:54:21

+1

+1帐单外国人 – Seth 2012-07-14 01:56:50

+1

@LarsH - 只是为了提高你的注意力:)......不认真:我需要打印位图数据,因为我觉得我需要说明这一点,所以我想到了一个着名的例子,A​​recibo消息。但是,如果你是我们的POS打印机,你会发送一个外星人?票据! – vikingosegundo 2012-07-14 02:01:20

回答

6

该命令在时刻生成该图像的一个水平条。条带的高度为8或24个点,具体取决于m的值。

nL和nH是一个整数的低字节和高字节,用于指定水平条形图像的点宽度。该宽度计算为nL + nH * 256,因此如果您想要图像为550点宽,则nH = 2且nL = 38。

参数d是位图数据;如果图像条的高度为8个点,则每个字节表示条中的一列。如果条带高24个点,则三个字节代表一列。

因此,让我们假设你有阿雷西博的整数,1的宽x高numpy的阵列或0您可以:

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