2

我想制作一个为桌面制作直播的小程序。应该是这样,您将图片发送到回显服务器,然后他将其响应给客户端。在那里你可以绘制图像。并排。所以它就像一部电影(或类似的东西)。但我总是得到一个 indexoutofbounds 异常。错误在哪里或如何改进我的程序。

ImageIO.write 行显示错误

//客户端主

公共类主要{

public static void main(String[] args) {

    Frame frm = new Frame();

    Frame.Client client;

    frm.setLayout(null);
    frm.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
    frm.setResizable(false);
    frm.setSize(1600,900);
    frm.setVisible(true);



}

}

// 获取并发送桌面图像

公共类桌面捕获{

Robot robo;
BufferedImage screenImage;
Rectangle bounding;

public desktopCapture() {
    try {
        bounding = new Rectangle(0,0,1600,900);
        robo = new Robot();
    } catch (AWTException e) {e.printStackTrace();}
}

public void sendScreenCapture(Socket client) {
    screenImage = robo.createScreenCapture(bounding);
    try {
        ImageIO.write(screenImage, "png", client.getOutputStream());

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

}

// 在用于 actionListener 对象的第二帧函数中,所以我可以说谁将他的桌面流式传输,哪些只将图像发送到。公共无效readImage(){而(真){

            try {
                while((screenImage = ImageIO.read(in)) != null){

                    repaintScreen();

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

公共无效发送图像(){

        try {
            while(true){
            dC.sendScreenCapture(client);
            System.out.println("read1");
            while((screenImage = ImageIO.read(in)) != null){
            System.out.println("read2");
            ImageIO.write(screenImage, "png", new File("image1.png"));

            Thread.sleep(250);
            }
            repaintScreen();
            screenImage = null;

            }   
            } catch (IOException | InterruptedException e) {e.printStackTrace();}

     }
    }

}

//客户端线程

公共类处理程序实现 Runnable {

Socket client;
OutputStream out;
InputStream in; 
PrintWriter writer;
BufferedImage image;

public handler(Socket client) {
    this.client = client;
}

@Override
public void run() {

    try {

        in = client.getInputStream();       
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    while(true) {
        System.out.println("write1");
        while((image = ImageIO.read(in)) != null){
            System.out.println("write2");
    for(int i = 1;i <= Server.connectionArray.size();i++){
        Socket TEMP_SOCK = (Socket)Server.connectionArray.get(i-1);
     out = TEMP_SOCK.getOutputStream();
     writer = new PrintWriter(out);

    ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
    System.out.println("write3");
        }
      image = null;
     }
    }

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

}

4

1 回答 1

0

I would change your for loop to:

int count = Server.connectionArray.size()
int index  = count - 1; 
for(int i = 0;i < count; i++){
        Socket TEMP_SOCK = (Socket)Server.connectionArray.get(index);
        out = TEMP_SOCK.getOutputStream();
        writer = new PrintWriter(out);

        ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
        System.out.println("write3");
  }
于 2013-08-18T01:33:48.180 回答