[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Stream images through socket

Posted on 2004-11-28
Medium Priority
Last Modified: 2008-02-01
hey all.

Okay, here's what I'm trying to achieve:

I am going to install two applications. One on the Client machine, and one on the Server machine.
I want to the user on the server machine to be able to view everything happening on the Client machine (like a video recording of it, and streamed over to the server).

I need to come up with the best method of doing this. I've come up with probably the most logical technique:

Keep printing screen on the client machine, and stream the BufferedImage's to the Server. The server then keeps updating the recieved BufferedImage's to the application (which is a JWindow, that takes up the whole screen).

Would you lot agree that this is the best/only technique of achieving this? If not, I'm open to any other suggestions.

If you agree however, that that's the best technique, then I have a couple of queries:

 1)  On the server machine, would the best method of constantly outputting the frames to the app be by using a JLabel ? (ie: JLabel jl_output = new JLabel ( [bufferedImage] );).
 2)  If I use a JLabel to store the frames, what would be the best method of updating it with the new BufferedImage? For example, I don't think that this way updates the display: jl_output = new JLabel ( [newBufferedImage] ); .. Would I need to use: repaint(); to update the display??
 3)  How would I write the BufferedImages to the Socket? Would I need to generate a byte[] array ? And write that to the Socket, then turn the Byte[] array (on the server side) into the BufferedImage ?? If so, anyone got any example code to do this???

I'm open to any and all comments/suggestions.

Thanks in advance! :-)
Question by:DrWarezz
  • 3
  • 3

Accepted Solution

OBCT earned 2000 total points
ID: 12692726
It sounds like this would require alot of bandwidth.
Anyway, you should use the javax.imageio.ImageIO class. It comes standard with J2SE 1.4.2 and up.
In this class you can write an image to Input/Output stream.
The java.awt.Robot class will supply you with a simple screen capture method.
Heres an example...

Rectangle screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
BufferedImage screenImage = robot.createScreenCapture(screenSize);

ImageIO.write(screenImage, "jpeg", socket.getOutputStream());

BufferedImage receivedImage = ImageIO.read(socket.getInputStream());

If you re-instantiate the JLabel, specifying an ImageIcon containing the buffered image in the constructor, the component should repaint itself.

I hope this helps.

Author Comment

ID: 12695031
Thanks for that. This is an app that I will be running on my LAN, so I have a fair bit of bandwidth :-)

Thanks alot for the code, I shall give it a go later when I'm home.

Thanks again,

Author Comment

ID: 12697186
I tried the following (to test the update of the image in the JLabel):

-- ScaleTest.java

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ScaleTest extends JFrame implements ActionListener {
    JLabel jl_testLabel = null;
    public ScaleTest () {
        super( "Scale Test" );
        setBounds( 100, 100, 200, 200 );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JPanel pane = new JPanel ();
        setContentPane ( pane );
        jl_testLabel = new JLabel( new ImageIcon( "imageName1.jpg" ) );
        JButton jb_change = new JButton ( "Change" );
        jb_change.addActionListener ( this );
        pane.add ( jb_change );
        pane.add ( jl_testLabel );
        setVisible ( true );
    public static void main ( String [] args ) {
        new ScaleTest().pack();
    public void actionPerformed ( ActionEvent evt ) {
        jl_testLabel = new JLabel( new ImageIcon( "imageName2.jpg" ) );

It doesn't work ... it should change the image when I click on the button "Change" ... what's up with that?!? :o\ lol

Have I done something wrong with the code? If not, do you have any other ideas? :o\

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 12701186
Sorry DrWarez, it turns out I was wrong.
Reconstructing the Label doesn't repaint it!!! Seems a bit stupid to me but anyway,
try this....

in the actionPerformed(ActionEvent) method, replace this...
jl_testLabel = new JLabel( new ImageIcon( "imageName2.jpg" ) );

with this...
jl_testLabel.setIcon(new ImageIcon("imageName2.jpg"));

Theres no need for repainting because the setIcon method takes care of it all.



Author Comment

ID: 12705872
Thanks alot for that. It works :-)

Your help is much appreciated.

Expert Comment

ID: 12710685
Always glad to help :-)



Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question