gpt4 book ai didi

Java 视频聊天应用程序输入输出流不起作用

转载 作者:行者123 更新时间:2023-11-30 01:53:22 25 4
gpt4 key购买 nike

我正在制作一个视频聊天应用程序,它使用 java 网络(又名套接字)将网络摄像头的图像发送到另一个客户端。

我的代码首先发送缓冲图像数据的长度,然后发送实际数据。服务器还首先读取 int,然后读取数据本身。第一个图像有效,但之后,数据输入流读取负数作为长度。

服务器端:

frame = new JFrame();
while (true) {
try {

length = input.readInt();
System.out.println(length);
imgbytes = new byte[length];
input.read(imgbytes);
imginput = new ByteArrayInputStream(imgbytes);
img = ImageIO.read(imginput);
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);

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

客户端:

while(true) {
try {

currentimg = webcam.getImage();
ImageIO.write(currentimg, "jpg", imgoutputstream);
imgbytes = imgoutputstream.toByteArray();
out.writeInt(imgbytes.length);
out.write(imgbytes);

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

最佳答案

在客户端,您始终将新图像写入现有流。这会导致每次迭代中数组大小不断增加。在java中int的最大值为2147483647。如果增加这个整数,它会跳到最小值 auf int ,该值是负数(参见 this article )。

因此,要修复此错误,您需要在写入下一个图像之前清除流,以便大小永远不会大于整数的最大值。

关于Java 视频聊天应用程序输入输出流不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55311825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com