Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image printing sometimes prints random characters #51

Open
AlbertoVillagomez opened this issue Oct 7, 2020 · 12 comments
Open

Image printing sometimes prints random characters #51

AlbertoVillagomez opened this issue Oct 7, 2020 · 12 comments
Labels
bug Something isn't working

Comments

@AlbertoVillagomez
Copy link

AlbertoVillagomez commented Oct 7, 2020

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?

@AlbertoVillagomez AlbertoVillagomez added the bug Something isn't working label Oct 7, 2020
@anastaciocintra
Copy link
Owner

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
Copy link
Author

Hi @anastaciocintra
It is actually a single thread application

@anastaciocintra
Copy link
Owner

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

@AlbertoVillagomez
Copy link
Author

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

@AlbertoVillagomez
Copy link
Author

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

@anastaciocintra
Copy link
Owner

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

@anastaciocintra
Copy link
Owner

@AlbertoVillagomez,
take a look on slice image

@AlbertoVillagomez
Copy link
Author

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

@palanganero
Copy link

palanganero commented Nov 18, 2020

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
Copy link

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
Copy link

haarlemnn commented Mar 6, 2024

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
Copy link

ShaoVN commented May 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants