我能够获得之前在我的PC上安装的打印机的名称。但现在它没有物理连接到我的电脑。在使用Java转移到Print()之前,我应该如何检查它。
答案 0 :(得分:3)
如果您的打印机支持PrinterState
属性,则可以使用该属性。
这样的事情:
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");
}
答案 1 :(得分:1)
看看javax.print API。一个很好的起点是PrintServiceLookup。
答案 2 :(得分:0)
Set中没有PrinterState属性。但是你可以加载库Winspool.drv并询问它的属性。有int属性@ 68 =,其中有40个在线值,e40值用于离线打印机。
从那里开始 - https://msdn.microsoft.com/cs-cz/library/windows/desktop/dd144911(v=vs.85).aspx。
使用这些类然后获取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依赖项:
<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>
答案 3 :(得分:0)
另一种方法是使用PowerShell和查询:
Get-WmiObject -Query "Select * From Win32_PnPEntity where deviceid like 'USBPRINT\\%' and caption like '%Canon%'"
这样,只有连接打印机才能获得结果。
您可以使用许多库从Java查询WMI,搜索&#34; WMI Java库&#34;。