Orv*_*röm 4 java byte bytearray thermal-printer receipt
我有一个与Java热敏打印机通信的应用程序,并使用Star tsp 100打印机使热敏打印机打印带有条形码/强调/不同尺寸等的收据.
我可以让程序打印出我喜欢的东西,但打印机很慢.我相信原因是我使用非优选的方式/方法发送字节命令.
public static void Command(byte[] bytes) throws Exception{
//The bytes array is the argument, consisting of a byte[] array such as
//byte[] Emphasis = {0x1B, 0x45}; //Which gives the text boldness.
//And Command(Emphasis); To execute the command with this method.
DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
}
Run Code Online (Sandbox Code Playgroud)
当我想打印一个字符串我使用这种方法.
public void PrintString(String s) throws Exception{
DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
System.out.println(job + " <- printer");
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
byte[] b = s.getBytes("CP437");
Doc doc = new SimpleDoc(b, flavor, null);
job.print(doc, null);
}
Run Code Online (Sandbox Code Playgroud)
因此,只要文本具有相同的样式,我就会使用此方法打印大量字符串(等等不需要其他命令).所以打印收据的代码看起来有点像这样.
PrintClass P = new PrintClass();
String Greating = "Hello";
P.Command(P.Emphasis);
P.Command(P.DoubleSize);
P.Command(P.Underline);
P.PrintString(Greating);
P.Command(P.CancelEmphasis);
P.Command(P.ResetSize);
P.Command(P.CancelUnderline);
String body = GenerateReceiptBody();
P.PrintString(body);
P.Command(P.Halfsize);
String footer = Constants.getFooter();
P.PrintString(footer);
P.Command(P.Cut);
Run Code Online (Sandbox Code Playgroud)
收据按照我想要的方式打印,但这是一个非常缓慢的过程.
在发送POS/ESC命令时,我绝不是专家.然而,我觉得必须有一个更好/更快的方法来做到这一点,因为许多应用程序可以打印不同大小/条形码/样式/徽标的收据,而不需要花费10-20秒.
当收据打印机到达收据的主要部分或"正文",其中所有东西都具有相同的尺寸/样式然后它很快就会进入,这让我相信这对我来说很慢的原因是因为我正在创建/执行如此多的个人"打印工作".
那么有没有更快的方式将ESC/POS命令作为字节命令发送到Thermal打印机?在这种情况下,热敏打印机是Star tsp 100,但我不认为这对答案有任何影响.
任何答案都将非常感激.如果这是一个简单的问题,我很抱歉,因为我仍然在学习如何编码.
小智 5
我不知道这是否会提高您的打印速度,但在回答有关减少"打印作业"数量的问题时,您可以先将所有字节写入流,然后将整个流发送到单个打印作业.我试图转换你的代码来做到这一点.
public static void test() throws Exception
{
ByteArrayOutputStream printData = new ByteArrayOutputStream();
printData.write(PrintClass.Emphasis);
printData.write(PrintClass.DoubleSize);
printData.write(PrintClass.Underline);
printData.write("Hello".getBytes("CP437"));
printData.write(PrintClass.CancelEmphasis);
printData.write(PrintClass.ResetSize);
printData.write(PrintClass.CancelUnderline);
printData.write(GenerateReceiptBody().getBytes("CP437"));
printData.write(PrintClass.Halfsize);
printData.write(Constants.getFooter().getBytes("CP437"));
printData.write(PrintClass.Cut);
printBytes(printData.toByteArray());
}
public static void printBytes(final byte[] bytes) throws Exception
{
DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
System.out.println(job + " <- printer");
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2041 次 |
| 最近记录: |