简体   繁体   English

我可以在Raspberry PI上从Java打印到USB打印机吗?

[英]Can I print to USB printer from Java on a Raspberry PI?

I am looking at creating a simple time clock. 我正在寻找创建一个简单的时钟。 It will read barcoded id badges from an HID USB scanner. 它将从HID USB扫描仪读取带条形码的身份证徽章。 Most of it is simple to implement. 大多数都易于实现。 Where I am stuck is when I consider writing the application in C# .net and using an old PC, or writing it for the Raspberry PI in Java. 当我考虑使用C#.net编写应用程序并使用旧PC或使用Java为Raspberry PI编写应用程序时,我就陷入了困境。 (I do not have any Java experience, but I am taking a semester class next month.) (我没有任何Java经验,但是下个月我要上一学期课程。)

I would prefer the Raspberry PI as the host for a number of reasons. 由于多种原因,我希望使用Raspberry PI作为主机。 The sticking point is, where on a Windows PC using .net there are printing facilities, I have had very little luck searching out the method where I could print the time sheets to a USB printer from the Raspberry PI using Java. 关键是,在使用.net的Windows PC上有打印功能的地方,我很少能找到可以使用Java从Raspberry PI将时间表打印到USB打印机的方法。 (CUPS would be installed on a Raspbien OS.) (CUPS将安装在Raspbien操作系统上。)

Now my question to the learned masses ... Have you successful printed from a Java application to a USB printer connected to a Raspberry PI? 现在,我向广大学界提出了一个问题……您是否已经成功地从Java应用程序打印到连接至Raspberry PI的USB打印机? Do I study and use the same code on the Raspberry PI as I would use to print from Java on my Windows desktop, or does it need some other magic? 我是否在Raspberry PI上学习并使用与在Windows桌面上从Java打印时使用的相同代码,还是需要其他魔术?

I have found through other sources that with CUPS properly installed, printing from Java is fully functional using the same methods as on a Windows platform; 通过其他来源,我发现正确安装了CUPS的情况下,使用与Windows平台上相同的方法,可以从Java进行打印。 java.awt.print and javax.print. java.awt.print和javax.print。

That means I can use the Raspberry Pi for my barcode time clock project as soon as I learn enough Java. 这意味着一旦我学习了足够的Java,就可以将Raspberry Pi用于我的条形码时钟项目。

KK KK

Sometimes, the JRE won't find the printer on a raspberry pi. 有时,JRE在树莓派上找不到打印机。 Go to terminal on your raspberry pi with the printer connected and type lpstat -a and copy the printer's name that you are looking for. 在已连接打印机的情况下转到树莓派上的终端,然后键入lpstat -a并复制您要查找的打印机名称。 Then use this in your program: 然后在程序中使用它:

String destination = "Your printer's name";
String path = "Your path to pdf";
String[] command = { "lp", "-d", destination, path};
Process proc = new ProcessBuilder(command).start();

The path to the pdf is relative to your current directory. pdf的路径是相对于您当前目录的。

This program executes a command on your linux system to print your desired pdf through ProcessBuilder. 该程序在Linux系统上执行命令,以通过ProcessBuilder打印所需的pdf。 It will only run on a linux system (raspberry pi). 它只能在linux系统(raspberry pi)上运行。

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

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