如何在Java中使用收据打印机和ESC/POS命令提高速度

如何在Java中使用收据打印机和ESC/POS命令提高速度,java,byte,bytearray,thermal-printer,receipt,Java,Byte,Bytearray,Thermal Printer,Receipt,我有一个应用程序,它用Java和Java语言与热敏打印机通信 使热敏打印机使用Star tsp 100打印机打印带有条形码/强调/不同尺寸等的收据 我可以让程序精确地打印我喜欢的内容,但打印机速度很慢。我相信原因是我正在使用非首选的方式/方法发送字节命令 public static void Command(byte[] bytes) throws Exception{ //The bytes array is the argument, consisting of a byte

我有一个应用程序,它用Java和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);          
}
收据的打印方式完全符合我的要求,但这是一个非常缓慢的过程

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

当收据打印机到达收据的主体或“主体”时,所有东西都具有相同的大小/样式,然后它会快速移动,这使我相信,这对我来说速度变慢的原因是因为我正在创建/执行许多单独的“打印工作”

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


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

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

    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);
    }
public static void test()引发异常
{
ByteArrayOutputStream printData=新建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);
write(GenerateReceiptBody().getBytes(“CP437”);
printData.write(PrintClass.Halfsize);
printData.write(Constants.getFooter().getBytes(“CP437”);
printData.write(PrintClass.Cut);
printBytes(printData.toByteArray());
}
公共静态void printBytes(最终字节[]字节)引发异常
{
DocPrintJob job=PrintServiceLookup.lookupDefaultPrintService().createPrintJob();

系统输出打印项次(作业+"谢谢,这真是一场梦!我可以从反对票的数量上看出,这不是一个很受欢迎的问题。但是,哦,我还是很高兴我问了这个问题。^^。你能分享一下打印的代码吗,我是一个初学者,谢谢。我很久以前就升级了代码,现在已经没有代码了:P,但是你为什么过得很艰难呢让它工作?你升级到了什么?如果你真的能以这样或那样的方式提供帮助,那就太好了。史密斯试着把它作为一个问题发布出来,我会看一看。
    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);
    }