2011-04-06 107 views
7

我试图获取有关系统上打印机的一些信息。
在Windows和Linux,使用此代码,只有PrinterName属性填充:Java中的扩展打印机信息

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null,null); 
for(PrintService printService : printServices) { 
    log.info("Found print service: "+printService); 
    log.info(printService.getAttribute(PrinterName.class)); 
    log.info(printService.getAttribute(PrinterLocation.class)); 
    log.info(printService.getAttribute(PrinterMakeAndModel.class)); 
    log.info(printService.getAttribute(PrinterMessageFromOperator.class)); 
    log.info(printService.getAttribute(PrinterMoreInfo.class)); 
    log.info(printService.getAttribute(PrinterMoreInfoManufacturer.class)); 
    log.info(printService.getAttribute(PrinterState.class)); 
    log.info(printService.getAttribute(PrinterStateReasons.class)); 
    log.info(printService.getAttribute(PrinterURI.class)); 
} 

使用就可以了toArray()功能后...

log.info("Found print service: "+printService); 
for(Attribute a : printService.getAttributes().toArray()) { 
    log.info("* "+a.getName()+": "+a); 
} 

...这就是结果:

Found print service: Win32 Printer : Brother MFC-9420CN BR-Script3 
* color-supported: supported 
* printer-name: Brother MFC-9420CN BR-Script3 
* printer-is-accepting-jobs: accepting-jobs 
* queued-job-count: 0

我如何获得更多的信息,像打印机ç omment?

+0

如果您从'PrintService.getAttributes()'迭代返回,您会看到什么? – 2011-04-06 19:16:12

+0

你不能遍历它。另外,我查看了Java Source并开始认为Java开发人员讨厌打印机。起初,他们构建了一个胖胖的API,然后他们自己也只是在Linux和Windows上用4个(!)〜20个可能的属性填充属性集。对不起,但这只是伤心。 – Daniel 2011-04-06 21:22:00

+2

当然你可以 - 只使用超类型的'toArray'('AttributeSet')并迭代数组。 – 2011-04-07 00:17:03

回答

3

还有其他PrintServiceAttribute实现,但如果你想获取更多...

这是一个唯一的代码,你也可以为文档味取不支持的值:

PrintService[] printServices = 
     PrintServiceLookup.lookupPrintServices(null, null); //get printers 

for (PrintService printService : printServices) { 
    System.out.println("Found print service: " + printService); 

    Set<Attribute> attribSet = new LinkedHashSet<Attribute>(); 

    Class<? extends Attribute>[] supportedAttributeCategories = (Class<? extends Attribute>[]) printService.getSupportedAttributeCategories(); 

    for (Class<? extends Attribute> category : supportedAttributeCategories) { 
     DocFlavor[] flavors = printService.getSupportedDocFlavors(); 
     for (DocFlavor flavor : flavors) { 
      Object supportedAttributeValues = printService.getSupportedAttributeValues(category, flavor, printService.getAttributes()); 
      if (supportedAttributeValues instanceof Attribute) { 
       Attribute attr = (Attribute) supportedAttributeValues; 
       attribSet.add(attr); 
      } else if (supportedAttributeValues != null) { 
       Attribute[] attrs = (Attribute[]) supportedAttributeValues; 
       for (Attribute attr : attrs) { 
        attribSet.add(attr); 
       } 
      } 
     } 
    } 

    for (Attribute attr : attribSet) { 
     System.out.println(attr.getName()); 

     System.out.println(printService.getDefaultAttributeValue(attr.getCategory())); 
    } 
} 

注意:您可能会看到重复的值,但可以对其进行过滤。

+0

+1 Okey,它检索有关打印机的更多信息,但我需要为每台打印机获取一些唯一的信息字符串。我想,必须在我的应用程序内分配它。 – Daniel 2011-04-19 15:45:01

+0

@Daniel - 查看我的答案。我的代码只是比默认的'getAttributes()'返回一个更加扩展的'Attributes'列表。这可能是你需要的。 – 2011-04-19 16:05:35

+0

我想要PrintService本身的属性,而不是打印机上作业的可能设置。我需要这些信息来存储打印机特定的配置设置,这些设置可以理解为我的应用 – Daniel 2011-04-19 16:36:27

3

下面是hGx's answer提供的代码的模块化,更易于理解的版本:

public static Set<Attribute> getAttributes(PrintService printer) { 
    Set<Attribute> set = new LinkedHashSet<Attribute>(); 

    //get the supported docflavors, categories and attributes 
    Class<? extends Attribute>[] categories = (Class<? extends Attribute>[]) printer.getSupportedAttributeCategories(); 
    DocFlavor[] flavors = printer.getSupportedDocFlavors(); 
    AttributeSet attributes = printer.getAttributes(); 

    //get all the avaliable attributes 
    for (Class<? extends Attribute> category : categories) { 
     for (DocFlavor flavor : flavors) { 
      //get the value 
      Object value = printer.getSupportedAttributeValues(category, flavor, attributes); 

      //check if it's something 
      if (value != null) { 
       //if it's a SINGLE attribute... 
       if (value instanceof Attribute) 
        set.add((Attribute) value); //...then add it 

       //if it's a SET of attributes... 
       else if (value instanceof Attribute[]) 
        set.addAll(Arrays.asList((Attribute[]) value)); //...then add its childs 
      } 
     } 
    } 

    return set; 
} 

这将返回发现指定的打印机的Attributes一个Set
注意:可能会出现重复的值。但是,它们可以被过滤。