用javasocket实现图像传输

kkbh8khc  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(196)

我有学校助理,我正在尝试用java实现从客户端到服务器的传输截图。事实上,我觉得我很接近它,因为当我在同一台计算机上运行客户端和服务器时,它工作得很好,但正如你所知道的,我需要在不同的计算机上运行它。问题是,当我试图这样做,有一个损坏的形象。我认为读写方法有问题。你能帮帮我吗?
客户:

InetAddress host = InetAddress.getLocalHost();
        Socket socket = null;
        while(true) {
        try {

            Robot r = new Robot();
            socket = new Socket(host.getHostAddress(), 9876);

            Rectangle capture = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 

            BufferedImage image = r.createScreenCapture(capture); 

            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

            ImageIO.write(image,"jpg",baos);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 

            BufferedInputStream reader = new BufferedInputStream(bais);

            BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

            byte[] buffer = new byte[4096];
            int bytesRead;
            while((bytesRead = reader.read(buffer))!=-1) {
                out.write(buffer, 0, bytesRead);
            }

            reader.close();
            out.flush();
            out.close();

            Thread.sleep(500);

        } catch (Exception e) {
            e.printStackTrace();
        }
}

服务器:

server = new ServerSocket(port);

        while(true) {

        Socket socket = server.accept();

        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int nRead;
        byte[] buffer = new byte[4096];
        while((nRead = in.read(buffer, 0, buffer.length))!= -1) {
            baos.write(buffer);
        }

        BufferedImage image = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));

        if(frame==null){
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                frame=new JFrame();
                frame.setSize(screenSize.width, screenSize.height);
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                if(frame.getHeight()!=image.getHeight() || frame.getWidth()!=image.getWidth()) {
                    Image resizedImage = image.getScaledInstance(frame.getWidth(), frame.getHeight(), Image.SCALE_SMOOTH);
                    label=new JLabel();
                    label.setIcon(new ImageIcon(resizedImage));
                    frame.getContentPane().add(label,BorderLayout.CENTER);
                    frame.setLocationRelativeTo(null);
                    frame.pack();
                    frame.setVisible(true);
                }else {
                    label=new JLabel();
                    label.setIcon(new ImageIcon(image));
                    frame.getContentPane().add(label,BorderLayout.CENTER);
                    frame.setLocationRelativeTo(null);
                    frame.pack();
                    frame.setVisible(true);
                }

           }else {
               if(frame.getHeight()!=image.getHeight() || frame.getWidth()!=image.getWidth()) {
                    Image resizedImage = image.getScaledInstance(frame.getWidth(), frame.getHeight(), Image.SCALE_SMOOTH);
                    label.setIcon(new ImageIcon(resizedImage));
                }else {
                    label.setIcon(new ImageIcon(image));
                }

           }

    }

这是当我从客户那里得到图像时的样子谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题