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

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

  •  
  •   adzchao · 21 天前 · 424 次点击

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

     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   ·   实用小工具   ·   1123 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 18ms · UTC 18:53 · PVG 02:53 · LAX 10:53 · JFK 13:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.