2015-12-21 15 views
6

Tôi cần có quyền kiểm soát nhiều hơn đối với máy in của mình và sau đó tôi đang cố gắng lấy PrinterState của máy in và sau đó sử dụng PrintStareReasons. Mã của tôi là như sau:Tại sao PrinterState luôn rỗng?

public void checkPrinterStatus(){ 

    try { 
     logger.info("Check -------------- "); 

     Thread.sleep(2000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    PrintService printer = configParamPrintService.getPrintService(); 
    logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class)); 
    Set<Attribute> attributes = getAttributes(printer); 
    for(Attribute attr : attributes){ 
     logger.info(attr.getName()); 
    } 



} 

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

Googling Tôi cũng viết getAttributes() để nhận tất cả các lưu ý nhưng PrinterState không có mặt.

Đây là danh sách của tất cả các thuộc tính:

21.12.2015 16:48:56 INFO PrintWorker:142 - Check -------------- 
21.12.2015 16:48:58 INFO PrintWorker:151 - State false 
21.12.2015 16:48:58 INFO PrintWorker:154 - copies-supported 
21.12.2015 16:48:58 INFO PrintWorker:154 - finishings 
21.12.2015 16:48:58 INFO PrintWorker:154 - job-sheets 
21.12.2015 16:48:58 INFO PrintWorker:154 - job-sheets 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - number-up 
21.12.2015 16:48:58 INFO PrintWorker:154 - orientation-requested 
21.12.2015 16:48:58 INFO PrintWorker:154 - orientation-requested 
21.12.2015 16:48:58 INFO PrintWorker:154 - orientation-requested 
21.12.2015 16:48:58 INFO PrintWorker:154 - orientation-requested 
21.12.2015 16:48:58 INFO PrintWorker:154 - page-ranges 
21.12.2015 16:48:58 INFO PrintWorker:154 - media 
21.12.2015 16:48:58 INFO PrintWorker:154 - spool-data-destination 

Trong khi

logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class)); 

trở lại luôn:

21.12.2015 16:48:58 INFO PrintWorker:151 - State false 

Tôi đã thử nghiệm đoạn mã sau trên Linux và Windows (7), nhưng không ai trong số họ trả lại trạng thái thực tế. Điều gì có thể là vấn đề? Máy in, Driver hoặc mã của tôi?

+1

Đã sao chép. Kiểm tra này: http://stackoverflow.com/questions/26985422/why-printerstate-always-returns-null Tôi muốn nói đừng bận tâm quá nhiều để có được trạng thái, có vẻ như nó không được thực hiện đúng cách – delephin

+0

http://stackoverflow.com/questions/5567709/extended-printer-information-in-java – Jayan

Trả lời

6

isAttributeCategorySupported() trả về true nếu dịch vụ in supports specifying a doc-level or job-level attribute in category trong yêu cầu in nếu không trả về false.

có một cái nhìn tại the official oracle documentation sẽ làm cho quan điểm của tôi rõ ràng hơn

+0

Sau đó, bạn nói với tôi rằng dịch vụ in của tôi không hỗ trợ loại thuộc tính này? Và sau đó tôi sẽ không bao giờ có được những thuộc tính này? – Skizzo

Các vấn đề liên quan