[英]How to check Printer is connected or not to your PC by Java?
I am able to get the name of printer which I had installed previously on my PC.But now its not physically connected to my pc. 我可以得到以前在PC上安装的打印机的名称。但是现在它没有物理连接到PC。 How should I check it first before moving to Print() in Java.
在移到Java中的Print()之前,我应该如何首先检查它。
You can use PrinterState
attribute if it is supported by your printer. 如果打印机支持,则可以使用
PrinterState
属性。
Something like this: 像这样:
PrintServiceAttributeSet printServiceAttributes = selectedService.getAttributes();
PrinterState printerState = (PrinterState) printServiceAttributes.get(PrinterState.class);
if (printerState != null){
System.out.println(printerName + " is online");
}
else {
System.out.println(printerName + " is offline");
}
Take a look at the javax.print API. 看一看javax.print API。 A good starting point would be PrintServiceLookup .
一个很好的起点是PrintServiceLookup 。
There is no PrinterState Attribute in Set. 集合中没有PrinterState属性。 But you can load library Winspool.drv and ask it for attributes.
但是您可以加载库Winspool.drv并询问其属性。 There is int Attributes@68=, which has a40 value for online and e40 value for offline printer.
有int Attributes @ 68 =,对于联机打印机,其值为40,对于脱机打印机,其值为e40。
Start there - https://msdn.microsoft.com/cs-cz/library/windows/desktop/dd144911(v=vs.85).aspx . 从此处开始-https: //msdn.microsoft.com/cs-cz/library/windows/desktop/dd144911(v=vs.85).aspx 。
Use these classes and then get WinspoolUtilExt.getPrinterInfo2(ps.getName()).toString() and there is an attribute. 使用这些类,然后获取WinspoolUtilExt.getPrinterInfo2(ps.getName())。toString()并有一个属性。
public interface WinspoolExt extends Winspool {
WinspoolExt INSTANCE = (WinspoolExt) Native.loadLibrary("Winspool.drv", WinspoolExt.class, W32APIOptions.UNICODE_OPTIONS);
boolean GetPrinter(HANDLE hPrinter, int Level, Pointer pPrinter, int cbBuf, IntByReference pcbNeeded);
boolean OpenPrinter(String pPrinterName, HANDLEByReference phPrinter, Pointer pDefault);
public static class PRINTER_INFO_2 extends Structure {
public String pServerName;
public String pPrinterName;
public String pShareName;
public String pPortName;
public String pDriverName;
public String pComment;
public String pLocation;
public INT_PTR pDevMode;
public String pSepFile;
public String pPrintProcessor;
public String pDatatype;
public String pParameters;
public INT_PTR pSecurityDescriptor;
public int Attributes;
public int Priority;
public int DefaultPriority;
public int StartTime;
public int UntilTime;
public int Status;
public int cJobs;
public int AveragePPM;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "pServerName", "pPrinterName", "pShareName", "pPortName", "pDriverName", "pComment", "pLocation", "pDevMode", "pSepFile", "pPrintProcessor", "pDatatype", "pParameters", "pSecurityDescriptor", "Attributes", "Priority", "DefaultPriority", "StartTime", "UntilTime", "Status", "cJobs", "AveragePPM" });
}
public PRINTER_INFO_2() {
}
public PRINTER_INFO_2(int size) {
super(new Memory(size));
}
}
}
public class WinspoolUtilExt extends WinspoolUtil {
public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
HANDLEByReference pHandle = new HANDLEByReference();
WinspoolExt.INSTANCE.OpenPrinter(printerName, pHandle, (Pointer) null);
WinspoolExt.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_2();
}
PRINTER_INFO_2 pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());
WinspoolExt.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned);
pinfo2.read();
return (PRINTER_INFO_2) pinfo2;
}
}
Maven dependencies: Maven依赖项:
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>${jna.version}</version>
</dependency>
Another way is to use PowerShell and query: 另一种方法是使用PowerShell和查询:
Get-WmiObject -Query "Select * From Win32_PnPEntity where deviceid like 'USBPRINT\\%' and caption like '%Canon%'"
This way, you get result only if printer is connected. 这样,只有在连接打印机后,您才能获得结果。
You can query WMI from Java with many libraries, search "WMI Java library". 您可以使用许多库从Java查询WMI,然后搜索“ WMI Java库”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.