0 0

Java怎么實現打印機監控15

實現目標:
1、可以測試打印機是否連接
2、監控打印機的狀態:打印機缺紙、打印機卡紙
我從PrintService里獲取PrinterStateReasons,可是總是為空。不知道怎么才能獲取到打印機的PrinterStateReasons
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.PrinterStateReasons;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;

import com.insigma.afc.common.LogHelper;

public class TextPrinter implements Printable {
    private static final LogHelper s_log = new LogHelper(TextPrinter.class);

    // 半角下的中文字符
    public static final short HALF_CHINESE = 0;

    // 半角下的英文字符
    public static final short HALF_ENGLISH = 1;

    protected String text;

    protected String seperator;

    public TextPrinter() {

    }

    public void print(String text, String seperator) {

        this.text = text;
        this.seperator = seperator;

        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

        aset.add(new MediaPrintableArea(20, 20, 700, 700, MediaPrintableArea.MM));

        PrintService services = PrintServiceLookup.lookupDefaultPrintService();

        if (services != null) {
            DocPrintJob pj = services.createPrintJob();
            services.addPrintServiceAttributeListener(new PrintServiceAttributeListener() {

                public void attributeUpdate(PrintServiceAttributeEvent psae) {
                    PrintServiceAttributeSet attribute = psae.getAttributes();
                }

            });
            pj.addPrintJobListener(new PrintJobListener() {

                public void printDataTransferCompleted(PrintJobEvent pje) {
                    s_log.info("111111111111  " + pje.getPrintEventType());
                }

                public void printJobCanceled(PrintJobEvent pje) {
                    s_log.info("22222222222222222  " + pje.getPrintEventType());
                }

                public void printJobCompleted(PrintJobEvent pje) {
                    s_log.info("33333333333333  " + pje.getPrintEventType());
                }

                public void printJobFailed(PrintJobEvent pje) {
                    s_log.info("444444444444444  " + pje.getPrintEventType());

                }

                public void printJobNoMoreEvents(PrintJobEvent pje) {
                    s_log.info("5555555555555555555  " + pje.getPrintEventType());
                    System.out.println("打印失敗:" + pje.JOB_FAILED);
                    System.out.println("沒有事件:" + pje.NO_MORE_EVENTS);
                }

                public void printJobRequiresAttention(PrintJobEvent pje) {
                    s_log.info("66666666666666666666  " + pje.getPrintEventType());
                }

            });
            try {
                Doc doc = new SimpleDoc(this, flavor, null);
                pj.print(doc, aset);
                PrinterStateReasons printerStateReasons = services
                        .getAttribute(PrinterStateReasons.class);
                System.out.println("-----------" + printerStateReasons);
            } catch (PrintException e) {
                e.printStackTrace();
                System.err.println(e);
            }
        } else {
            s_log.info("未找到打印機。");
        }
    }

    public int print(Graphics g, PageFormat pf, int pageIndex) {

        if (pageIndex == 0) {

            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            g2d.setColor(Color.black);

            String[] lines = text.split(seperator);

            for (int i = 0; i < lines.length; i++) {
                g2d.drawString(lines[i], 0, (i + 1) * 20);
            }

            return Printable.PAGE_EXISTS;
        } else {
            return Printable.NO_SUCH_PAGE;
        }
    }

    /**
     * 為中文字符對齊補空格(打印對齊時候用)
     * 
     * @param content
     *            需要補空格的字符
     * @param expectLength
     *            占用的最大長度
     * @param type
     *            字符的類型:中文、英文
     * @return
     */
    public static String validChineseString(String content, int expectLength, short type) {
        String resultString = content.trim();
        String space = null;
        switch (type) {
        case HALF_CHINESE: {
            space = " ";
            break;
        }
        case HALF_ENGLISH: {
            space = " ";
            break;
        }

        default:
            break;
        }
        int contentLength = content.length();
        if (contentLength < expectLength) {
            int different = expectLength - contentLength;
            for (int i = 0; i < different; i++) {
                resultString += space;
            }
        }
        System.out.println(content + "    " + resultString);
        return resultString;
    }
}

2009年10月10日 10:41

2個答案 按時間排序 按投票排序

0 0

第三方包讀取com

2009年10月26日 16:44
0 0

打印機監控系統的設計與實現
http://download.csdn.net/source/855257

2009年10月10日 15:02
Global site tag (gtag.js) - Google Analytics 开心农场种蔬菜赚钱 陕西11选5规则 任2 上海11选5直播开奖 北京赛车开奖软件 广西11选五今天走势囹 北京快中彩开奖查询 十大最安全的理财abb 加拿大28每天开奖数据 拉伯配资 陕西福彩快乐十分遗漏查询 重庆幸运农场今天开奖号 陕西11选5最大遗漏 快乐十分钟开奖走势图福彩快乐十分钟开奖 产业基金配资比例 广西快三一定牛形态走势图 幸运飞艇官方网站开奖号码 安徽25选5预测