在更大的应用程序环境中,我的applet需要将一些数据打印到Zebra或Dymo(取决于用户安装的内容)标签打印机。
我收到的数据是转发形式的数据,我只需要发送给打印机并让它解释它。
搜索我找到了两个解决方案。 方法1:
byte[] printdata;
PrintService pservice = PrintServiceLookup.lookupDefaultPrintService(); //or get the printer in some other way
DocPrintJob job = pservice.createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(printdata, flavor, null);
和方法2:
PrintStream printStream = new PrintStream(new FileOutputStream(“LPT1”));
printStream.print(“Hello World”);
printStream.close();
我需要这个跨平台工作,打印机使用USB或串口。 实现此行为的正确方法是什么?
方法2的一个问题是我需要以相同的方式找到打印机的URL ...
答案 0 :(得分:3)
public String rawprint(String printerName, String conte) {
String res = "";
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
PrintService printServices[] = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
if (printServices.length != 1) {
return "Can't select printer :" + printerName;
}
byte[] printdata = conte.getBytes();
PrintService pservice = printServices[0];
DocPrintJob job = pservice.createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(printdata, flavor, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
try {
job.print(doc, aset);
} catch(Exception e){
res = e.getMessage();
}
return res;
}
在javafx中运行很酷
答案 1 :(得分:0)
Hex打印输出值得信赖。调用String.getBytes(encoding)
,然后使用System.out.format
将每个字节打印为十六进制数字。