gpt4 book ai didi

sockets - 在Java中的套接字上循环传输时,我只得到一个图像

转载 作者:行者123 更新时间:2023-12-03 12:05:09 25 4
gpt4 key购买 nike

我基本上想做的是在Java中实现实时视频流。即从一台计算机的网络摄像头获取视频,并将该视频实时发送到另一台计算机

这是我的服务器端代码,我在其中使用opencv库以循环的形式捕获图像,然后将其保存到文件中。
然后,我使用Filestream打开它,并将其发送到其他网络,下面是我的客户端代码

错误:
我在客户端仅获得一张图像,即从网络摄像头捕获的第一张图像

如何获得所有图像(即在客户端上作为视频)的解决方法是什么

我已经导入了所有必要的程序包

 public class filesen{
static IplImage bimg;
public static void main(String ar[])throws Exception{
Socket clientSocket=new Socket("localhost",1234);
CanvasFrame canvas = new CanvasFrame("sending");
//int i=1;
FrameGrabber grabber = new OpenCVFrameGrabber("");
try {
grabber.start();
IplImage img;
while (true) {
OutputStream out=clientSocket.getOutputStream();
img = grabber.grab();
if (img != null) {

opencv_highgui.cvSaveImage("a.jpg", img);

FileInputStream fis=new FileInputStream("a.jpg");
System.out.println(fis);
int x=0;
while(true){
x=fis.read();
if(x==-1)break;
out.write(x);
}
bimg = opencv_highgui.cvLoadImage("a.jpg");
canvas.showImage(bimg);
// out.close();
//canvas.showImage(img);
i++;
System.out.println(i);
}

}

}
catch (Exception e) {
}
}
}

这是我的客户端代码
public class filere extends Applet {
//static IplImage bimg;
public static void main(String ar[]) throws Exception{
CanvasFrame canvas = new CanvasFrame("Webcam");
ServerSocket ss=new ServerSocket(1234);
while(true){
Socket clientSocket=ss.accept();
InputStream in=clientSocket.getInputStream();
//OutputStream out=clientSocket.getOutputStream();
FileOutputStream fos=new FileOutputStream("b.jpg");
int x=0;
while(true){
x=in.read();
if(x==-1)break;
fos.write(x);
}
bimg = opencv_highgui.cvLoadImage("b.jpg");
canvas.showImage(bimg);

}
}
}

最佳答案

这样想吧...

您有一个生产者,它生成图像,您有一个消费者,它想要获取这些图像。

消费者要求生产者提供下一张图像,当生产者准备好时,它发送下一张图像,然后消费者以所需的任何方式消费它。

通常,消费者等待生产者生产某种东西然后对其进行处理,我采取了稍微不同的方向,允许消费者“请求”下一个可用的图像。这使得制作人可以继续一点点快乐,而不会因发送任何可能的东西而陷入困境。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

为什么要问,因为生成帧可能比通过网络发送帧要快,所以您可以生成丢帧算法,用图像填充队列,并让队列管理它愿意缓冲的帧数。然后,“生产者”将根据消费者的请求拉出队列的帧并将其发送...

这仅是概念证明,它不涉及多个连接或连接恢复或其他您需要处理的事情

此示例使生产者(服务器)从服务器上的目录发送图像文件。它将根据客户端/消费者的请求继续发送图像,直到没有剩余的图像为止,这时它将发送“NoMoreImages”响应...

使用者将向服务器请求下一个图像,将其写入磁盘,然后继续操作直到收到“NoMoreImages”响应为止,此时它将退出。

消费者

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Consumer {

public static void main(String[] args) {
System.out.println("Connecting to producer");
try (Socket socket = new Socket("localhost", 1234)) {

try (InputStream is = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
OutputStream os = socket.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {

boolean moreImages = true;
int imageCount = 0;
do {

System.out.println("Requesting next image " + imageCount);
writer.write("NextImage");
writer.newLine();
writer.flush();

String response = reader.readLine();
System.out.println("Got response " + response);
if (response.startsWith("NextImage;")) {

String type = response.split(";")[1];
String lengthValue = response.split(";")[2];
long bytesToBeConsumed = Long.parseLong(lengthValue);
try (FileOutputStream fos = new FileOutputStream(new File("Image" + imageCount + "." + type))) {

long bytesThatHaveBeenConsumed = 0;
int bytesThatWereRead = 0;
byte[] buffer = new byte[2048];

System.out.println("Reading image of type " + type + " of byte length " + bytesToBeConsumed);

while (bytesThatHaveBeenConsumed != bytesToBeConsumed && (bytesThatWereRead = is.read(buffer)) != -1) {

bytesThatHaveBeenConsumed += bytesThatWereRead;
fos.write(buffer, 0, bytesThatWereRead);

}

System.out.println("Finished reading image " + imageCount + " with " + bytesThatHaveBeenConsumed + " bytes consumed");

}

imageCount++;

} else if (response.startsWith("NoMoreImages")) {

moreImages = false;
System.out.println("No more images...");
writer.write("Bye");
writer.newLine();
writer.flush();

}

} while (moreImages);

}

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

System.out.println("Consumer has left the building...");

}

}

制片人
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Producer {

public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(1234)) {

System.out.println("Waiting for connection from consumer...");
try (Socket socket = serverSocket.accept()) {

System.out.println("Listing files...");
File[] images = new File("/some/directory/full/of/images").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String name = pathname.getName().toLowerCase();
return name.endsWith(".gif")
|| name.endsWith(".jpeg")
|| name.endsWith(".jpg")
|| name.endsWith(".png");
}
});

System.out.println("Have " + images.length);

int imageIndex = 0;

try (InputStream is = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
OutputStream os = socket.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {

boolean moreRequets = true;

do {

System.out.println("Waiting for consumer request");
String request = reader.readLine();
if ("NextImage".equals(request)) {

System.out.println("Get next image " + imageIndex);
if (images != null && imageIndex < images.length) {

File imageFile = images[imageIndex];
imageIndex++;

long length = imageFile.length();
String type = imageFile.getName();
type = type.substring(type.lastIndexOf(".") + 1).toLowerCase();

String response = new StringBuilder(128).
append("NextImage;").
append(type).
append(";").
append(length).toString();

System.out.println("Sending response to consumer " + response);
writer.write(response);
writer.newLine();
writer.flush();

System.out.println("Sending file...");
try (FileInputStream fis = new FileInputStream(imageFile)) {

byte[] buffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = fis.read(buffer)) != -1) {

os.write(buffer, 0, bytesRead);

}

}

System.out.println("File sent...");

} else {

System.out.println("No more images...");
writer.write("NoMoreImages");
writer.newLine();
writer.flush();

}

} else if ("Bye".equals(request)) {

moreRequets = false;
System.out.println("Consumer has left the building...");

}

} while (moreRequets);

}

}

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

System.out.println("Producer has left the building");
}

}

我不能强调,这仅是 概念的证明,您确实应该考虑拥有一个单独的线程来管理与服务器的传入连接,因此每个客户端都可以独立处理。

您不应将套接字管理与GUI管理混为一谈,它们应在单独的线程中进行管理,并且GUI更新应在主GUI线程的上下文中进行维护。

关于sockets - 在Java中的套接字上循环传输时,我只得到一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26495165/

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