I am using a Robot to capture a screenshot. In order to avoid unnecessary I/O of writing the BufferedImage on disk and then loading it back up into a Mat I am trying to load the BufferedImage directly into a Mat with the following code.
public static Mat screenShot() throws AWTException, IOException {
Robot r = new Robot();
Rectangle capture = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = r.createScreenCapture(capture);
Mat mat = new Mat(Image.getHeight(), Image.getWidth(), CvType.CV_8UC1);
byte[] data = ((DataBufferByte) Image.getRaster().getDataBuffer()).getData();
mat.put(0, 0, data);
return mat;
}
I am getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
How might I go about circumventing this issue?