简体   繁体   English

如何用Java中的Receipt打印机和ESC / POS命令提高速度

[英]How to improve speed with Receipt printer and ESC/POS commands in Java

I have an application that communicates with a thermal printer in Java and makes the thermal printer print receipts with a barcode/emphasis/different sizes and so forth using a Star tsp 100 Printer. 我有一个与Java热敏打印机通信的应用程序,并使用Star tsp 100打印机使热敏打印机打印带有条形码/强调/不同尺寸等的收据。

I can make the program print exaclty what i like but the printer is very slow. 我可以让程序打印出我喜欢的东西,但打印机很慢。 I believe the reason is that I am using non-preferable way/method of sending the byte commands. 我相信原因是我使用非优选的方式/方法发送字节命令。

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

And when i want to print a String i use this method. 当我想打印一个字符串我使用这种方法。

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

So i use this method to print the bulk of the String for as long as the text has the same style(Etc no additional commands necessary). 因此,只要文本具有相同的样式,我就会使用此方法打印大量字符串(等等不需要其他命令)。 So my code that prints the receipts looks somewhat like this. 所以打印收据的代码看起来有点像这样。

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

The receipt gets printed exactly the way i want but it is a very sluggish process. 收据按照我想要的方式打印,但这是一个非常缓慢的过程。

I am by no means an expert when it comes to sending POS/ESC commands. 在发送POS / ESC命令时,我绝不是专家。 I feel however that there must be a better/faster way to do this since many applications can print a receipt with different size/barcode/style/logo without it taking 10-20 seconds. 然而,我觉得必须有一个更好/更快的方法来做到这一点,因为许多应用程序可以打印不同大小/条形码/样式/徽标的收据,而不需要花费10-20秒。

When the receipt printer comes to a the main bulk or "body" of the receipt where everything has the same size/styling then it goes quickly, this makes me believe that the reason this is going slow for me is because i am making am creating/executing so many individual "print jobs". 当收据打印机到达收据的主要部分或“正文”,其中所有东西都具有相同的尺寸/样式然后它很快就会进入,这让我相信这对我来说很慢的原因是因为我正在创建/执行如此多的个人“打印工作”。

So is there any faster way to send ESC/POS commands to a Thermal printer as byte commands ? 那么有没有更快的方式将ESC / POS命令作为字节命令发送到Thermal打印机? In this case the thermal printer is a Star tsp 100 but i don't believe that it makes any difference for the answer. 在这种情况下,热敏打印机是Star tsp 100,但我不认为这对答案有任何影响。

Any answers would be very appreciated. 任何答案都将非常感激。 I am sorry if this was an easy question as I am still learning how to code. 如果这是一个简单的问题,我很抱歉,因为我仍然在学习如何编码。

I have no idea if this will improve your printing speed, but in answer to your question about reducing the number of "print jobs", you could write all the bytes to a stream first, then send the whole stream to a single print job. 我不知道这是否会提高您的打印速度,但是在回答有关减少“打印作业”数量的问题时,您可以先将所有字节写入流,然后将整个流发送到单个打印作业。 I've attempted to convert your code to do this. 我试图转换你的代码来做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM