Results 1 to 4 of 4
  1. #1
    Incisus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Unhappy BufferedImage Distortion Over Socket?

    Hello, I'm writing a remote administration tool for fun, anyhow I've added a "screen share" function, which allows me to view the machines screen real-time. The image is taken with the Robot class, loaded into a buffered image, then sent over an OutputStream via ImageIO.

    It works for a bit, then the server (Receiving the images) starts displaying things such as the image attached.

    Any idea's what's going wrong?BufferedImage Distortion Over Socket?-dla.png

    Client's send image code:
    Java Code:
    					robot = new Robot();
    					BufferedImage image = null;
    					Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    					Socket objSocket = new Socket(Connection.host, 2705);
    					OutputStream objOut = objSocket.getOutputStream();
    					while (enabled)
    					{
    						image = robot.createScreenCapture(screenRect);
    						try
    						{
    							if (image != null)
    							{
    								BufferedImage scalredImage = Scalr.resize(image, Scalr.Mode.FIT_EXACT, 800, 600,  Scalr.OP_ANTIALIAS);
    								ImageIO.write(scalredImage, "PNG", objOut);
    							}
    						} catch (Exception e) {}
    					}
    					objOut.close();
    *Please note, this issue occured even without using the Apache library to scale the image.

    Server receiving code:
    Java Code:
    					startTime = System.currentTimeMillis();
    					try {
    						objServerSocket = new ServerSocket(2705);
    	        			  client.sendData("screen_share:enabled");
    						objSocket = objServerSocket.accept();
    						objInput = objSocket.getInputStream();
    					} catch (IOException e1) {e1.printStackTrace();
    					}
    					while (enabled)
    					{
    						BufferedImage tmpImage = ImageIO.read(objInput);
    						if (tmpImage != null)
    						{
    							image = tmpImage;
    							ScreenShareUI.redraw();
    						}
    					}
    					objInput.close();
    JPanel paint method to display image:
    Java Code:
    		panel = new JPanel()
    		{
    			public void paintComponent(Graphics graphics)
    			{
    				super.paintComponents(graphics);
    				if (ScreenShareModule.getImage() != null)
    				{
    					graphics.drawImage(ScreenShareModule.getImage(), 0, 0, this);
    				}
    				else
    				{
    					graphics.setColor(Color.black);
    					graphics.fillRect(0,0,width,height);
    				}
    			}
    		};
    Thanks for reading, I look forward to a response!
    Last edited by Incisus; 12-10-2012 at 05:11 PM.

  2. #2
    Incisus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: BufferedImage Distortion Over Socket?

    Bump, desperate here.

  3. #3
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: BufferedImage Distortion Over Socket?

    I wouldn't trust using ImageIO to read directly from a stream. How about writing the image to a byte array first, then sending the size of that array, and the array over the stream so that you know exacctly what to expect when you're reading from the server side. Like so (I just modified your code a little):

    Java Code:
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import javax.imageio.ImageIO;
    
    
    public class Client {
    
    	public static void main(String[] args) throws UnknownHostException, IOException, AWTException {
    		Robot robot = new Robot();
    		BufferedImage image = null;
    		Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    		Socket objSocket = new Socket("127.0.0.1", 2705);
    		OutputStream objOut = objSocket.getOutputStream();
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		while (true)
    		{
    		    image = robot.createScreenCapture(screenRect);
    		    try
    		    {
    		        if (image != null)
    		        {
    		            ImageIO.write(image, "JPEG", out);
    		        }
    		        objOut.write(new byte[] {
    		        	(byte) (out.size() >> 24), (byte) (out.size() >> 16), (byte) (out.size() >> 8), (byte) out.size()
    		        });
    		        objOut.write(out.toByteArray());
    		        out.reset();
    		        objOut.flush();
    		    } catch (Exception e) {
    		    	e.printStackTrace();
    		    }
    		}
    	}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Server {
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Test");
    		frame.setSize(800, 600);
    		frame.add(panel = new JPanel()
    		{
    			public void paintComponent(Graphics graphics)
    			{
    				super.paintComponents(graphics);
    				if (image != null)
    				{
    					graphics.drawImage(image, 0, 0, this);
    				}
    				else
    				{
    					graphics.setColor(Color.black);
    					graphics.fillRect(0,0,800,600);
    				}
    			}
    		});
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		try {
    			ServerSocket objServerSocket = new ServerSocket(2705);
    			Socket objSocket = objServerSocket.accept();
    			InputStream objInput = objSocket.getInputStream();
    			while (true)
    			{
    				int size = (objInput.read() & 0xFF) << 24 | (objInput.read() & 0xFF) << 16 | (objInput.read() & 0xFF) << 8 | (objInput.read() & 0xFF);
    				byte[] data = new byte[size];
    				int total = 0;
    				while (total != size) {
    					total += objInput.read(data, total, size - total);
    				}
    				BufferedImage tmpImage = ImageIO.read(new ByteArrayInputStream(data));
    				if (tmpImage != null)
    				{
    					image = tmpImage;
    					panel.repaint();
    				}
    			}
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    	}
    
    	public static BufferedImage image;
    	public static JPanel panel;
    
    }
    Last edited by Potato; 12-14-2012 at 05:47 PM.

  4. #4
    Incisus is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: BufferedImage Distortion Over Socket?

    I cannot believe I didn't think of a Bytestream, I was trying to simplify it too much, caused my own problem.

    Thanks for your assistance!

Similar Threads

  1. Bizarre screen distortion
    By morello in forum New To Java
    Replies: 1
    Last Post: 12-21-2011, 04:28 PM
  2. Socket Programming ERROR : Socket not connected
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-04-2011, 10:32 AM
  3. Sending BufferedImage through socket
    By Desh Banks in forum Networking
    Replies: 9
    Last Post: 01-30-2011, 06:05 PM
  4. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  5. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •