anastaciocintra / escpos-coffee

Java library for ESC/POS printer
https://anastaciocintra.github.io/escpos-coffee
MIT License
277 stars 72 forks source link

Image printing sometimes prints random characters #51

Open AlbertoVillagomez opened 3 years ago

AlbertoVillagomez commented 3 years ago

When printing a ticket which has a logo, sometimes I get a bunch of random characters.

My code is packaged as an executable jar file My logo image is: 300 px width and 250 px height Thermal printer: Sanbee mini 58 mm Code sample

public class PrintTicket {
    private static BufferedImage logoImage = null;

    public void print(String printerName, List<String> processedData, String totalAmount, String id){
        PrintService printService = PrinterOutputStream.getPrintServiceByName(printerName);
        try {
            EscPos escpos = new EscPos(new PrinterOutputStream(printService));

            printImage(escpos);

            //... 

            escpos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printImage(EscPos escPos) throws IOException {
        if (logoImage == null) {
            logoImage = ImageIO.read(Objects.requireNonNull(PrintTicket.class.getClassLoader()
                    .getResource("images/logo.jpeg")));
        }
        escPos.write(new RasterBitImageWrapper(), new EscPosImage(new CoffeeImageImpl(logoImage), new BitonalThreshold()));
        escPos.feed(1);
    }
}

As you can see I load the image just once the first time it is needed as a "BufferedImage", my thinking (which was wrong) was that maybe the read operation was causing the issue. Any ideas on this?

anastaciocintra commented 3 years ago

Hi @AlbertoVillagomez, nothing wrong with your code, I beleave that we have a bug on lib. You open the same problem related by issue #42.

and we need to make effort to reproduce the error.

one point of attention is that the lib operations isn't thread-safe.

is your application multithreaded?

AlbertoVillagomez commented 3 years ago

Hi @anastaciocintra It is actually a single thread application

anastaciocintra commented 3 years ago

Ok, if one specific Ticket with ProcessedData, totalAmount and Id have problem, and you re-print the same ticket, it will print ok?

AlbertoVillagomez commented 3 years ago

Yes, I just need to reprint it for it to work

AlbertoVillagomez commented 3 years ago

But I'm curious in why this is printing such characters from time to time

anastaciocintra commented 3 years ago

is it image fixed for all printed tickets or you buid one custom image for eachone?

anastaciocintra commented 3 years ago

@AlbertoVillagomez, take a look on slice image

AlbertoVillagomez commented 3 years ago

Yes, it is always the same image. I will try that code and comeback with my findings later

palanganero commented 3 years ago

that happens to me too. once in five (more or less) instead of printing the image it prints random characters. But only by usb. By ethernet it always prints well. Is it the fault of my drivers and my printer?: https://postimg.cc/8sNF7BZx

bruno-dicastro commented 1 year ago

I'm facing the same problem, but it's not related with this library. Did anyone, @AlbertoVillagomez or @palanganero, discovered what is causing this?

haarlemnn commented 5 months ago

Hi guys, I'm facing the same issue... I'm using a STAR printer, the model is SK1-311. This printer works with ESC/POS commands and also StarPRNT commands, I've been testing the printer with this library and it works even using it in the StarPRNT function, but I can't print images.

My code is too simple

public static void printImage(EscPos escpos) throws IOException {
        BufferedImage logoImage = ImageIO.read(new URL("https://images.squarespace-cdn.com/content/v1/5d3f241fa4e0350001fa20d5/1636491460338-AIZAXV2978MGIDQE0GT7/qr-code.png?format=50w"));

        GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper();

        escpos.write(imageWrapper, new EscPosImage(new CoffeeImageImpl(logoImage), new BitonalOrderedDither()));;
        escpos.write("\u001Bd3\n");
    }
ShaoVN commented 3 months ago

I experienced this issue, and it was remedied by changing which implementation of the escpos.Image function. I was originally using bitImageRaster (the default implementation) and switched to bitImageColumn which has been performing well ever since (knock on wood). Specifically, in the docs, you are looking for

image(img_source, high_density_vertical=True, high_density_horizontal=True, impl='bitImageRaster', fragment_height=960, center=False)[source] Print an image.

You can select whether the printer should print in high density or not. The default value is high density. When printing in low density, the image will be stretched.

Esc/Pos supplies several commands for printing. This function supports three of them. Please try to vary the implementations if you have any problems. For example the printer IT80-002 will have trouble aligning images that are not printed in Column-mode.

The available printing implementations are:

bitImageRaster: prints with the GS v 0-command

graphics: prints with the GS ( L-command

bitImageColumn: prints with the ESC *-command

When trying to center an image make sure you have initialized the printer with a valid profile, that contains a media width pixel field. Otherwise the centering will have no effect.

Parameters : img_source – PIL image or filename to load: jpg, gif, png or bmp

high_density_vertical (bool) – print in high density in vertical direction default: True

high_density_horizontal (bool) – print in high density in horizontal direction default: True

impl (str) – choose image printing mode between bitImageRaster, graphics or bitImageColumn

fragment_height (int) – Images larger than this will be split into multiple fragments default: 960

center (bool) – Center image horizontally default: False

Return type : None