title: Java OpenCV how to capture video from HTTP URL
body: <p>My little robot captures video but doesn't have enough performance to process image files locally. I have been gifted some simple video streaming code that I can view remotely in my desktop Chrome browser. This "server code" on the robot, shown below, works fine.</p>
  
  <pre><code>public class HttpStreamOpenCV {
      public static void main(String[] args) throws Exception {
          System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
          Mat mat = new Mat();
          VideoCapture vid = new VideoCapture(0);
          vid.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 160);
          vid.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 120);
          vid.open(0);
          System.out.println("Camera open");
  
          ServerSocket ss = new ServerSocket(8080);
          Socket sock = ss.accept();
          System.out.println("Socket connected");
          String boundary = "Thats it folks!";
          writeHeader(sock.getOutputStream(), boundary);
          System.out.println("Written header");
  
          long stime = System.currentTimeMillis();
          int cnt = 0;
          while (Button.ESCAPE.isUp()) {
              vid.read(mat);
              if (!mat.empty()) {
                  writeJpg(sock.getOutputStream(), mat, boundary);
                  System.out.println("Written jpg");
                  if (cnt++ &gt;= 100)
                  {
                      long stop = System.currentTimeMillis();
                      System.out.println("Frame rate: " + (cnt*1000/(stop - stime)));
                      cnt = 0;
                      stime = stop;
                  }
              } else  System.out.println("No picture");
          }
          sock.close();
          ss.close();    
      }
  
      private static 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());
      }
  
      private static void writeJpg(OutputStream stream, Mat img, String boundary) throws IOException {
          MatOfByte buf = new MatOfByte();
          Highgui.imencode(".jpg", img, buf);
          byte[] imageBytes = buf.toArray();
          stream.write(("Content-type: image/jpeg\r\n" +
              "Content-Length: " + imageBytes.length + "\r\n" +
              "\r\n").getBytes());
          stream.write(imageBytes);
          stream.write(("\r\n--" + boundary + "\r\n").getBytes());
      }
  }
  </code></pre>
  
  <p>I'd like to use Java OpenCV on my desktop machine to read this stream. From snippets of OpenCV I've inferred that I should be able to open a video capture with a URL like this..</p>
  
  <pre><code>String url = "http://192.168.1.86:8080/?dummy=param.jpg";
  VideoCapture capture =new VideoCapture(url); 
  </code></pre>
  
  <p>but this "client code" does nothing. I doesn't throw an error, it doesn't even try to open a socket (checked using Wireshark). I've tried both openCV 2411 and 331. I've searched around and seen no evidence that anyone else got it to work, so should it work? If not I'll need to write the reciprocal client code to the writeJpg method above (ReadJpg) to buffer and pass the image to OpenCV methods at present this is beyond me.   </p>