All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aimmac23.node.RobotScreenshotSource Maven / Gradle / Ivy

The newest version!
package com.aimmac23.node;

import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;

import com.aimmac23.node.jna.EncoderInterface;
import com.aimmac23.node.jna.JnaLibraryLoader;
import com.sun.jna.Pointer;

public class RobotScreenshotSource implements ScreenshotSource {

	private Robot robot;

	public RobotScreenshotSource() throws AWTException, SecurityException {
		robot = new Robot();
	}
	
	@Override
	public int applyScreenshot(Pointer encoderContext) {
		BufferedImage image = takeScreenshot();

		int[] screenshotData = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
		
		EncoderInterface encoder = JnaLibraryLoader.getEncoder();

		return encoder.convert_frame(encoderContext, screenshotData);
	}

	@Override
	public int getWidth() {
		return getScreenSize().width;
	}

	@Override
	public int getHeight() {
		return getScreenSize().height;
	}

	protected Rectangle getScreenSize() {
		//XXX: This probably won't work with multiple monitors
		DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
		return new Rectangle(displayMode.getWidth(), displayMode.getHeight());
		
	}
	
	public BufferedImage takeScreenshot(){
		return robot.createScreenCapture(getScreenSize());
	}

	@Override
	public void doStartupSanityChecks() {
		// test to make assert that the bit depth has 8 bits per pixel
		
		ColorModel colorModel = GraphicsEnvironment.getLocalGraphicsEnvironment().
		getDefaultScreenDevice().getDefaultConfiguration().getColorModel();
		
		int[] bitAllocations = colorModel.getComponentSize();
		
		// don't count alpha bits
		int bitDepth = bitAllocations[0] + bitAllocations[1] + bitAllocations[2];
		
		if(bitDepth != 24) {
			throw new IllegalStateException("Display colour depth incorrect (should be 8 bits of red, blue and green). Currently: " 
					+ bitAllocations[0] + "-" + bitAllocations[1] + "-" + bitAllocations[2]);
		}
	}

	@Override
	public String getSourceName() {
		return "Java AWT Robot";
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy