gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:18:52 25 4
gpt4 key购买 nike

我在裁纸时遇到问题。我有 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();
}
}

最佳答案

问题在于,只有字符串被发送到打印机,而不是关于打印状态的信息。因此,类和方法完全改变了。

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();
}
}

}

关于java - 爱普生打印完成后如何裁纸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com