从套接字接收图像并显示它

时间:2012-05-09 11:42:02

标签: java sockets user-interface

首先,我想声明我不做我的作业,只是帮助一个无法编码的朋友来挽救她的生命。我必须编写一个小程序,打开TCP套接字,接收图像,并以全屏显示。我不是Java专家(我不能选择语言)或gui专家,而且我有点陷入困境。

我已经接受了接收部分:

public class test {
public static void main(String[] args) {


    int listenPort = 25001;

    try {
        ServerSocket socket= new ServerSocket(listenPort);
        Socket clientSocket = null;

        while(true){

        clientSocket = socket.accept();
        InputStream input = clientSocket.getInputStream();
        BufferedInputStream input_r = new BufferedInputStream(input);
        byte[] message= new byte[5];
        byte[] width_b = new byte[4];
        byte[] length_b = new byte[4];
        input.read(message,0,5);
        input.read(length_b,0,4);
        input.read(width_b,0,4);
        int width = ByteBuffer.wrap(width_b).getInt();
        int length = ByteBuffer.wrap(length_b).getInt();
        String s = new String(message);
        System.out.println("Message:" + s + " width:" + width + " length:" + length);
        byte[][] data = new byte[length][width];
        for(int i=0;i<length;i++){
            input.read(data[i],0,width);
        }



        }



    }
    catch (IOException e) {
        System.out.println("Accept failed: 4444");
        System.exit(-1);
    }   


}

}

而且,我在网上发现了一个覆盖第二部分的片段:

public class fullscreen extends JFrame { 

// this line is needed to avoid serialization warnings  
private static final long serialVersionUID = 1L;


Image screenImage; // downloaded image  
int w, h; // Display height and width 


// Program entry 
public static void main(String[] args) throws Exception { 
    if (args.length < 1) // by default program will load AnyExample logo 
        new fullscreen("http://www.anyexample.com/i/logo.gif"); 
    else 
        new fullscreen(args[0]); // or first command-line argument 
} 

// Class constructor  
fullscreen(String source) throws MalformedURLException { 

    // Exiting program on window close 
    addWindowListener(new WindowAdapter() { 
        public void windowClosing(WindowEvent e) { 
            System.exit(0);
        } 
    });

    // Exitig program on mouse click 
    addMouseListener(new MouseListener() { 
        public void mouseClicked(MouseEvent e) { System.exit(0); } 
        public void mousePressed(MouseEvent e) {} 
        public void mouseReleased(MouseEvent e) {} 
        public void mouseEntered(MouseEvent e) {} 
        public void mouseExited(MouseEvent e) {} 
    } 
    );

    // remove window frame  
    this.setUndecorated(true);

    // window should be visible 
    this.setVisible(true);

    // switching to fullscreen mode 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice[] gs = ge.getScreenDevices();
    gs[gs.length-1].setFullScreenWindow(this);

    // getting display resolution: width and height 
    w = this.getWidth();
    h = this.getHeight();
    System.out.println("Display resolution: " + String.valueOf(w) + "x" + String.valueOf(h));

    // loading image  
    if (source.startsWith("http://")) // http:// URL was specified 
        screenImage = Toolkit.getDefaultToolkit().getImage(new URL(source));
    else 
        screenImage = Toolkit.getDefaultToolkit().getImage(source); // otherwise - file 
} 

public void paint (Graphics g) { 
    if (screenImage != null) // if screenImage is not null (image loaded and ready) 
        g.drawImage(screenImage, // draw it  
                    w/2 - screenImage.getWidth(this) / 2, // at the center  
                    h/2 - screenImage.getHeight(this) / 2, // of screen 
                    this);
        // to draw image at the center of screen 
        // we calculate X position as a half of screen width minus half of image width 
        // Y position as a half of screen height minus half of image height 
} 

}

我如何合并这两件事?我的第一个猜测是在fullscreen :: ctor中打开套接字但是我应该在哪里处理接收/数据格式化?我怎样才能确保显示器正确刷新?

感谢您的帮助。

0 个答案:

没有答案