我需要對我的打印機有更多的控制權,然後我試圖獲得我的打印機的PrinterState,然後使用PrintStareReasons。我的代碼如下:爲什麼PrinterState始終爲空?
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;
}
谷歌搜索我也寫的getAttributes()來獲取所有attribures但PrinterState不存在。
這是所有屬性的列表:
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
雖然
logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class));
回報總是:
21.12.2015 16:48:58 INFO PrintWorker:151 - State false
我已經測試了下面的代碼在Linux和Windows(7 ),但沒有一個返回實際狀態。可能是什麼問題呢?打印機,驅動程序還是我的代碼?
重複。檢查了這一點:http://stackoverflow.com/questions/26985422/why-printerstate-always-returns-null 我會說,不要打擾太多,以獲得狀態,它似乎沒有正確實施 – delephin
相關: http://stackoverflow.com/questions/5567709/extended-printer-information-in-java – Jayan