Search code examples
javaprintingescpos

ESCPOS Printer image out glitches


I'm working on a project, that reads the data from a generic source, formes an image using swing, than translates the image(a row of images) into escpos commands and sends it to the printer.

To transfer the image to escpos code i used java-escpos-image-printing material, but with a minor alteration:

            int n = 0;
            bos.write(printerSchema.getLineSpace24());
            for (int y = 0; y < image.length; y += 24) {
                // Like I said before, when done sending data,
                // the printer will resume to normal text printing
                if (n == 2) {
                    bos.write(printerSchema.getCutPaper());
                }
                bos.write(printerSchema.getImageMode());
                // Set nL and nH based on the width of the image
                bos.write(new byte[] { (byte) (0x00ff & image[y].length), (byte) ((0xff00 & image[y].length) >> 8) });
                for (int x = 0; x < image[y].length; x++) {
                    // for each stripe, recollect 3 bytes (3 bytes = 24 bits)
                    bos.write(recollectSlice(y, x, image));
                }

                // Do a line feed, if not the printing will resume on the same
                // line
                bos.write(printerSchema.getLineFeed());
                n++;

The alteration is a "cut-paper-command", that should init after the second line is drawn(physically, the printer has a large space between the cutter and the printer head).

All seem to be working fine, but sometimes i randomly recieve a missing second line(always before the cut paper command), sometimes with missing space(the first and third line just get together), and sometimes with a white space.

The printer: Sam4s Giant-100 Commands:

    INIT_PRINTER = new byte[]{0x1B,0x40},//1B 40 Initialize printer
    IMAGE_MODE = new byte[] { 0x1B, 0x2A, 33 }, LINE_FEED = new byte[] { 0x0A },
    LINE_SPACE_24 = new byte[] { 0x1B, 0x33, 24 }, LINE_SPACE_30 = new byte[] { 0x1B, 0x33, 30 },
    CUT_PAPER = new byte[] { 29, 86, 1 }; // 1B 33 n

Localized the problem to the part

if (n == 2) { bos.write(printerSchema.getCutPaper()); } the line before it isnt drawn.


Solution

  • You can use escpos-coffee library and print image with feed will works fine like this:

                /*
                 * to print one image we need to have:
                 * - one BufferedImage.
                 * - one bitonal algorithm to define what and how print on image.
                 * - one image wrapper to determine the command set to be used on 
                 * image printing and how to customize it.
                 */
    
                // creating the EscPosImage, need buffered image and algorithm.
                URL imageURL = getURL("dog.png"); 
                BufferedImage  imageBufferedImage = ImageIO.read(imageURL);
    
    
                // this wrapper uses esc/pos sequence: "GS 'v' '0'"
                RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
    
    
    
                escpos = new EscPos(new PrinterOutputStream(printService));
    
    
                escpos.feed(5);
                escpos.writeLF("BitonalThreshold()");
                // using bitonal threshold for dithering
                Bitonal algorithm = new BitonalThreshold(); 
                EscPosImage escposImage = new EscPosImage(imageBufferedImage, algorithm);     
                escpos.write(imageWrapper, escposImage);
    
                escpos.feed(5);
    
                escpos.cut(EscPos.CutMode.PART);