anastaciocintra / escpos-coffee

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

Rotation/Orientation #58

Open proxseas opened 3 years ago

proxseas commented 3 years ago

To my knowledge, there is currently no way to set the print orientation in this library. To me this is important, because one of the receipt printers I'm working with prints upside down, by default. This is something I'm able to change in the printer settings in Windows and then prove that this change worked by printing a test page (through Windows). However, when I use EscPos, it bypasses all the Windows settings, so I think this feature should be made available through the EscPos library.

anastaciocintra commented 3 years ago

Hi @proxseas Yes, this lib doesn't have rotation. But.. you can find on samples how to print a pdf, html or how to build one image of the receipt. With these tools you can apply one way to rotate.

proxseas commented 3 years ago

I'll have to look into that. If it's trivial to turn text into an image that eventually looks the same when printed, then this might be my solution. Thanks.

anastaciocintra commented 3 years ago

take a look at https://github.com/anastaciocintra/escpos-coffee-samples/blob/master/miscellaneous/CoffeeBitmap/src/main/java/CoffeeBitmap.java

and how to rotate image ... https://coderanch.com/t/485958/java/Rotating-buffered-image

putting all together


public class Rotate {
    public void exec(EscPos escpos) throws IOException {

        String html = "" +
                "<html>\n" +
                "<head>\n" +
                "<style>\n" +
                "body {\n" +
                "  background-color: lightblue;\n" +
                "}\n" +
                "\n" +
                "h1 {\n" +
                "  text-align: center;\n" +
                "}\n" +
                "\n" +
                "p {\n" +
                "  font-family: verdana;\n" +
                "  font-size: 20px;\n" +
                "}\n" +
                "p.korean {\n" +
                "  font-family: Single Day;\n" +
                "  font-size: 20px;\n" +
                "}\n" +
                "</style>\n" +
                "</head>" +
                "<body>" +
                "<h1>Hello, world.</h1>" +
                "<p>الصفحة الرئيسية \n" + // Arabiac
                "<br>你好,世界 \n" + // Chinese
                "<br>こんにちは世界 \n" + // Japanese
                "<br>Привет мир \n" + // Russian
                "<br>नमस्ते दुनिया \n" + //  Hindi
                "<p class=\"korean\"><br>안녕하세요 세계</p>" + // if necessary, you can download and install on your environment the Single Day from fonts.google...
                "</body>"
                ;

        int width = 576, height = 576;
        // Create a `BufferedImage` and create the its `Graphics`
        BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration()
                .createCompatibleImage(width, height);
        Graphics graphics = image.createGraphics();
        // Create an `JEditorPane` and invoke `print(Graphics)`

//        JEditorPane jep = new JEditorPane("text/html", html);
        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");

        jep.setText(html);
        jep.setSize(width, height);
        jep.print(graphics);

        BufferedImage rotatedImage = rotate(image);
        // send the graphic to the escpos printer...
        new ImageHelper().write(escpos, new CoffeeImageImpl(rotatedImage),new RasterBitImageWrapper(),new BitonalThreshold());
        escpos.feed(5).cut(EscPos.CutMode.FULL);
        escpos.close();

        /* DEBUG, you can save the image */
        File output = new File("/home/marco/desenv/html.png");
        ImageIO.write(rotatedImage, "png", output);

    }

    private BufferedImage rotate(BufferedImage img) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage newImage = new BufferedImage(w, h, img.getType());
        Graphics2D g2 = newImage.createGraphics();
        g2.rotate(Math.toRadians(90), w/2, h/2);
        g2.drawImage(img,null,0,0);
        return newImage;
    }
Eftychiou commented 2 years ago

Has anyone applied this and worked ?

anastaciocintra commented 2 years ago

hi @Eftychiou , can you share your specific problem? is this about rotation?

Eftychiou commented 2 years ago

