Loading screenshot into Mat












0














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?










share|improve this question






















  • check this
    – Redanium
    Nov 10 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 at 19:36
















0














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?










share|improve this question






















  • check this
    – Redanium
    Nov 10 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 at 19:36














0












0








0







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?










share|improve this question













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?







java opencv bufferedimage mat






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:04









user2441988

32




32












  • check this
    – Redanium
    Nov 10 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 at 19:36


















  • check this
    – Redanium
    Nov 10 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 at 19:36
















check this
– Redanium
Nov 10 at 14:33




check this
– Redanium
Nov 10 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 at 19:36




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 at 19:36












1 Answer
1






active

oldest

votes


















0














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

}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239237%2floading-screenshot-into-mat%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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

    }





    share|improve this answer


























      0














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

      }





      share|improve this answer
























        0












        0








        0






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

        }





        share|improve this answer












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

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 20:05









        user2441988

        32




        32






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239237%2floading-screenshot-into-mat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check