增加java截图的分辨率

时间:2014-11-08 18:40:36

标签: java screenshot

我使用以下代码在Java中截取屏幕截图:

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws AWTException, IOException{
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File("Path/To/screenshot.png"));
    }
}

此代码有效,但输出的屏幕截图分辨率相当低。 如何提高屏幕截图的分辨率?

谢谢。如果你投票,请告诉我为什么我可以改进这个问题。

3 个答案:

答案 0 :(得分:1)

此代码没有任何问题。我的屏幕分辨率为1680x1050像素,生成的png文件正好具有此大小。

答案 1 :(得分:0)

使用Robot.createMultiResolutionScreenCapture,它是added in Java 9+

例如(使用Kotlin):

    val graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().defaultScreenDevice
    val screenWidth = graphicsDevice.displayMode.width
    val screenHeight = graphicsDevice.displayMode.height

    val robot = Robot(graphicsDevice)
    val rect = Rectangle(Toolkit.getDefaultToolkit().screenSize)

    val multiResolutionScreenshot = robot.createMultiResolutionScreenCapture(rect)
    val screenshot = multiResolutionScreenshot.getResolutionVariant(
            screenWidth.toDouble(), screenHeight.toDouble())

    ImageIO.write(screenshot as BufferedImage, "png", File("Path/To/screenshot.png"))

答案 2 :(得分:0)

也许你在新机器人之前需要这个

System.setProperty("sun.java2d.uiScale", "1");

当你设置桌面系统缩放时,java ui 也会缩放。
Desktop scale set