There is a Robot class in the Java standard API, which can implement screenshots and simulate mouse and keyboard operations. Only a screenshot of it is shown here.
The key method for taking screenshots is createScreenCapture(Rectangle rect). This method requires a Rectangle object. Rectangle is a rectangular area that defines the screen. It is also quite easy to construct a Rectangle:
new Rectangle(int x, int y, int width, int height), the four parameters are the x coordinate of the upper left corner of the rectangle, the y coordinate of the upper left corner of the rectangle, the width of the rectangle, and the height of the rectangle. The screenshot method returns a BufferedImage object, sample code:
/** * Specify a screenshot of the screen area and return the BufferedImage object of the screenshot * @param x * @param y * @param width * @param height * @return */ public BufferedImage getScreenShot(int x, int y, int width, int height ) { BufferedImage bfImage = null; try { Robot robot = new Robot(); bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height)); } catch (AWTException e) { e.printStackTrace(); } return bfImage; }
If you need to save the screenshot as a file, use ImageIO.write(RenderedImage im, String formatName, File output), sample code:
/** * Take a screenshot of the specified screen area and save it to the specified directory * @param x * @param y * @param width * @param height * @param savePath - File saving path * @param fileName - File saving name * @param format - File format*/ public void screenShotAsFile(int x, int y, int width, int height, String savePath, String fileName, String format) { try { Robot robot = new Robot(); BufferedImage bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height)); File path = new File(savePath); File file = new File(path, fileName+ "." + format); ImageIO.write(bfImage , format, file); } catch (AWTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
After capturing the screenshot, maybe, we need to crop it. It mainly involves two classes, CropImageFilter and FilteredImageSource. For an introduction to these two classes, see the java documentation.
/** *BufferedImage picture cropping* @param srcBfImg - the clipped BufferedImage * @param x - the X coordinate of the upper left corner of the clipping point * @param y - the Y coordinate of the upper left corner of the clipping point * @param width - the width of the cropped image* @param height - the height of the cropped image* @return the cropped BufferedImage */ public BufferedImage cutBufferedImage(BufferedImage srcBfImg, int x, int y, int width, int height) { BufferedImage cutedImage = null; CropImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcBfImg.getSource(), cropFilter)); cutedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = cutedImage.getGraphics(); g.drawImage(img, 0, 0, null); g. dispose(); return cutedImage; }
If you need to save the trimmed file after trimming, use ImageIO.write and refer to the code above to save the screenshot as a file.