2012-07-14 204 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