这个问题在这里已经有答案了:
创建一个socket服务器,允许通过线程和java进行多个连接(2个答案)
socket编程多客户端到一台服务器(5个答案)
4个月前关门了。
如何在java中打开多个服务器套接字连接?
我的项目
我想用opencv和http流访问网络摄像头。
如何在浏览器中的多个选项卡中访问?
public class HttpStreamServer implements Runnable {
private BufferedImage img = null;
private ServerSocket serverSocket;
private Socket socket;
private final String boundary = "stream";
private OutputStream outputStream;
public Mat imag;
public HttpStreamServer(Mat imagFr) {
this.imag = imagFr;
}
public void startStreamingServer() throws IOException {
serverSocket = new ServerSocket(8080);
socket = serverSocket.accept();
writeHeader(socket.getOutputStream(), boundary);
}
private void writeHeader(OutputStream stream, String boundary) throws IOException {
stream.write(("HTTP/1.0 200 OK\r\n" +
"Connection: close\r\n" +
"Max-Age: 0\r\n" +
"Expires: 0\r\n" +
"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" +
"Pragma: no-cache\r\n" +
"Content-Type: multipart/x-mixed-replace; " +
"boundary=" + boundary + "\r\n" +
"\r\n" +
"--" + boundary + "\r\n").getBytes());
}
public void pushImage(Mat frame) throws IOException {
if (frame == null)
return;
try {
outputStream = socket.getOutputStream();
BufferedImage img = Mat2bufferedImage(frame);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
byte[] imageBytes = baos.toByteArray();
outputStream.write(("Content-type: image/jpeg\r\n" +
"Content-Length: " + imageBytes.length + "\r\n" +
"\r\n").getBytes());
outputStream.write(imageBytes);
outputStream.write(("\r\n--" + boundary + "\r\n").getBytes());
} catch (Exception ex) {
socket = serverSocket.accept();
writeHeader(socket.getOutputStream(), boundary);
}
}
public void run() {
try {
System.out.print("go to http://localhost:8080 with browser");
startStreamingServer();
while (true) {
pushImage(imag);
}
} catch (IOException e) {
return;
}
}
public void stopStreamingServer() throws IOException {
socket.close();
serverSocket.close();
}
public static BufferedImage Mat2bufferedImage(Mat image) throws IOException {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
img = ImageIO.read(in);
return img;
}
}
opencvcamerastream
当我打开 localhost:8080
在浏览器中,它工作平稳。
但是,当我在多个选项卡中打开时,它只在一个选项卡中工作。
此代码适用于单个连接。
如何在浏览器的多个选项卡中访问网络摄像头?
暂无答案!
目前还没有任何答案,快来回答吧!