Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Java
Server Side that sending an images - computer capture screen
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Server  {	
////////////Variables
   private static ObjectOutputStream output;
   //private static DataInputStream input;
   private ServerSocket server;
   private static Socket connection;
   private BufferedImage capture;
   private Rectangle screenRectangle;
   private Robot robot;
   public static boolean isSetUp = false; /*this parameter principle about be sure that setUp streams is being if the streams not setup that'll make problem with close it so we have to check if it setup !*/

/////////////////////////////Methods
   //Constructor
   public Server()
   {
      try{
         server = new ServerSocket(8080,1);
         startRunning();
      } catch(IOException e){e.printStackTrace();}

      //setUp and run the server
      public void startRunning(){
         screenRectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
         while(true){
            try{
               connection = server.accept();//wait for connection, then display connection information
               ComputerRemote.frame.setState(JFrame.ICONIFIED);//hiding JFrame
               ComputerRemote.frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("online.png")));//change icon application to online
               //setUp stream to send and receive data
               output = new ObjectOutputStream(connection.getOutputStream());
               //input = new DataInputStream(connection.getInputStream());
               robot = new Robot();
               isSetUp = true;
               whileConnecting();//during the connecting
               //startRunning();//return to listen clients
         }catch(EOFException e){
            e.printStackTrace();
         }catch(IOException e){
            e.printStackTrace();
         } catch (AWTException e) {
               e.printStackTrace();
         }finally{closeConnection();}//close stream and sockets after done connecting
      }
   }

   //during the connecting
   private void whileConnecting() throws IOException, AWTException {  
      while(connection.isConnected()){
      capture = robot.createScreenCapture(screenRectangle);
      ImageIO.write(capture, "png", connection.getOutputStream());
      output.flush();
      }
   }	

   //close stream and sockets after done connecting
   public static void closeConnection(){
      try{
         if(isSetUp){
            output.close();
            //input.close();
            connection.close();
         }
      }
      catch(IOException e){
         e.printStackTrace();
      }
   }
}


Client - android app that recive the computer's capture screen

Java
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

public class ScreenCapture extends Activity{
   private  Socket connection;
   private  ImageView screenCapture;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_screen_capture);
      screenCapture = (ImageView) findViewById(R.id.screenCapture);
      try {
         connection = new Socket(getIntent().getStringExtra("ip"), 8080);
         while(connection.isConnected())screenCapture.setImageBitmap(BitmapFactory.decodeStream(connection.getInputStream()));
      } catch (IOException e) {
         Toast.makeText(getApplicationContext(), "Error with input picture", Toast.LENGTH_LONG).show();
      }finally {
         try {
            connection.close();
         } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Error with closing connection", Toast.LENGTH_LONG).show();
         }
      }
   }
}
Posted
Updated 18-Apr-15 11:53am
v3
Comments
Member 11325219 18-Apr-15 14:01pm    
My problem that i can to send only one images that is the first one !!!
can someone help please ?
[no name] 18-Apr-15 14:18pm    
Very positive: You posted your code in a very structured/comfortable way.
Still the question: What is the Problem? It can not be one or several Images... because socket are not really interesting whether you send images, mp3 or what else...
Richard MacCutchan 19-Apr-15 3:35am    
Your server process needs to go back to the accept call after processing each image.
Member 11325219 20-Apr-15 15:03pm    
The Sulotion of this problem that to put all this code in AsyncTask :)
thankscfor all

look i changed my code for this and i'll let you see what the exactly happened...

Server Java Side :
C#
/during the connecting
private void whileConnecting() throws IOException, AWTException, InterruptedException {
    int i=0;
    while(i<5){
    System.out.println("i = " + i);
    capture = robot.createScreenCapture(screenRectangle);
    baos = new ByteArrayOutputStream();
    ImageIO.write(capture, "png", baos);
    baos.flush();
    System.out.println("Size of baos = " + baos.size());
    byte[] buffer = baos.toByteArray();
    baos.close();
    baos = null;
    output.writeObject(buffer);
    output.flush();
    i++;
    Thread.sleep(1000);
    }
}


Client Android Side :
C#
try {
       connection = new Socket(getIntent().getStringExtra("ip"), 8080);
       input = new ObjectInputStream(connection.getInputStream());
       int i = 0;
       while (i < 5) {
          i++;
          buffer = (byte[]) input.readObject();
Toast.makeText(getApplicationContext(), "buffer length = " + buffer.length, Toast.LENGTH_LONG).show();
screenCapture.setImageBitmap(BitmapFactory.decodeByteArray(buffer, 0, buffer.length));
           }
         } catch (ClassNotFoundException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }finally {
         try {
             connection.close();
         } catch (IOException e) {
             Toast.makeText(getApplicationContext(), "Error with closing connection", Toast.LENGTH_LONG).show();
         }
     }




i receive on the phone only one picture i dont know how to upload it here
but that what i receive :
i = 0
Size of baos = 81559
i = 1
Size of baos = 81556
i = 2
Size of baos = 109330
i = 3
Size of baos = 110072

and that exactly the length of the bytes that i sent it ...!!
 
Share this answer
 
The Solution is :
put all this code in AsyncTask :)
Thanks for all :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900