繁体   English   中英

打印连续的BufferedImage

[英]Printing a continuous BufferedImage

我正在尝试将连续的缓冲图像打印到单独的页面上。 我的问题是,我能做的最好的就是打印最后一页。 直到变量inty的计数似乎一直被忽略。 这是我的代码

public class PrintDocs implements Printable {
    BufferedImage incomingImage;
    int inty;

    public PrintDocs(BufferedImage incomingImage) {
        this.incomingImage = incomingImage;
        inty = 0;

    }

    public void PrintImage() {
        HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
        attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth() / 72f, incomingImage.getHeight() / 72f,
                MediaPrintableArea.INCH));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        PageFormat pf = job.pageDialog(job.defaultPage());
        boolean ok = job.printDialog();

        if (ok) {

            try {
                job.print(attr);
                inty = inty + 842;

            } catch (PrinterException ex) {
                /* The job did not successfully complete */
            }

        }

    }

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        // if (page > 0){ //Use this for the moment may get rid of it
        // return NO_SUCH_PAGE;
        // }

        /*
         * User (0,0 is typacally outside the imageable area, so we must
         * translate by the X and Y values in the PageFormatz to avoid clipping
         */

        if (inty < incomingImage.getHeight()) {
            // if (inty < incomingImage.getHeight()){
            Graphics2D g2d = (Graphics2D) g;

            g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0);

            /* Now we perform out rendering */

            g.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595, 842 + inty, null);

            // g2d.drawImage(incomingImage, 0, 0, 595, 842, 0, inty, 595,842 +
            // inty, null);
            System.out.println("inty = " + inty);
            inty = inty + 842;
            return PAGE_EXISTS;
        }
        return NO_SUCH_PAGE;
    }
}

调用printImage():

private void printActionPerformed(java.awt.event.ActionEvent evt){ 
    // TODO add your handling code here:
    //BufferedImage printCopy = tooOutput.displayProcessing.getCreatedImage();
    BufferedImage printCopy = tooOutput.getPrintImage();
    PrintDocs sendToPrinter = new PrintDocs(printCopy);
    sendToPrinter.PrintImage();
}

您的变量inty似乎重置为,因为每次调用actionPerformed方法时总是创建一个PrintDocs的新实例。

PrintDocs sendToPrinter = new PrintDocs(printCopy); //This line causing the reset of inty
sendToPrinter.PrintImage();

为避免这种情况,您可以保留对同一PrintDocs实例的引用:

private void printActionPerformed(java.awt.event.ActionEvent evt){ 
    BufferedImage printCopy = tooOutput.getPrintImage();
    sendToPrinter.PrintImage();  //Keep a reference of sendToPrinter in your class
}

另一种解决方案将是使intY一个staic变量和printImage()的静态方法。 当需要print ,只需通过类名调用:

PrintDos.printImage();

您的课程将如下所示:

public class PrintDocs implements Printable {
    static int inty = 0;    //<= become static 

    //other variables, methods and constructor not shown

    public static void PrintImage() {    //<= become static
        //implementation for printImage
    }
}

问题是每次内部调用print方法时inty都会增加。 如果每个页面仅调用一次print方法,则该方法有效。

java.awt.print.Printable的JavaDoc( https://docs.oracle.com/javase/7/docs/api/java/awt/print/Printable.html ):

打印系统可能会多次请求页面索引。 在每种情况下,都将提供相等的PageFormat参数。

您的打印方法应该使用page参数来确定要打印图像的哪一部分,而不是计算当前的页码本身。

public class PrintDocs implements Printable {
    private static final int pageHeight = 842;
    private BufferedImage incomingImage;
    private int pagePrintCount = 0;

    public PrintDocs(BufferedImage incomingImage) {
        this.incomingImage = incomingImage;
    }

    public void PrintImage() {
        HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
        attr.add(new MediaPrintableArea(0f, 0f, incomingImage.getWidth() / 72f, incomingImage.getHeight() / 72f,
                MediaPrintableArea.INCH));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        PageFormat pf = job.pageDialog(job.defaultPage());
        boolean ok = job.printDialog();

        if (ok) {

            try {
                job.print(attr);
            } catch (PrinterException ex) {
                System.out.println("The job did not successfully complete");
                ex.printStackTrace();
            }

        }

    }

    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        System.out.println("print called for page="+page+", call count="+pagePrintCount++);

        /*
         * User (0,0 is typacally outside the imageable area, so we must
         * translate by the X and Y values in the PageFormatz to avoid clipping
         */

        if (page*pageHeight < incomingImage.getHeight()) {
            Graphics2D g2d = (Graphics2D) g;

            g2d.translate(pf.getImageableX() - 1.0, pf.getImageableY() - 1.0);

            /* Now we perform out rendering */

            g.drawImage(incomingImage, 0, 0, 595, pageHeight, 0, page*pageHeight, 595, pageHeight*(page+1), null);

            return PAGE_EXISTS;
        }
        return NO_SUCH_PAGE;
    }
}

暂无
暂无

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

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