Search code examples
javaopencvbufferedimagemat

Loading screenshot into Mat


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?


Solution

  • 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);
    
    }