爱普生打印完成后如何裁纸?

How to cut paper after printing is done on Epson?

裁纸有问题。我有 Epson TM-T20 收据,打印完成后尝试裁纸。我在某处发现这是用于切割 byte[] bytes = { 27, 100, 3 } 的代码,但它不起作用。

下面是我用于打印的代码。

public static void printer(String printerData, Boolean bill) throws IOException {
    try {
        PrintService[] printServices = PrinterJob.lookupPrintServices();

        String testData = printerData + "\r";
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = { 27, 100, 3 }; //Code for cutting
        outputStream.write(testData.getBytes(Charset.forName("UTF-8")));
        outputStream.write(bytes);
        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        System.out.println(service);

        DocPrintJob job = service.createPrintJob();

        Doc doc = new SimpleDoc(is, flavor, null);

        PrintJobWatcher pjDone = new PrintJobWatcher(job);

        job.print(doc, null);

        pjDone.waitForDone();

        is.close();
    } catch (PrintException e) {
        Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter writer = new PrintWriter(bw);
        e.printStackTrace(writer);
        writer.close();
    } catch (IOException e) {
        Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter writer = new PrintWriter(bw);
        e.printStackTrace(writer);
        writer.close();
    }
}

问题在于仅将字符串发送到打印机,而不是有关打印状态的信息。所以,class和方法完全改变了。

public class Printing implements Printable {

    private String stringToPrint;

    public Printing(String stringToPrint) {
        this.stringToPrint = stringToPrint;
    }

    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        if (pageIndex >= 1) {
            return Printable.NO_SUCH_PAGE;
        }
        g.setColor(Color.black);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
        g.translate(0, 0);
        int x = 0;
        int y = 100;
        //
        for (String line : stringToPrint.split("\n")) {
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
        }

        return Printable.PAGE_EXISTS;
    }
    public static void printer(String printerData, String designatedPrinter)
        throws IOException, PrinterException {
        try {

            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            PrintService designatedService = null;
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
            AttributeSet aset = new HashAttributeSet();
            aset = new HashAttributeSet();
            aset.add(ColorSupported.NOT_SUPPORTED);
            String printers = "";
            for (int i = 0; i < printServices.length; i++) {
                printers += " service found " + printServices[i].getName() + "\n";
            }
            for (int i = 0; i < printServices.length; i++) {
                System.out.println(" service found " + printServices[i].getName());
                if (printServices[i].getName().equalsIgnoreCase(designatedPrinter)) {
                    System.out.println("I want this one: " + printServices[i].getName());
                    designatedService = printServices[i];
                    break;
                }
            }
            Writer fw = new OutputStreamWriter(new FileOutputStream("printing.txt"), StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter writer = new PrintWriter(bw);
            writer.print(printers);
            writer.close();
            PrinterJob pj = PrinterJob.getPrinterJob();
            pj.setPrintService(designatedService);
            Printable painter;

            // Specify the painter
            painter = new Printing(printerData);
            pj.setPrintable(painter);
            pj.print();

        } catch (PrinterException e) {
            Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter writer = new PrintWriter(bw);
            e.printStackTrace(writer);
            writer.close();
        }
    }

   }
PrintWriter oStream = new PrintWriter(sock.getOutputStream());

关闭流之前..

oStream.print(""+(char)29+(char)86+(char)0);