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?
java opencv bufferedimage mat
New contributor
add a comment |
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?
java opencv bufferedimage mat
New contributor
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
add a comment |
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?
java opencv bufferedimage mat
New contributor
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
java opencv bufferedimage mat
New contributor
New contributor
New contributor
asked 2 days ago
user2441988
11
11
New contributor
New contributor
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
add a comment |
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
add a comment |
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);
}
New contributor
add a comment |
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);
}
New contributor
add a comment |
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);
}
New contributor
add a comment |
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);
}
New contributor
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);
}
New contributor
New contributor
answered 2 days ago
user2441988
11
11
New contributor
New contributor
add a comment |
add a comment |
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.
user2441988 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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