V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
adzchao
V2EX  ›  问与答

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

  •  
  •   adzchao · 7 天前 · 301 次点击

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

     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();
            }
        }
        
        
        
    

    谁有经验给看一下呗

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2493 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 00:29 · PVG 08:29 · LAX 16:29 · JFK 19:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.