如何使用Java中的收据打印机和ESC/POS命令提高速度 [英] How to improve speed with Receipt printer and ESC/POS commands in Java

查看:241
本文介绍了如何使用Java中的收据打印机和ESC/POS命令提高速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Java热敏打印机通信的应用程序, 使用Star tsp 100打印机使热敏打印机打印带有条形码/强调/不同尺寸等的收据.

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.

我可以使程序完全打印我喜欢的东西,但是打印机速度很慢.我相信原因是我使用了不理想的方式/方法来发送字节命令.

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

因此,只要文本具有相同样式,我就使用此方法来打印String的大部分内容(等等,无需其他命令).所以我的打印收据的代码看起来像这样.

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.

在发送POS/ESC命令方面,我绝不是专家.但是我觉得必须有一种更好/更快的方法,因为许多应用程序可以打印具有不同大小/条形码/样式/徽标的收据,而无需花费10-20秒的时间.

I am by no means an expert when it comes to sending POS/ESC commands. 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.

当收据打印机到达收据的大块或主体"时,凡是大小相同或样式相同的东西,然后它很快就会运走,这使我相信这对我来说变慢的原因是因为我正在创建/执行许多个人打印作业".

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".

那么,有没有一种更快的方法可以将ESC/POS命令作为字节命令发送到热敏打印机?在这种情况下,热敏打印机为Star Tsp 100,但我认为这对答案没有任何影响.

So is there any faster way to send ESC/POS commands to a Thermal printer as byte commands ? In this case the thermal printer is a Star tsp 100 but i don't believe that it makes any difference for the answer.

任何答案将不胜感激.如果这是一个简单的问题,很抱歉,因为我仍在学习如何编码.

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

这篇关于如何使用Java中的收据打印机和ESC/POS命令提高速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