Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: sending images client/server sockets

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sending images client/server sockets

    I have a camera that monitors conditions outside via a webserver, I can't use a webservice so I currently use JBoss to send the images through queue's and topics. I feel JBoss is overkill for what I need and want to use a server/client socket. Here is my current code; I only get one image to work.

    Server code:
    package server;
     
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URL;
     
    import javax.imageio.ImageIO;
    import common.ByteArrayConversion;
     
    public class Image_Server {
     
    	private static final int port = 6666;
    	private static final String ONESHOTNAME = "oneshotimage.jpg";
    	private static byte[] byteImage;
     
    	/**
    	 * @param args
    	 * @param ONESHOTNAME 
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
     
    		ServerSocket server = null;
     
    		try {
    			server = new ServerSocket(port);
    			System.out.println("Server socket ready on port: " + port);
    		} catch (IOException e) {
    			System.err.println("Could not listen on port: " + port);
    			System.exit(-1);
    		}
     
    		Socket socket = server.accept();
     
    		/*
    		 * The following gets the latest image from the camera.* 
    		 */
    		URL url = new URL("http://camera3:8080/" + ONESHOTNAME);				
    		BufferedImage bufferedImage = ImageIO.read(url);		
    		byteImage = ByteArrayConversion.toByteArray(bufferedImage);	
     
    		System.out.println(byteImage.toString());
     
    		OutputStream os = socket.getOutputStream();
     
    		ObjectOutputStream oos = new ObjectOutputStream(os);
    		oos.writeObject(byteImage);
    	}
    }

    Client code:
    package client;
     
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    import common.ByteArrayConversion;
     
    public class Image_client {
    	private static Socket socket;
    	private static String host = "devett";
     
    	public static void main(String[] args) {
     
    		try {
    			socket = new Socket(host, 6666);
    			System.out.println("Connection to host established.");
    		} catch (UnknownHostException e) {
    			System.err.println("Don't know about host: " + host);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		try {
    			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    			byte[] byteImage = (byte[])ois.readObject();
    			BufferedImage bufferedImage = ByteArrayConversion.fromByteArray(byteImage);
    			System.out.println(bufferedImage.toString());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}	
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: sending images client/server sockets

    Hello devett. Welcome to the Java Programming Forums.

    I have moved this thread to - Java Networking

    What is the problem here exactly? What are you stuck on? What works, and what doesn't?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending images client/server sockets

    What I'm stuck on is getting the image to continually feed from the server to the client as long as the client is open. The image is always named oneshotimage but it is always a new image. I get it to the the image once, I need it as long as it's requested.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending images client/server sockets

    I'm stuck on getting the image to continaully feed itself the client. The client shows the image as a stream where they can double click the client and move it. The client needs to act as a live stream of the latest image.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending images client/server sockets

    I knew it had to be simple, I figured it out!!!!

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: sending images client/server sockets

    Quote Originally Posted by devett View Post
    I knew it had to be simple, I figured it out!!!!
    Nice one. What was the issue?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending images client/server sockets

    I just had to put the call to the URL in a while (true)
    <highlight=Java]
    while (true) {
    URL url = new URL ...
    down to the ood.writeObject(byteImage);
    [/hightlight]

    and on the client I put the ObjectInputStream into a while true block also.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Location
    Salt Lake City, Utah
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sending images client/server sockets

    Here is a better look at the code:

    server code:
    package server;
     
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URL;
     
    import javax.imageio.ImageIO;
    import common.ByteArrayConversion;
     
    public class Image_Server {
     
    	private static final int port = 6666;
    	private static final String ONESHOTNAME = "oneshotimage.jpg";
    	private static byte[] byteImage;
     
    	/**
    	 * @param args
    	 * @param ONESHOTNAME 
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
     
    		ServerSocket server = null;
     
    		try {
    			server = new ServerSocket(port);
    			System.out.println("Server socket ready on port: " + port);
    		} catch (IOException e) {
    			System.err.println("Could not listen on port: " + port);
    			System.exit(-1);
    		}
     
    		Socket socket = server.accept();
     
    		/*
    		 * The following gets the latest image from the camera.* 
    		 */
    		while (true) {
    			URL url = new URL("http://camera3:8080/" + ONESHOTNAME);				
    			BufferedImage bufferedImage = ImageIO.read(url);		
    			byteImage = ByteArrayConversion.toByteArray(bufferedImage);	
     
    			System.out.println(byteImage.toString());
     
    			OutputStream os = socket.getOutputStream();
     
    			ObjectOutputStream oos = new ObjectOutputStream(os);
    			oos.writeObject(byteImage);
    		}
    	}
    }

    client code:
    package client;
    import java.awt.BorderLayout;
    import java.awt.Graphics2D;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
     
    import common.ByteArrayConversion;
     
    public class Client_Camera_Image extends javax.swing.JFrame {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel jPanelBack;
    	private JLabel jLabelCamera3;
     
    	private static Socket socket;
    	private static String host = "devett";
     
    	/**
    	* Auto-generated main method to display this JFrame
    	*/
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Client_Camera_Image inst = new Client_Camera_Image();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    			}
    		});
    	}
     
    	public Client_Camera_Image() {
    		super();
    		initGUI();
    	}
     
    	private void initGUI() {
    		try {
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			{
    				jPanelBack = new JPanel();
    				getContentPane().add(jPanelBack, BorderLayout.CENTER);
    				jPanelBack.setLayout(null);
    				{
    					jLabelCamera3 = new JLabel();
    					jPanelBack.add(jLabelCamera3);
    					jLabelCamera3.setBounds(421, 48, 348, 298);
    				}
    			}
    			pack();
     
    			this.setSize(856, 494);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    		Thread cgiT = new Thread(new GetCameraImage(), "Camera 3");
    		cgiT.start();
    	}
     
    	protected class GetCameraImage implements Runnable {
     
    		@Override
    		public void run() {
    			try {
    				socket = new Socket(host, 6666);
    				System.out.println("Connection to host established.");
    			} catch (UnknownHostException e) {
    				System.err.println("Don't know about host: " + host);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    			try {
    				int count = 1;
    				while (count != 0) {
    					ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    					byte[] byteImage = (byte[])ois.readObject();
    					BufferedImage bufferedImage = ByteArrayConversion.fromByteArray(byteImage);
    //					System.out.println(bufferedImage.toString());					
     
    					ScaleJPG scale = new ScaleJPG();
     
    					try {
    						scale.scale("camera3", bufferedImage, 640, 480);
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				} count++;
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (ClassNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}	
    		}
     
    	}
     
    	/**
    	 * ScaleJPG class will be used to scale the camera image which comes in as a 640X480 pixel 
    	 * image to half the size on the client. As a camera image is received from the server
    	 * the image is passed into this class to be scaled.  First a new graphics 2d graphic is created 
    	 * to draw into the buffered image.  Then AffineTransform performs a linear mapping of the image
    	 * to preserve the straightness and parallelness of the lines.  It will then set the icon of the
    	 * specific camera based on the cameraName.
    	 * 
    	 */
    	public class ScaleJPG {
     
    		public void  scale(String cameraName, BufferedImage bufferedImage, int width, int height) throws IOException {
    			BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
     
    			Graphics2D g = img.createGraphics();			
     
    			AffineTransform trans = AffineTransform.getScaleInstance((double)320/bufferedImage.getWidth(), 
    					(double)240/bufferedImage.getHeight());
     
    			g.drawRenderedImage(bufferedImage, trans);
     
    			ImageIcon icon = new ImageIcon(img);
     
    			jLabelCamera3.setIcon(icon);
    		}
    	}
     
    }

Similar Threads

  1. Sending zip from server socket
    By moon_werewolf in forum Java Theory & Questions
    Replies: 0
    Last Post: May 30th, 2011, 08:51 AM
  2. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 03:42 PM
  3. sending objects from client to server
    By 11moshiko11 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 8th, 2010, 05:47 AM
  4. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 08:01 AM
  5. Replies: 1
    Last Post: March 23rd, 2010, 03:29 AM