简体   繁体   中英

Writing imageicon in the output stream

I am not able to write an imageicon in the outputstream .Here is my code.Please anyone help me.

    public ScreenSpyer(Socket socket, Robot robot, Rectangle rect) {
        this.socket = socket;
        this.robot = robot;
        this.rectangle = rect;
        start();
    }

    public void run(){
        oos = null; 
        try{                
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(rectangle);
            //  oos.flush();
            // oos.reset();
        }catch(IOException ex){
            ex.printStackTrace();
        }

        while(continueLoop){
            //Capture screen
            image =  robot.createScreenCapture(rectangle);              
            imageIcon = new ImageIcon(image);    
            //Send captured screen to the server
            try {
                System.out.println("before sending image");
                System.out.println("intermidiate");
                // oos.reset();
                oos.writeObject(imageIcon);                    
                System.out.println("New screenshot sent");
                //oos.reset();
                //oos.flush();
                oos.reset();
            } catch (IOException ex) {
               ex.printStackTrace();
            }    

            try{
                Thread.sleep(1000);                   
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }

You say "it gets stuck"; how do you know? This is obviously a thread, terminated by other code. I assume the trace output "New screenshot sent" does not get executed; that could be because it is stuck, or writeObject() could be throwing an exception you are not catching.

Catch throwable after IOException to see if there's another exception.

Right after your image is generated, replace it with a known image and see if it gets written; that will help figure out if there's a problem with this particular writeObject() call or something wrong elsewhere in the program.

Try using a small rectangle from the screen, instead of all of it. Perhaps getScreenSize() returns something unusable, like something one pixel size larger than the screen. If a small rectangle works, try reducing the rectangle by a few pixels in both dimensions.

It looks like you are actually trying to save the screenshot images to an OutputStream or maybe to disk.

In this case, you don't have to use an ImageIcon . You can save the image that is returned from the createScreenCapture call. You can use ImageIO for saving images:

ImageIO.write(BufferedImage image, String formatName, File output);

or

ImageIO.write(BufferedImage image, String formatName, ImageOutputStream output);

or

ImageIO.write(BufferedImage image, String formatName, OutputStream output);

The formatName can be either jpg, png or gif.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM