I am attempting to take continuous screenshots and stream them over a socket to my android phone. I used ImageIO for this like this:
while(true){
baos = new ByteArrayOutputStream();
ImageIO.write(screenshot, "png", baos);
byte[] imageArray = baos.toByteArray();
oos.writeInt(imageArray.length);
oos.write(imageArray);
oos.flush();
imageArray = null;
}
This worked fine, however, there was huge lag time in the pictures showing up on the phone, and I figured it was because of the ImageIO. so I looked around in stackoverflow for a solution to this, and found this method and tried it:
while(true){
//take screenshot of the screen periodically and send to the server
screenshot = robot.createScreenCapture(rectangle);
byte[] imageArray = ((DataBufferByte)screenshot.getRaster().getDataBuffer()).getData();
oos.writeInt(imageArray.length);
oos.write(imageArray);
oos.flush();
imageArray = null;
}
But this keeps giving me this exception:
Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferInt
cannot be cast to java.awt.image.DataBufferByte
Can someone please help?
.
stackoverflow.comm