如何捕获类似于alt plus打印屏幕的图像?

时间:2011-11-10 18:02:57

标签: java swing

尝试捕获图像质量和尺寸类似于ALT + printscreen。有人可以帮我一下吗?

我尝试了以下代码来捕获:

dimScreenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
rectScreenSize = new Rectangle (dimScreenSize);
BufferedImage bufimg=robot.createScreenCapture (rectScreenSize);

捕获我用于将其存储在特定位置的图像后,我再次获取图像并使用thumbnails调整其大小。

loc =d_path+"\\a"+m+"a"+".jpg"; 
Thumbnails.of(new File(loc)).size(800, 800).outputQuality(1f).toFile(new File(loc));
但是图像质量太差了。请帮帮我,还有其他的吗?

2 个答案:

答案 0 :(得分:1)

如果图像质量不理想,您可以尝试其他格式,例如png?

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Capture
{
    public static void main(String[] args) throws Exception
    {
        Robot robot = new Robot();

        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "png", new File("screenShot.png"));
    }
}

答案 1 :(得分:1)

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

    public class AltPrintScreen {

        public static void main(String[] args) throws IOException, UnsupportedFlavorException, AWTException {

            Robot robot = new Robot();

            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_PRINTSCREEN);
            robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
            robot.keyRelease(KeyEvent.VK_ALT);

            try {
                Thread.sleep(1000 * 2);
            } catch (InterruptedException e) {
                throw new RuntimeException( e );
            }

            Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
            RenderedImage image = (RenderedImage)t.getTransferData(DataFlavor.imageFlavor);

            boolean isSuccess = ImageIO.write(image, "png", new File("altScreen.png"));

            System.out.println(isSuccess);
        }
    }