无法在 Java/Android 中创建 MJPEG 服务器
Cannot create a MJPEG server in Java/Android
我一直在尝试创建一个 MJPEG 服务器,为此,我得到了一个简单的基础服务器 from here。大部分代码是相同的,只是修改了 handle(socket)
函数。这是 handle(...)
代码 -
private void handle(Socket socket) {
try {
...
Read HTTP request... Not needed for MJPEG.
...
// Initial header for M-JPEG.
String header = "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";
output.write(header.getBytes());
// Start the loop for sending M-JPEGs
isStreaming = true;
if (!socket.isClosed()) {
new Thread(new Runnable() {
int id = 1;
@Override
public void run() {
try {
while (isStreaming) {
if (id > 2) id = 1;
byte[] buffer = loadContent(id + ".jpg");
output.write(("--boundary\r\n" +
"Content-Type: image/jpeg\r\n" +
"Content-Length: " + buffer.length + "\r\n").getBytes());
output.write(buffer);
Thread.sleep(1000);
Log.i("Web Server", "Current id: " + id);
id++;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
output.flush();
output.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我从一些网站上得到了 headers,这个服务器在 C++/Linux 中工作(我不能直接移植代码,因为在 C++ 中,我使用了 QTcpServer
Qt 与 SocketServer
).
有点不同
资产文件夹中有 2 个 JPG,此服务器的工作是显示它们并每秒在它们之间切换。
当我在笔记本电脑 Google Chrome 上打开这个网站时,我只看到一个白屏(服务器连接时出现,但没有正确输出数据)。
如果需要任何其他信息,请在评论中询问,我将编辑问题并添加。
谢谢!
找到解决办法。显然,在 Thread 中使用 Thread 是行不通的。这是新代码 -
if (!socket.isClosed()) {
int id = 1
while (isStreaming) {
try {
if (id > 2) id = 1;
byte[] buffer = loadContent(id + ".jpg");
assert buffer != null;
Log.i("Web Server", "Size of image: " + buffer.length + "bytes");
output.write(("--boundary\r\n" +
"Content-Type: image/jpeg\r\n" +
"Content-Length: " + buffer.length + "\r\n\r\n").getBytes());
output.write(buffer);
Thread.sleep(100);
Log.i("Web Server", "Current id: " + id);
id++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我一直在尝试创建一个 MJPEG 服务器,为此,我得到了一个简单的基础服务器 from here。大部分代码是相同的,只是修改了 handle(socket)
函数。这是 handle(...)
代码 -
private void handle(Socket socket) {
try {
...
Read HTTP request... Not needed for MJPEG.
...
// Initial header for M-JPEG.
String header = "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";
output.write(header.getBytes());
// Start the loop for sending M-JPEGs
isStreaming = true;
if (!socket.isClosed()) {
new Thread(new Runnable() {
int id = 1;
@Override
public void run() {
try {
while (isStreaming) {
if (id > 2) id = 1;
byte[] buffer = loadContent(id + ".jpg");
output.write(("--boundary\r\n" +
"Content-Type: image/jpeg\r\n" +
"Content-Length: " + buffer.length + "\r\n").getBytes());
output.write(buffer);
Thread.sleep(1000);
Log.i("Web Server", "Current id: " + id);
id++;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
output.flush();
output.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我从一些网站上得到了 headers,这个服务器在 C++/Linux 中工作(我不能直接移植代码,因为在 C++ 中,我使用了 QTcpServer
Qt 与 SocketServer
).
资产文件夹中有 2 个 JPG,此服务器的工作是显示它们并每秒在它们之间切换。
当我在笔记本电脑 Google Chrome 上打开这个网站时,我只看到一个白屏(服务器连接时出现,但没有正确输出数据)。
如果需要任何其他信息,请在评论中询问,我将编辑问题并添加。
谢谢!
找到解决办法。显然,在 Thread 中使用 Thread 是行不通的。这是新代码 -
if (!socket.isClosed()) {
int id = 1
while (isStreaming) {
try {
if (id > 2) id = 1;
byte[] buffer = loadContent(id + ".jpg");
assert buffer != null;
Log.i("Web Server", "Size of image: " + buffer.length + "bytes");
output.write(("--boundary\r\n" +
"Content-Type: image/jpeg\r\n" +
"Content-Length: " + buffer.length + "\r\n\r\n").getBytes());
output.write(buffer);
Thread.sleep(100);
Log.i("Web Server", "Current id: " + id);
id++;
} catch (Exception e) {
e.printStackTrace();
}
}
}