简体   繁体   English

为什么PrinterState总是为空?

[英]Why is PrinterState always null?

I need to have more control about my printer and then I'm trying to get PrinterState of my printer and then use PrintStareReasons. 我需要对我的打印机有更多的控制权,然后我试图获得打印机的PrinterState,然后使用PrintStareReasons。 My code is the following: 我的代码如下:

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 I also write getAttributes() to get all attribures but PrinterState is not present. 谷歌搜索我也写了getAttributes()来获取所有的归因,但PrinterState不存在。

This is the list of all attributes: 这是所有属性的列表:

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

While

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

return always: 总是回来:

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

I have tested the following code on Linux and on Windows (7), but none of them return the actual status. 我在Linux和Windows上测试了以下代码(7),但没有一个返回实际状态。 What could be the problem? 可能是什么问题呢? The printer, the Driver or my code? 打印机,驱动程序或我的代码?

isAttributeCategorySupported() returns true if the print service supports specifying a doc-level or job-level attribute in category in a Print Request otherwise returns false. 如果打印服务supports specifying a doc-level or job-level attribute in category在打印请求supports specifying a doc-level or job-level attribute in category中的supports specifying a doc-level or job-level attribute in categoryisAttributeCategorySupported()返回true ,否则返回false。

having a look at the official oracle documentation will make my point much clearer 看看官方的oracle文档会让我的观点更加清晰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM