My program takes a picture of the users screen, when they are using my program, to make a screenshot, then sends it to a server. The image will load about 1/4 of the way and freeze.
Sending the screenshot:
BufferedImage buffimg = robot.createScreenCapture(captureSize);
BufferedImage image = buffimg;
byte[] imageInByte;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println("!SCREENDATA!");
out.flush();
dos.writeInt(baos.toByteArray().length);
dos.write(baos.toByteArray());
dos.flush();
Getting the image:
if (input.startsWith("!SCREENDATA!")) {
System.out.println("reading");
DataInputStream dis = new DataInputStream(socket.getInputStream());
int len = dis.readInt();
System.out.println(len);
byte[] data = new byte[len];
dis.read(data);
InputStream in = new ByteArrayInputStream(data);
Image image = Toolkit.getDefaultToolkit().createImage(data);
v.repaint(image);
}
Displaying the image:
public void repaint(Image img) {
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.repaint();
frame.pack();
}
If anyone could help me with this, I would appreciate it a lot!