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:
Code java:
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:
Code java:
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();
}
}
}
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?
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.
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.
Re: sending images client/server sockets
I knew it had to be simple, I figured it out!!!!
Re: sending images client/server sockets
Quote:
Originally Posted by
devett
I knew it had to be simple, I figured it out!!!!
Nice one. What was the issue?
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.
Re: sending images client/server sockets
Here is a better look at the code:
server code:
Code Java:
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:
Code Java:
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);
}
}
}