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 boolean isScanPossible() { return OsUtils.getOS() != OsUtils.WIN; }
public static File[] getVideoFiles() { String[] names = DEV.list(new NixVideoDevUtils()); File[] files = new File[names.length]; for (int i = 0; i < names.length; i++) { files[i] = new File(DEV, names[i]); } return files; } }
private List<WebcamDevice> getDevicesLinux() { final List<WebcamDevice> devices = new ArrayList<WebcamDevice>(); for (File vfile : NixVideoDevUtils.getVideoFiles()) { devices.add(new JavaCvDevice(vfile)); } return devices; }
@Override public BufferedImage getImage() { try { return stream.readFrame(); } catch (IOException e) { throw new WebcamException("Cannot get image frame from " + url, e); } }
public static final byte[] getImageBytes(Webcam webcam, String format) { return ImageUtils.toByteArray(webcam.getImage(), format); }
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); } }
private Dimension[] findResolutions() { initializer.initialize(); pipeReady(); try { return findResolutions0(); } finally { pipeStop(); } }
/** * Put image RGB data into the {@link ByteBuffer}. Please note that data from {@link ByteBuffer} * consumed by this method does not contain original JPEG data bytes, but bytes representing RGB * data of the image constructed from JPEG data. */ @Override public void getImageBytes(ByteBuffer buffer) { final BufferedImage bi = getImage(); if (bi != null) { buffer.put(ImageUtils.imageToBytes(bi)); } } }
public byte[] write(final BufferedImage bi) { // loop and try to compress until compressed image bytes array is not longer than a given // maximum value, reduce quality by 25% in every step int m = size; int s = 0; int i = 0; do { if ((s = compress(bi, quality)) > m) { quality *= 0.75; if (i++ >= 20) { break; } } } while (s > m); return baos.toByteArray(); }
@Override public void close() { // not initialized = do nothing, no need to close if (!initializer.isInitialized()) { return; } if (!open) { return; } pipeStop(); open = false; }
@Override public BufferedImage filter(BufferedImage src, BufferedImage dest) { final int w = src.getWidth(); final int h = src.getHeight(); int c, a, r, g, b, i, max = 1; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { c = src.getRGB(x, y); a = clamp((c >> 24) & 0xff); r = clamp((c >> 16) & 0xff); g = clamp((c >> 8) & 0xff); b = clamp(c & 0xff); i = (a << 24) | (r << 16) | (g << 8) | b; if (i > max) { max = i; } } } for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { c = src.getRGB(x, y); i = c * 256 / max; dest.setRGB(x, y, i); } } return dest; }
public void teardown() { if (initialized.compareAndSet(true, false)) { try { initializable.teardown(); } catch (Exception e) { throw new WebcamException(e); } } }
@Override public void dispose() { close(); LOG.debug("Teardowning device {}", getName()); initializer.teardown(); GsUtils.dispose(source); GsUtils.dispose(filter); GsUtils.dispose(convert); GsUtils.dispose(sink); GsUtils.dispose(pipe); disposed = true; }
/** * Get capture device protocol. This will be: * <ul> * <li><code>dshow://</code> for Windows</li> * <li><code>qtcapture://</code> for Mac</li> * <li><code>v4l2://</code> for linux</li> * </ul> * * @return Capture device protocol * @throws WebcamException in case when there is no support for given operating system */ public String getCaptureDevice() { switch (OsUtils.getOS()) { case WIN: return "dshow://"; case OSX: return "qtcapture://"; case NIX: return "v4l2://"; default: throw new WebcamException("Capture device not supported on " + OsUtils.getOS()); } }
@Override public BufferedImage getImage() { initializer.initialize(); LOG.trace("Device {} get image", getName()); try { return exchanger.exchange(null); } catch (InterruptedException e) { throw new WebcamException("Image exchange has been interrupted", e); } }
/** * Return image RGB data in form of {@link ByteBuffer}. Please note that {@link ByteBuffer} * returned by this method does not contain original JPEG data bytes, but bytes representing RGB * data of the image constructed from JPEG data. */ @Override public synchronized ByteBuffer getImageBytes() { final BufferedImage bi = getImage(); if (bi == null) { return null; } return ByteBuffer.wrap(ImageUtils.imageToBytes(bi)); }
private OpenCVFrameGrabber buildGrabber() throws FrameGrabber.Exception { switch (OsUtils.getOS()) { case WIN: case OSX: return OpenCVFrameGrabber.createDefault(address); case NIX: return OpenCVFrameGrabber.createDefault(vfile); default: throw new UnsupportedOperationException("Current OS is not supported"); } }
private VideoCapture getVideoCaptureForOs() { switch (OsUtils.getOS()) { case WIN: case OSX: return new VideoCapture(address); case NIX: return new VideoCapture(vfile.getAbsolutePath()); default: throw new UnsupportedOperationException("Unsupported operating system"); } }
@Override public List<WebcamDevice> getDevices() { switch (OsUtils.getOS()) { case WIN: return getDevicesWindows(); case NIX: return getDevicesLinux(); case OSX: return getDevicesMacOs(); default: throw new WebcamException("Operating system not supported"); } }