【问题标题】:How to improve speed with Receipt printer and ESC/POS commands in Java如何使用 Java 中的收据打印机和 ESC/POS 命令提高速度
【发布时间】:2015-04-21 23:31:59
【问题描述】:

我有一个使用 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);
    }

当我想打印一个字符串时,我使用这个方法。

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

所以我使用这种方法打印大部分字符串,只要文本具有相同的样式(等等,不需要额外的命令)。所以我打印收据的代码看起来有点像这样。

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

收据完全按照我想要的方式打印,但这是一个非常缓慢的过程。

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

当收据打印机到达收据的主要部分或“主体”时,所有东西都具有相同的尺寸/样式,然后它会很快运行,这让我相信这对我来说变慢的原因是因为我正在创建/执行这么多单独的“打印作业”。

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

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

【问题讨论】:

  • 能否分享一下打印代码,我是初学者,谢谢
  • 我已经没有代码了,因为我很久以前就升级了代码:P 但是你为什么很难让它工作呢?
  • 你升级到了什么?如果你真的能以一种或另一种方式提供帮助,那就太好了
  • 好吧,史密斯尝试一下并将其作为问题发布,我会看看它。

标签: java byte bytearray thermal-printer receipt


【解决方案1】:

我不知道这是否会提高您的打印速度,但在回答您关于减少“打印作业”数量的问题时,您可以先将所有字节写入流,然后将整个流发送到单个打印作业。我已尝试将您的代码转换为执行此操作。

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

【讨论】:

  • 非常感谢,这就像做梦一样!我可以从反对票的数量上看出这不是一个非常受欢迎的问题。但是哦,我仍然很高兴我问了它^^。
猜你喜欢
  • 1970-01-01
  • 2020-05-07
  • 2017-10-21
  • 2016-08-25
  • 2017-05-06
  • 2018-03-18
  • 2013-06-18
  • 1970-01-01
  • 2016-12-24
相关资源
最近更新 更多