mark(FRAME_MAX_LENGTH); int n = getStartOfSequence(this, SOI_MARKER); reset(); readFully(header); length = parseContentLength(header); } catch (NumberFormatException e) { length = getEndOfSeqeunce(this, EOI_MARKER); reset(); skipBytes(n); readFully(frame);
@Override public void run() { long t1; long t2; while (running) { try (final MjpegInputStream stream = request(uri)) { do { t1 = System.currentTimeMillis(); if ((tmp = stream.readFrame()) != null) { image = tmp; } t2 = System.currentTimeMillis(); fps = (double) 1000 / (t2 - t1 + 1); } while (running && !stream.isClosed()); } catch (IOException e) { if (e instanceof EOFException) { // EOF, ignore error and recreate stream continue; } LOG.error("Cannot read MJPEG frame", e); } } }
private MjpegInputStream request(final URI uri) { try { return new MjpegInputStream(get(uri, true)); } catch (Exception e) { throw new WebcamException("Cannot download image. " + e.getMessage(), e); } }
@Override public BufferedImage getImage() { try { return stream.readFrame(); } catch (IOException e) { throw new WebcamException("Cannot get image frame from " + url, e); } }
private int getStartOfSequence(final DataInputStream in, final byte[] sequence) throws IOException { int end = getEndOfSeqeunce(in, sequence); return end < 0 ? -1 : end - sequence.length; }
@Override public void close() { if (!open) { return; } if (connection != null && connection instanceof Closeable) { try { ((Closeable) connection).close(); } catch (IOException e) { throw new WebcamException("Unable to close connection to " + url, e); } } if (stream != null) { try { stream.close(); } catch (IOException e) { throw new WebcamException("Unable to close stream from connection to " + url, e); } } open = false; }
@Override public void open() { if (open) { return; } try { connection = url.openConnection(); } catch (IOException e) { throw new WebcamException("Unable to open connection to " + url, e); } try { stream = new MjpegInputStream(connection.getInputStream()); } catch (IOException e) { throw new WebcamException("Unable to get input stream from connection to " + url, e); } open = true; }