Loading screenshot into Mat











up vote
0
down vote

favorite












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







New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • check this
    – Redanium
    2 days ago










  • 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
    2 days ago















up vote
0
down vote

favorite












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







New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • check this
    – Redanium
    2 days ago










  • 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
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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







New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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







New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









user2441988

11




11




New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • check this
    – Redanium
    2 days ago










  • 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
    2 days ago


















  • check this
    – Redanium
    2 days ago










  • 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
    2 days ago
















check this
– Redanium
2 days ago




check this
– Redanium
2 days ago












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
2 days ago




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
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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








New contributor




user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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',
    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
    });


    }
    });






    user2441988 is a new contributor. Be nice, and check out our Code of Conduct.










     

    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
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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








    New contributor




    user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      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








      New contributor




      user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        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








        New contributor




        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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








        New contributor




        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        user2441988

        11




        11




        New contributor




        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        user2441988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            user2441988 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            user2441988 is a new contributor. Be nice, and check out our Code of Conduct.













            user2441988 is a new contributor. Be nice, and check out our Code of Conduct.












            user2441988 is a new contributor. Be nice, and check out our Code of Conduct.















             


            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




















































































            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python