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

Rotation/Orientation #58

Open
proxseas opened this issue Dec 22, 2020 · 11 comments
Open

Rotation/Orientation #58

proxseas opened this issue Dec 22, 2020 · 11 comments
Labels
enhancement New feature or request

Comments

@proxseas
Copy link

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.

@proxseas proxseas added the enhancement New feature or request label Dec 22, 2020
@anastaciocintra
Copy link
Owner

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

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

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

Has anyone applied this and worked ?

@anastaciocintra
Copy link
Owner

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

@Eftychiou
Copy link

Eftychiou commented Apr 18, 2022 via email

@anastaciocintra
Copy link
Owner

anastaciocintra commented Apr 18, 2022

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

Eftychiou commented Apr 18, 2022 via email

@anastaciocintra
Copy link
Owner

anastaciocintra commented Apr 18, 2022

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

Eftychiou commented Apr 18, 2022

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

@anastaciocintra
Copy link
Owner

count on me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants