【问题标题】:Printing raw data from java in Ubuntu在 Ubuntu 中从 java 打印原始数据
【发布时间】:2012-04-10 12:00:34
【问题描述】:

我打算使用 Eclipse 和一个简单的 swing gui 来打印原始数据。我的条码打印机使用.prn 文件进行打印,所有文件都被编码在文件中,所以如果我能设法将文件的确切内容作为原始数据发送到我的打印机,我可以让它打印出我想要的确切格式。我可以选择文件并使用StringBuffer 读取其内容并写入字符串。现在我怎样才能设法将此字符串作为原始数据发送到我的打印机?

问候。

编辑:

也许我应该详细说明一下我的问题;现在它可以工作了,在 Windows 中使用;

    int ch;

            FileInputStream fin = null;
            try {
                fin = new FileInputStream(prnfile);
                while ((ch = fin.read()) != -1)
                    strContent.append((char) ch);
                fin.close();
                JOptionPane.showMessageDialog(frame, strContent);
            } catch (Exception e1) {
                System.out.println(e1);
            }



            try {

                PrintService service = PrintServiceLookup.lookupDefaultPrintService();
                JOptionPane.showMessageDialog(frame, service);




                DocPrintJob job = service.createPrintJob();
                InputStream is = new ByteArrayInputStream(strContent.toString().getBytes());




                DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

                Doc doc = new SimpleDoc(is, flavor, null);
                job.print(doc, null);

            is.close();

但是当我尝试在 Ubuntu 中运行它时,我只收到“打印取消,java 打印”通知,而打印机什么也不做(通常我的打印机在 ubuntu 中使用终端上的cat xxx.prn | lpr 工作。有什么想法吗?

【问题讨论】:

  • 您可能不应该使用String,并且如何将其发送到您的打印机取决于打印机及其提供的接口类型。那么:它是一种什么样的打印机,它提供了哪些接口?到目前为止,您尝试过什么?
  • 我设法让它实际工作,但仅在 Windows 中,但当我在 Ubuntu 中运行它时,它只是说“打印取消”“java 打印”......
  • 打印机通常需要通过 CUPS 或 GUI 进行原始打印配置。 stackoverflow.com/a/38965500/3196753。一些 Windows 驱动程序会自动感知可能描述差异的数据类型(Epson 和 Zebra 会这样做)。

标签: java eclipse ubuntu printing ubuntu-11.10


【解决方案1】:

首先,按照注释将打印机创建为原始打印机。

其次,正如评论的那样,将您的文件读入字节数组,而不是 String 以避免将字节解码为 un​​icode。

第三,不要使用 defaultPrintService(),寻找一个特定的服务,指定我们将发送一个字节数组到打印机(原始数据)以确保目标打印机支持原始数据:

DocFlavor.byteFlavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintServiceAttributeSet aset = new HashPrintServiceAttributeSet();
aset.add(new PrinterName(sPrinterName, null)); 
PrintService[] services = PrintServiceLookup.lookupPrintServices(byteFlavor, aset);

services[0] 应该是能够发送您的原始数据的服务。这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多