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

5.2k views Asked by At

I have an app that can print tickets with Thermal Printers using ESC POS language. 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:

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);
}
1

There are 1 answers

3
Elemental On

I have completed this task before in c++ and it isn't trivial. 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). The POS documentation should tell you how to print a single row of graphics using this method.

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. 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:

Send the ESC POS sequence that puts the printer into 'graphics mode'. Loop through the image band left to right. For each column of 8 pixels in the band:

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. 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.

Also I thought Epson had an android SDK for ESC/POS devices; I've never used it but thought it was free to use.