为什么 PrinterState 总是空的?
Why is PrinterState always null?
我需要对我的打印机进行更多控制,然后我尝试获取打印机的 PrinterState,然后使用 PrintStareReasons。我的代码如下:
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() 来获取所有属性,但 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));
return 总是:
21.12.2015 16:48:58 INFO PrintWorker:151 - State false
我已经在 Linux 和 Windows (7) 上测试了以下代码,但其中 none return 是实际状态。可能是什么问题呢?打印机、驱动程序或我的代码?
isAttributeCategorySupported()
returns true
如果打印请求中的打印服务 supports specifying a doc-level or job-level attribute in category
否则 returns false.
看看 the official oracle documentation 会让我的观点更清楚
我需要对我的打印机进行更多控制,然后我尝试获取打印机的 PrinterState,然后使用 PrintStareReasons。我的代码如下:
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() 来获取所有属性,但 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));
return 总是:
21.12.2015 16:48:58 INFO PrintWorker:151 - State false
我已经在 Linux 和 Windows (7) 上测试了以下代码,但其中 none return 是实际状态。可能是什么问题呢?打印机、驱动程序或我的代码?
isAttributeCategorySupported()
returns true
如果打印请求中的打印服务 supports specifying a doc-level or job-level attribute in category
否则 returns false.
看看 the official oracle documentation 会让我的观点更清楚