java - 使用机器人类截取屏幕截图 - 希望减少文件大小

标签 java image awtrobot

我有一小段代码,每五分钟对我的桌面进行一次屏幕截图 - 这是一个瞬间的工作,可以快速思考并计算出有多少屏幕截图,例如,facebook...这非常有用,但是充满屏幕截图的目录变得相当大。我正在寻找减少图像文件大小的方法 - 我不需要它们具有完全完美的屏幕截图质量 - 我希望能够降低图像的整体质量 - 也许使用更有损的格式或者要求机器人以灰度保存。

我正在询问如何修改以下代码,以便生成的图像占用更少的文件空间,并且我愿意容忍在此过程中相当高的质量损失。

/**
 * Code modified from code given in http://whileonefork.blogspot.co.uk/2011/02/java-multi-monitor-screenshots.html following a SE question at  
 * http://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen and then modified by a code review at http://codereview.stackexchange.com/questions/10783/java-screengrab
 */
package com.tmc.personal;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

class ScreenCapture {

    static int minsBetweenScreenshots = 5;

    public static void main(String args[]) {
        int indexOfPicture = 1000;// should be only used for naming file...
        while (true) {
            takeScreenshot("ScreenCapture" + indexOfPicture++);
            try {
                TimeUnit.MINUTES.sleep(minsBetweenScreenshots);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //from http://www.coderanch.com/t/409980/java/java/append-file-timestamp
    private  final static String getDateTime()
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("PST"));
        return df.format(new Date());
    }

    public static void takeScreenshot(String filename) {
        Rectangle allScreenBounds = getAllScreenBounds();
        Robot robot;
        try {
            robot = new Robot();
            BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);
            ImageIO.write(screenShot, "jpg", new File(filename + getDateTime()+ ".jpg"));
        } catch (AWTException e) {
            System.err.println("Something went wrong starting the robot");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Something went wrong writing files");
            e.printStackTrace();
        }
    }

    /**
     * Okay so all we have to do here is find the screen with the lowest x, the
     * screen with the lowest y, the screen with the higtest value of X+ width
     * and the screen with the highest value of Y+height
     * 
     * @return A rectangle that covers the all screens that might be nearby...
     */
    private static Rectangle getAllScreenBounds() {
        Rectangle allScreenBounds = new Rectangle();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();

        int farx = 0;
        int fary = 0;
        for (GraphicsDevice screen : screens) {
            Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
            // finding the one corner
            if (allScreenBounds.x > screenBounds.x) {
                allScreenBounds.x = screenBounds.x;
            }
            if (allScreenBounds.y > screenBounds.y) {
                allScreenBounds.y = screenBounds.y;
            }
            // finding the other corner
            if (farx < (screenBounds.x + screenBounds.width)) {
                farx = screenBounds.x + screenBounds.width;
            }
            if (fary < (screenBounds.y + screenBounds.height)) {
                fary = screenBounds.y + screenBounds.height;
            }
            allScreenBounds.width = farx - allScreenBounds.x;
            allScreenBounds.height = fary - allScreenBounds.y;
        }
        return allScreenBounds;
    }
}

最佳答案

为什么不简单地缩放您收到的图像:

  BufferedImage img = robot.createScreenCapture(allScreenBounds);

  // scaledWidth and scaledHeight are the new smaller image size
  Image scaledImg = img.getScaledInstance(scaledWidth, scaledHeight,
        BufferedImage.SCALE_AREA_AVERAGING);

如果您需要新图像为 BufferedImage,则:

  BufferedImage scaledBufferedImg = new BufferedImage(scaledWidth, scaledHeight,
        BufferedImage.TYPE_INT_ARGB);
  Graphics g = scaledBufferedImg.getGraphics();
  g.drawImage(scaledImg, 0, 0, null);
  g.dispose();

关于java - 使用机器人类截取屏幕截图 - 希望减少文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18171734/

相关文章:

java - 创建自定义迭代器 Java?

java - 客户端调用 JAX-WS 项目类中的方法时丢失

java - 为什么 BufferedImage 不能正常工作?!!是因为我用错了吗?

java - 在 Java 中跟踪操作系统级别的窗口事件

java - robots.getColorPixel() 返回错误的颜色?

java - 如何从java异常中提取列号

java - Java中的求和方程?

Java(机器人)屏幕截图超出可见范围

image - 来自网络共享的 UWP XAML ImageBrush.imageSource

html - CSS border-radius 属性不起作用