Search code examples
javaprintingthermal-printerescpos

ESC/POS image in java


I need to print this image in a thermal printer(SAM4S ELLIX40).

enter image description here

I've been searching and testing several codes but nothing actually works, they said "use this command and you'll see a line", I send that and nothing happens.

I tried this commands from manuals:

  • ESC * m nL nH d1…dk
  • GS * x y d1…dk

The one that I most tried was ESC *, but I never understood how to set nL and nH.

String text_to_print = "Hello world!";
DocPrintJob dpj = selected_printjob.createPrintJob();
InputStream stream = new ByteArrayInputStream((text_to_pring).getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(stream, flavor, null);
dpj.print(doc, null);

Solution

  • Question from Pedro (i do not know how to make reference to it): "with this library I'll have to change the way I send the data to the printer in my proyect, right?"

    Answering your last comment, I do not think that is totally apropriate to mix calls from diferent libs, but, occasionally, this can happen... lets go, before sending the image, we need to align the example of hello world // I think that to send a hello word to the thermal printer (ESC / POS) you could write the code this way. With this seed, you can enter new ESC / POS commands as needed. If your legacy code can be embedded in this example, then the answer to your question is yes, the escpos_coffee library works with OutputStream and can be silently embedded as in the following example. this code uses escpos_coffee

    package pedrojoaquim;
    
    import escpos.EscPos;
    import escpos.image.BitonalThreshold;
    import escpos.image.EscPosImage;
    import escpos.image.RasterBitImageWrapper;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    
    public class PedroJoaquim {
    
        public void printImage() throws PrintException, IOException{
            String text_to_print = "Hello world!";
            PrintService foundService = PrintServiceLookup.lookupDefaultPrintService();
            DocPrintJob dpj = foundService.createPrintJob();
    
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            /// your legacy commands ini
            // initialize printer
            outputStream.write(27); // ESC
            outputStream.write('@');
    
            // print text
            outputStream.write(text_to_print.getBytes());
    
            // feed 5 lines
            outputStream.write(27); // ESC
            outputStream.write('d');
            outputStream.write(5);
    
            // cut paper
            outputStream.write(29); // GS
            outputStream.write('V');
            outputStream.write(48);
    
            /// your legacy commands end
    
            /// fitting lib calls on same outputStream
            EscPos escpos = new EscPos(outputStream);
    
            RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
            BufferedImage  githubBufferedImage = ImageIO.read(new File("/Users/marco/Downloads/smile.jpg"));
            EscPosImage escposImage = new EscPosImage(githubBufferedImage, new BitonalThreshold()); 
            // print smile image...
            escpos.write(imageWrapper, escposImage);        
            // lib end
            /// legacy continues from this point
    
            // print text
            outputStream.write(" Joaquim's Smile image".getBytes());
    
            // feed 5 lines
            outputStream.write(27); // ESC
            outputStream.write('d');
            outputStream.write(5);
    
            // cut
            outputStream.write(29); // GS
            outputStream.write('V');
            outputStream.write(48);
    
    
            // do not forguet to close outputstream
            outputStream.close();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    
    
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            Doc doc = new SimpleDoc(inputStream, flavor, null);
            dpj.print(doc, null);
    
    
        }
    
        public static void main(String[] args) throws IOException {
            PedroJoaquim obj = new PedroJoaquim();
            try {
                obj.printImage();
            } catch (PrintException ex) {
                Logger.getLogger(PedroJoaquim.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    }