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?
Đã 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
http://stackoverflow.com/questions/5567709/extended-printer-information-in-java – Jayan