yes i have this printer (https://www.xprintertech.com/pos-printer-xprinter) that supports rotation. Label printer compatible with TSPL\ EPL\ DPL\ ZPL emulation.

Your code (dantsu) looks perfect for usage and implementation therefore the code provided by dantsu does not implement rotation. How can we apply the rotation feature on the code provided by dantsu ?

Best Regards, George Eftichiou Cyprus [https://img5541.weyesimg.com/uploads/rsirawa8.allweyes.com/images/777ce2954082e50b6d739efff0c0ceff.jpg]https://www.xprintertech.com/pos-printer-xprinter XP-P200https://www.xprintertech.com/pos-printer-xprinter Want to know more about pos printer form Xprinter Group? Click in to learn more! www.xprintertech.com


From: Marco Antonio @.> Sent: Monday, April 18, 2022 2:21 PM To: anastaciocintra/escpos-coffee @.> Cc: Eftychiou @.>; Mention @.> Subject: Re: [anastaciocintra/escpos-coffee] Rotation/Orientation (#58)

hi @Eftychiouhttps://github.com/Eftychiou , can you share your specific problem? is this about rotation?

— Reply to this email directly, view it on GitHubhttps://github.com/anastaciocintra/escpos-coffee/issues/58#issuecomment-1101325080, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ARL4ZVPQGGR7TPJIVXGOJT3VFVASBANCNFSM4VGGKB2A. You are receiving this because you were mentioned.Message ID: @.***>

anastaciocintra commented 2 years ago

ok, point of attention: unfortunately, the escpos-coffee doesn't work with another pattern than esc/pos, and, I'm in doubt if your printer is compatible with escpos!

Label printer compatible with TSPL\ EPL\ DPL\ ZPL emulation.

if the answer is not, then I don't know if this solution fits for your printer.

the idea is not send escpos rotation commands to the printer, but, instead of this, create one image with graphics2d, then rotate image and then print rotated image...

If your printer is compatible with escpos commands, it can work! if you are using another library, espos-coffee might work too! I hope it works!

78a70d0e-f171-433a-b2e5-7e0520ca7d10

import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.EscPosConst;
import com.github.anastaciocintra.escpos.image.BitonalThreshold;
import com.github.anastaciocintra.escpos.image.CoffeeImageImpl;
import com.github.anastaciocintra.escpos.image.RasterBitImageWrapper;
import com.github.anastaciocintra.output.PrinterOutputStream;
import image.ImageHelper;

import java.awt.*;
import java.awt.image.BufferedImage;
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.PrintService;
import javax.swing.*;

public class Rotate {
    public void exec(EscPos escpos) throws IOException {

        String html = "" +
                "<html>\n" +
                "<head>\n" +
                "<style>\n" +
                "body {\n" +
                "  background-color: lightblue;\n" +
                "}\n" +
                "\n" +
                "h1 {\n" +
                "  text-align: center;\n" +
                "}\n" +
                "\n" +
                "p {\n" +
                "  font-family: verdana;\n" +
                "  font-size: 20px;\n" +
                "}\n" +
                "p.korean {\n" +
                "  font-family: Single Day;\n" +
                "  font-size: 20px;\n" +
                "}\n" +
                "</style>\n" +
                "</head>" +
                "<body>" +
                "<h1>Hello, world.</h1>" +
                "<p>الصفحة الرئيسية \n" + // Arabiac
                "<br>你好,世界 \n" + // Chinese
                "<br>こんにちは世界 \n" + // Japanese
                "<br>Привет мир \n" + // Russian
                "<br>नमस्ते दुनिया \n" + //  Hindi
                "<p class=\"korean\"><br>안녕하세요 세계</p>" + // if necessary, you can download and install on your environment the Single Day from fonts.google...
                "</body>"
                ;

        int width = 576, height = 576;
        // Create a `BufferedImage` and create the its `Graphics`
        BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration()
                .createCompatibleImage(width, height);
        Graphics graphics = image.createGraphics();
        // Create an `JEditorPane` and invoke `print(Graphics)`

//        JEditorPane jep = new JEditorPane("text/html", html);
        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");

        jep.setText(html);
        jep.setSize(width, height);
        jep.print(graphics);

        BufferedImage rotatedImage = rotate(image);
//        BufferedImage rotatedImage = image;
        // send the graphic to the escpos printer...
        new ImageHelper().write(escpos, new CoffeeImageImpl(rotatedImage),new RasterBitImageWrapper(),new BitonalThreshold());
        escpos.feed(5).cut(EscPos.CutMode.FULL);
        escpos.close();

        /* DEBUG, you can save the image */
        File output = new File("/home/marco/desenv/html.png");
        ImageIO.write(rotatedImage, "png", output);

    }

    private BufferedImage rotate(BufferedImage img) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage newImage = new BufferedImage(w, h, img.getType());
        Graphics2D g2 = newImage.createGraphics();
        g2.rotate(Math.toRadians(90), w/2, h/2);
        g2.drawImage(img,null,0,0);
        return newImage;
    }

    public static void main(String[] args) throws IOException {
        PrintService printService = PrinterOutputStream.getPrintServiceByName("TM-T20");
        PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
        EscPos escpos = new EscPos(printerOutputStream);
        Rotate obj = new Rotate();
        obj.exec(escpos);

    }

}
Eftychiou commented 2 years ago

Sorry for mentioning dantsu code in the previous email. Now i can see what you are solving with this library. As this could be my last solution of creating an image then rotating it. Do you have any sample code that shows this implementation ? Do you have an example case that this library was tested on android/gradle ?


From: Marco Antonio @.> Sent: Monday, April 18, 2022 7:35 PM To: anastaciocintra/escpos-coffee @.> Cc: Eftychiou @.>; Mention @.> Subject: Re: [anastaciocintra/escpos-coffee] Rotation/Orientation (#58)

ok, point of attention: unfortunately, the escpos-coffee doesn't work with another pattern than esc/pos, and, I'm in doubt if your printer is compatible with escpos!

Label printer compatible with TSPL\ EPL\ DPL\ ZPL emulation.

if the answer is not, then I don't know if this solution fits for your printer.

the idea is not send escpos rotation commands to the printer, but, instead of this, create one image with graphics2d, then rotate image and then print rotated image...

If your printer is compatible with escpos commands, it can work! if are you using another library then espos-coffee it can work too! I hope it works!

[78a70d0e-f171-433a-b2e5-7e0520ca7d10]https://user-images.githubusercontent.com/43155086/163838817-eeaba3cc-6d6b-47de-8bc9-9ab65a330f0e.jpeg

— Reply to this email directly, view it on GitHubhttps://github.com/anastaciocintra/escpos-coffee/issues/58#issuecomment-1101549643, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ARL4ZVNEWDDQ7PF5JMNVQ6LVFWFNXANCNFSM4VGGKB2A. You are receiving this because you were mentioned.Message ID: @.***>

anastaciocintra commented 2 years ago

Sorry for mentioning dantsu code in the previous email.

no problem, it is an excellent library

Do you have any sample code that shows this implementation ?

yeap, on last post at the final part.

Do you have an example case that this library was tested on android/gradle ?

sure, you can use android with escpos-coffee, you can see one sample at https://github.com/anastaciocintra/escpos-coffee-samples/tree/master/miscellaneous/AndroidImage

caveats about rotation solution: the solution above (suggested here in this issue) works fine at server java or desktop jdk. for android, (I don't test it yet), you need to translate Graphics to Canvas (android) and I think that it will work too!.

see you, Marco

Eftychiou commented 2 years ago

thank you very much and sorry for asking twice questions that you had answered earlier :p

anastaciocintra commented 2 years ago

count on me