Robot()。createScreenCapture使用打印对话框捕获图像

时间:2013-06-10 16:14:50

标签: java

我希望我的应用程序截取屏幕截图并显示打印对话框,以便用户可以打印屏幕截图。但是,屏幕截图拍摄速度太慢/对话框显示得太快,导致对话框出现在屏幕截图中(如下所示)。

enter image description here

BufferedImage image = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight()));

...

if (printJob.printDialog()) {
    try {
        printJob.print();
    } catch (Exception PrintException) {
        PrintException.printStackTrace();
    }
}

有没有办法在显示打印对话框之前指示屏幕截图何时完成捕获?


编辑:

好吧,我想我弄清楚我做错了什么。真正发生的是在显示打印对话框并且用户单击“确定”之后捕获屏幕,但在打印对话框动画之前。这是由于以下代码:

// Print button action listener
public class printButtonPressed implements ActionListener {
    public void actionPerformed(ActionEvent arg0) {

        PrinterJob printJob = PrinterJob.getPrinterJob ();
        Book book = new Book ();
        PageFormat documentPageFormat = new PageFormat ();
        documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
        book.append(new Page(), documentPageFormat);
        printJob.setPageable(book);

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (Exception PrintException) {
                PrintException.printStackTrace();
            }
        }
    }
}

// Create printable page
private class Page implements Printable {
    public int print (Graphics g, PageFormat pageFormat, int page) {

        JFrame frame = Main.frame;
        try { // THIS SHOULD NOT BE HERE
            screenShot = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight());
        } catch (AWTException e) {
            e.printStackTrace();
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());

        int pageWidth = (int) pageFormat.getImageableWidth();
        int newImageWidth = pageWidth;
        int newImageHeight = (int)((double)newImageWidth/screenShot.getWidth(null)*screenShot.getHeight(null));

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.drawImage(screenShot, 0, 0, newImageWidth, newImageHeight, 0, 0, screenShot.getWidth(), screenShot.getHeight(), null);

        return PAGE_EXISTS;
    }
}

相反,如果在任何PrinterJob之前调用Robot()。createScreenCapture,则会在打印对话框出现之前捕获屏幕,从而解决此问题。

// Print button action listener
public class printButtonPressed implements ActionListener {
    public void actionPerformed(ActionEvent arg0) {

        JFrame frame = Main.frame;
        try { // MOVE THIS UP HERE
            screenShot = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight());
        } catch (AWTException e) {
            e.printStackTrace();
        }

        PrinterJob printJob = PrinterJob.getPrinterJob ();
        Book book = new Book ();
        PageFormat documentPageFormat = new PageFormat ();
        documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
        book.append(new Page(), documentPageFormat);
        printJob.setPageable(book);

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (Exception PrintException) {
                PrintException.printStackTrace();
            }
        }
    }
}

// Create printable page
private class Page implements Printable {
    public int print (Graphics g, PageFormat pageFormat, int page) {

        Graphics2D g2d = (Graphics2D) g;
        g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());

        int pageWidth = (int) pageFormat.getImageableWidth();
        int newImageWidth = pageWidth;
        int newImageHeight = (int)((double)newImageWidth/screenShot.getWidth(null)*screenShot.getHeight(null));

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.drawImage(screenShot, 0, 0, newImageWidth, newImageHeight, 0, 0, screenShot.getWidth(), screenShot.getHeight(), null);

        return PAGE_EXISTS;
    }
}

1 个答案:

答案 0 :(得分:0)

将其转储到一个数组然后解析整个数组然后你可以保证一切都在那里

我的意思是这个

BufferedImage TestShot = iris.createScreenCapture(new Rectangle(0,0,width,height));
int[] ImgData = null;
TestShot.getRGB(0, 0, width, height, ImgData, 0, 0);
for(int i:ImgData){
    // count or something
}
// all of it is there in TestShot

那应该有用。我不知道为什么你有这个问题,我一直使用机器人,我的截图功能立即工作。也许你的printJob需要更多的时间。