adzchao
V2EX  ›  问与答

Java 连接本地惠普打印机,获取打印机状态

  •  
  •   adzchao · Dec 4, 2024 · 1120 views
    This topic created in 535 days ago, the information mentioned may be changed or developed.

    打印没有问题,想要获取本地打印机状态或者异常状态,但是一直是空的

     public static void checkPrinterStatus(PrintService printService) {
            try {
                // 获取打印机的状态原因属性
                boolean attributeCategorySupported = printService.isAttributeCategorySupported(PrinterState.class);
                System.out.println(attributeCategorySupported);
                PrinterState attribute = printService.getAttribute(PrinterState.class);
    
                PrinterStateReasons reasons = (PrinterStateReasons) printService.getAttribute(PrinterStateReasons.class);
    
                // 检查打印机是否缺纸
                if (reasons.containsKey(MEDIA_NEEDED) || reasons.containsKey(PrinterStateReason.MEDIA_LOW)|| reasons.containsKey(PrinterStateReason.MEDIA_EMPTY)) {
                    System.out.println("打印机缺纸!");
                }
    
                // 检查打印机是否缺墨
                if (reasons.containsKey(PrinterStateReason.TONER_LOW) || reasons.containsKey(PrinterStateReason.TONER_EMPTY)) {
                    System.out.println("打印机缺墨!");
                }
    
                // 其他状态检查...
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        
        
    

    谁有经验给看一下呗

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1040 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 23:35 · PVG 07:35 · LAX 16:35 · JFK 19:35
    ♥ Do have faith in what you're doing.