2015-11-16 50 views
0

我有一个来自ip-camera的视频流,我想通过服务器处理这个流,所以我可以根据需要在很多设备(如iPad /浏览器)上显示它(相机只有100Mbit/s所以许多设备不显示任何东西)。我有一个jetty http-Server运行。我写了一类获取数据流并将其转换为一个MjpegFrame:刷新servlet jetty java

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"); 
    } 

} 

有谁知道什么是错我的代码?

+0

'Accept-Encoding'不是响应标题。你也可能想看看Servlet 3.1的WriteListener进行异步响应写入。 –

回答

0

由我自己解决了这个问题:

问题是Conent型

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(); 

希望它有助于某人。 欢呼声