简体   繁体   English

如何在Android(Java)中使用Esc Pos打印图像

[英]How can I print an image with Esc Pos with Android (Java)

I have an app that can print tickets with Thermal Printers using ESC POS language. 我有一个应用程序,可以使用ESC POS语言通过热敏打印机打印票证。 The code I'm using right now is: 我现在使用的代码是:

       /* <-40char-> */
                    Socket sock = new Socket(Impresora.getImpresora_Tickets().getIp(), Impresora.getImpresora_Tickets().getPuerto());
                    OutputStreamWriter osw = new OutputStreamWriter(sock.getOutputStream(), Charset.forName("CP1252"));
                    PrintWriter oStream = new PrintWriter(osw);

       /*Start*/
                    for(int i = 0; i<Impresora.getImpresora_Tickets().getInic().size(); i++)
                        oStream.print(Impresora.getImpresora_Tickets().getInic().get(i));

       /*Set Font Size*/
                    for(int i = 0; i<Impresora.getImpresora_Tickets().getLetra4().size(); i++)
                            oStream.print(Impresora.getImpresora_Tickets().getLetra4().get(i));

       oStream.println("HELLO WORLD");

And it works fine. 而且效果很好。 The thing is that now I'm capturing the user's signature with the tablet and I want to print it in the end of the ticket. 事实是,现在我正在使用平板电脑捕获用户的签名,并且希望在票证的末尾打印它。 I have it as a bitmap object but I don't know how to send it to the printer. 我将其作为位图对象,但是我不知道如何将其发送到打印机。 Can someone help me? 有人能帮我吗? Thanks! 谢谢!

EDIT 1: 编辑1:

I'm trying to do something but I think I'm not going in the right way... 我正在尝试做某事,但我认为我做事的方式不正确。

/**
* Redimensionar imagen
*/
Bitmap firma = Current_Mesa.getT().getFirma_credito();
firma = Bitmap.createScaledBitmap(firma, 255, 64, false);

/**
* Print imagen
*/
ByteArrayOutputStream stream = new ByteArrayOutputStream();
firma.compress(CompressFormat.JPEG, 70, stream);
byte[] firma_bytes = stream.toByteArray();

byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33};
byte[] SET_LINE_SPACE_24 = {0x1B, 0x33, 24};
byte[] PRINT_AND_FEED_PAPER = new byte[]{0x0A};

for(byte b : SELECT_BIT_IMAGE_MODE)
oStream.print((char)b);

for(byte b : SET_LINE_SPACE_24)
oStream.print((char)b);

for(int i = 0; i < firma_bytes.length; i+=8)
{
    for(int plus = 0; plus < 8; plus++)
    oStream.print(firma_bytes[i+plus]);

    for(byte b : PRINT_AND_FEED_PAPER)
        oStream.print((char)b);
}

I have completed this task before in c++ and it isn't trivial. 我之前已经在c ++中完成了此任务,而且它并不简单。 You need to get pixel by pixel access to the image (which should be easy if you have a bitmap). 您需要逐像素访问图像(如果有位图,这应该很容易)。

Then you have to divide the image into 8 pixel horizontal bands and get a character representing each column of eight pixels (using a bitwise or). 然后,您必须将图像划分为8个像素的水平带,并得到一个代表每列8个像素的字符(按位或)。 The POS documentation should tell you how to print a single row of graphics using this method. POS文档应告诉您如何使用此方法打印一行图形。

None of this is very hard but it took a bit of fiddling to get it perfect for all image sizes and shapes. 这些都不是很难,但是花了一点时间才能使其完美适合所有图像尺寸和形状。

A more detailed description: The printer can print images left to right in 8 pixel vertical bands, that means it first print the top 8 pixels of the image - then scrolls the paper and prints the next 8 pixel row. 更详细的描述:打印机可以在8像素垂直带中从左到右打印图像,这意味着它首先打印图像的前8像素-然后滚动纸张并打印下一个8像素行。 So you do it like this: 所以你这样做:

Go down the image starting at the top and divide it into 8 pixel high bands.For each band: 从顶部开始向下浏览图像,并将其分为8个像素高条带。

Send the ESC POS sequence that puts the printer into 'graphics mode'. 发送使打印机进入“图形模式”的ESC POS序列。 Loop through the image band left to right. 从左到右遍历图像带。 For each column of 8 pixels in the band: 对于带中8像素的每一列:

Work out the bit values of the pixels top to bottom. 从上到下计算像素的位值。 Convert this to a simple byte values like so IsPixelBlack(0)*1+IsPixelBlack(1)*2+IsPixelBlack(2)*4+...+IsPixelBlack(7)*128 where IsPixelBlack(x) is 1 if the x pixel in the column is black or 0 otherwise. 将其转换为简单的字节值,例如IsPixelBlack(0)* 1 + IsPixelBlack(1)* 2 + IsPixelBlack(2)* 4 + ... + IsPixelBlack(7)* 128,如果x为1,则IsPixelBlack(x)为1列中的像素为黑色,否则为0。 Then send this byte value to the printer as a character. 然后将此字节值作为字符发送给打印机。

So you end up sending one character for every 8 pixel column expressed as string of one band of data to be printed. 因此,您最终每8个像素列发送一个字符,表示为要打印的一个数据带的字符串。

Also I thought Epson had an android SDK for ESC/POS devices; 我还认为爱普生有一个用于ESC / POS设备的android SDK; I've never used it but thought it was free to use. 我从未使用过它,但认为它是免费使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM