【问题标题】:refresh servlet jetty java刷新servlet码头java
【发布时间】:2016-02-17 08:52:28
【问题描述】:

我有一个来自 ip-camera 的视频流,我想通过服务器处理这个流,这样我就可以根据需要在尽可能多的设备(如 iPad/浏览器)上显示它(相机只有 100Mbit/s,所以许多设备没有显示任何内容)。我有一个码头 http-Server 正在运行。我编写了一个获取流并将其转换为 MjpegFrame 的类:

MjpegFrame = frame; 


        try {
            MjpegInputStream m = new MjpegInputStream(url.openStream());
            MjpegFrame f;
            while ((f = m.readMjpegFrame()) != null) {
                if(!running) break;

                frame = f;
            }
            m.close();
        } catch (IOException e) {
            //some error outputs
        }

获取当前帧

 public MjpegFrame getCurrentFrame() {
     return frame;
 }

这很好用。现在我试图用我的 Servlet 显示它,但在这里我只得到一张照片而不是流:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //String auth = request.getAuthType();
    //System.out.println("auth:"+auth);
    if(vm != null) {
        MjpegFrame frame = vm.getCurrentFrame();
        if(frame != null) {

            BufferedOutputStream output = null;
            try{
                output = new BufferedOutputStream(response.getOutputStream(), 1024);
                response.reset();
                response.setBufferSize(1024);
                response.setContentType("image/webp");
                response.setHeader("Cache-Control", "max-age=0") ;
                response.setHeader("Accept-Encoding", "gzip, deflate, sdch");


                while(frame != null){


                    response.setContentLength(frame.getContentLength());    

                    output.write(frame.getJpegBytes(), 0, frame.getContentLength());


                    frame = vm.getCurrentFrame();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {

            }
        } else {
            System.out.println("No image available...");
        }

    } else {
        System.out.println("Error: VideoMultiplier is not set");
    }

}

有人知道我的代码有什么问题吗?

【问题讨论】:

  • Accept-Encoding 不是响应标头。此外,您可能想查看 Servlet 3.1 的 WriteListener 以进行异步响应写入。

标签: java servlets jetty


【解决方案1】:

自己解决了:

问题是内容类型

 response.setContentType("image/webp");

我使用wireshark 对其进行分析,并意识到响应应该看起来不同。无论如何,这就是我的回应:

String contentType = "multipart/x-mixed-replace; boundary=--yourboundary";
response.setContentType(contentType); 

而不是 --yourboundary 您使用相机的边界,或者,为了使其更灵活,构建您自己的标题:

public StringBuffer createHeader(int contentLength) {
    StringBuffer header = new StringBuffer(100);
    header.append("--yourboundary\r\nContent-Type: image/jpeg\r\nContent-Length: ");
    header.append(contentLength);
    header.append("\r\n\r\n");

    return header;
}

然后这样写:

frame = vm.getCurrentFrame();//here I get my frame of the current image I wanna send

StringBuffer header = createHeader(frame.getJpegBytes().length);


byte[] headerBytes = header.toString().getBytes();

byte[] imageBytes = frame.getJpegBytes();
// create a newImage array that is the size of the two arrays
byte[] newImage = new byte[headerBytes.length + imageBytes.length];
// copy headerBytes into start of newImage (from pos 0, copy headerBytes.length bytes)
System.arraycopy(headerBytes, 0, newImage, 0, headerBytes.length);

// copy imageBytes into end of newImage (from pos headerBytes.length, copy imageBytes.length bytes)
System.arraycopy(imageBytes, 0, newImage, headerBytes.length, imageBytes.length);


output.write(newImage,0,newImage.length);
output.flush();

希望它可以帮助某人。 干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2011-01-30
    • 1970-01-01
    • 2016-08-19
    • 2020-08-19
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多