2017-12-21 60 views
0

내 작은 로봇이 비디오를 캡처하지만 이미지 파일을 로컬에서 처리 할만큼 성능이 떨어집니다. 필자는 데스크탑 크롬 브라우저에서 원격으로 볼 수있는 간단한 비디오 스트리밍 코드를 선물했습니다. 아래에 표시된 로봇의이 "서버 코드"는 정상적으로 작동합니다.자바 OpenCV HTTP URL에서 비디오를 캡처하는 방법

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++ >= 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()); 
    } 
} 

이 스트림을 읽으려면 데스크톱 컴퓨터에서 Java OpenCV를 사용하고 싶습니다. OpenCV의의 조각에서 나는 .. 나는이 같은 URL로 비디오 캡처를 열 수 있어야

String url = "http://192.168.1.86:8080/?dummy=param.jpg"; 
VideoCapture capture =new VideoCapture(url); 

을 추정했지만이 "클라이언트 코드는"아무것도하지 않는다. 나는 오류를 던지지 않으며 소켓을 열지도 않는다 (Wireshark를 사용하여 검사). 나는 openCV 2411과 331을 모두 시험해 보았습니다. 나는 주변을 수색 했으므로 아무도 작동하지 않는다는 증거를 보지 못 했으므로 작동해야합니까? 그렇지 않다면 위의 writeJpg 메서드 (ReadJpg)에 상호 클라이언트 코드를 작성하여 현재 OpenCV 메서드에 버퍼링하고 이미지를 전달해야합니다.

답변

0

대부분의 브라우저에서 클라이언트 측 캡쳐는 대부분의 브라우저에서 허용되지 않습니다. (크로스 스크립팅 규칙으로 인해 Mozilla는 exprame을 통해 Iframe에서 미리보기 이미지를 얻을 수 있지만 파이어 폭스 만 지원됩니다) 또는 서버 측에서 imagecreatefromwebp가 있습니다 다음 페이지/삽입을 읽을 수 있어야 PHP는 서버 측에서 그것을 포착하고 imagejpeg 기능을 사용하여 JPG로 저장 화면

갈 것 같은.

  <?php 

      $im = imagecreatefromwebp('URL'); 

     // Convert it to a jpeg file with 100% quality 
      $newimage=imagejpeg($im, './example.jpeg', 100); 
      imagedestroy($im); 
       echo $newimage; 
      ?>