1

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?

  • check [this](https://stackoverflow.com/questions/15839223/java-awt-image-databufferbyte-cannot-be-cast-to-java-awt-image-databufferint?answertab=active#tab-top) – Redanium Nov 10 '18 at 14:33
  • It doesn't really help me as I can't change the type of my BufferedImage since it gets initialized by the return of my robot's screencapture. – user2441988 Nov 10 '18 at 19:36

1 Answers1

1

I found a workaround on this thread, ultraviolet's response deals with the issue.

Working 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 = BufferedImage2Mat(Image);

    return mat;

}

public static Mat BufferedImage2Mat(BufferedImage image) throws IOException {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", byteArrayOutputStream);
    byteArrayOutputStream.flush();
    return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

}