2009-09-07 88 views
0

我能夠得到我以前在我的PC上安裝過的打印機的名稱,但現在它的物理上並沒有連接到我的電腦上。在Java中移動到Print()之前,我應該如何檢查它。如何檢查打印機是否通過Java連接到您的PC?

+0

如果它是好的,該解決方案是針對Windows的,看到http://stackoverflow.com/a/41808295/1082681。 – kriegaex 2017-01-23 14:13:04

回答

2

看看javax.print API。一個好的起點是PrintServiceLookup

+0

從什麼時候起,SO upvote「閱讀文檔」的評論? – th3byrdm4n 2017-09-26 20:35:54

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"); 
     } 
+2

在過去的幾天裏,我一直在測試很多連接到操作系統的打印機和網絡打印機,並且似乎無法檢查打印機的狀態。上面的代碼片段什麼都不做。 (如在;所有「離線」。)(我是Windows 7與Java 7) – Philipp 2013-12-03 12:03:32

+0

你知道爲什麼PrinterState總是空? – whizzzkey 2014-11-18 01:25:45

+0

沒有爲我們工作,謝謝。 – th3byrdm4n 2017-09-26 20:36:11

0

中有沒有設定PrinterState屬性。但是你可以加載庫Winspool.drv並詢問它的屬性。存在int Attributes @ 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> 
0

另一種方法是使用PowerShell和查詢:

Get-WmiObject -Query "Select * From Win32_PnPEntity where deviceid like 'USBPRINT\\%' and caption like '%Canon%'" 

這樣,你得到的只是,如果打印機結果連接的。

可以從Java查詢WMI有許多圖書館,搜索 「WMI Java庫」。

相關問題