Java Buffer Image表格在线网络摄像头

时间:2013-11-26 17:46:57

标签: java servlets webcam bufferedimage

我必须编写servlet,从在线网络摄像头捕获一些图像。每个参数(如URL,间隔,数字和计数)都是通过POST方法发送的。在我的servlet文件中,我有类似的东西:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url = request. getParameter("url").toString();
    int interwal = Integer.parseInt(request.getParameter("interwal").toString());
    int nrSt = Integer.parseInt(request.getParameter("nr").toString());
    int il = Integer.parseInt(request.getParameter("il").toString());
    PrintWriter out = response.getWriter();
    BufferedImage img;
    URL imgURL;
    File imgFile;
    for(int i=0; i<il; i++){
        try{
            imgURL = new URL(url);
            img = ImageIO.read(imgURL);
            imgFile = new File("E:\\image" + (nrSt+i) + ".jpg");
            ImageIO.write(img, "png", imgFile);
            out.print("Saved image" + (nrSt+i) + ".jpg<br>");
        } catch(IOException e){
            out.print("Error reading Image!");
            e.printStackTrace();
        }
        try {
            Thread.sleep(interwal*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

为了检测此网络摄像头,我必须更改以捕获图像表单:cam

1 个答案:

答案 0 :(得分:0)

使用网络摄像头页面可能会困难得多。您选择的示例使用动态图像URL,该URL使用JavaScript在页面上计算 - 但它很简单,可以轻松提取您需要的内容。下一个问题是图片网址指向的服务以ImageIO.read()似乎无法理解的多部分回复进行回答。

以下独立代码似乎成功获取了网络摄像头图像:

public static void main(String[] args) {
    try {
        // get webcam page
        URL url = new URL("...your example URL.../webcam/campob.html");
        InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
        Scanner scanner = new Scanner(isr);
        final Pattern nullDelimiter = Pattern.compile("<<<>>>");
        String html = scanner.useDelimiter(nullDelimiter).next();
        scanner.close();

        // extract image URL from HTML
        final Pattern extractPattern = Pattern.compile(
                "^var BaseURL = \"([^\"]+)\".*"
                + "^var ImageResolution = \"([^\"]+)\".*"
                + "^var File = \"([^\"]+)\"", 
                Pattern.MULTILINE|Pattern.DOTALL);
        Matcher m = extractPattern.matcher(html);
        if (!m.find()) throw new RuntimeException();
        URL imgURL = new URL(m.group(1) + m.group(3) + m.group(2));
        System.out.println("imgURL=" + imgURL);

        // read headers into buffer 
        InputStream is = imgURL.openStream();
        byte[] buffer = new byte[131072];
        int bytes = 0;
        Pattern headersPattern = Pattern.compile(
                "^Content-Length:\\s*(\\d+)\\s*$.*?^$\\r?\\n", 
                Pattern.MULTILINE|Pattern.DOTALL);
        while (bytes < buffer.length) {
            int count = is.read(buffer, bytes, buffer.length - bytes);
            if (count < 0) break;
            bytes += count;
            m = headersPattern.matcher(new String(buffer, "ASCII"));
            if (m.find()) break;
        }

        // read rest of image bytes into buffer
        int offset = m.end();
        int contentLength = Integer.parseInt(m.group(1));
        int limit = Math.min(buffer.length, offset + contentLength);
        while (bytes < limit) {
            int count = is.read(buffer, bytes, limit - bytes);
            if (count < 0) break;
            bytes += count;
        }
        is.close();
        System.out.println("bytes=" + bytes + " offset=" + offset);

        // read image from buffer (start after header)
        is = new ByteArrayInputStream(buffer);
        is.skip(offset);
        Image img = ImageIO.read(is);
        System.out.println(img);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
}

请注意,代码缺少真正的错误处理,缓冲区具有固定大小等。