@Override public BufferedImage getImage() { if (!open) { throw new RuntimeException("Webcam has to be open to get image"); } Buffer buffer = grabber.grabFrame(); Image image = converter.createImage(buffer); if (image == null) { throw new RuntimeException("Cannot get image"); } int width = image.getWidth(null); int height = image.getHeight(null); int type = BufferedImage.TYPE_INT_RGB; BufferedImage buffered = new BufferedImage(width, height, type); Graphics2D g2 = buffered.createGraphics(); g2.drawImage(image, null, null); g2.dispose(); buffered.flush(); return buffered; }
@Override public void rgbFrame(boolean preroll, int width, int height, IntBuffer rgb) { LOG.trace("New RGB frame"); if (t1 == -1 || t2 == -1) { t1 = System.currentTimeMillis(); t2 = System.currentTimeMillis(); } BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); tmp.setAccelerationPriority(0); rgb.get(((DataBufferInt) tmp.getRaster().getDataBuffer()).getData(), 0, width * height); tmp.flush(); image = tmp; if (starting.compareAndSet(true, false)) { synchronized (this) { this.notifyAll(); } LOG.debug("GStreamer device ready"); } t1 = t2; t2 = System.currentTimeMillis(); fps = (4 * fps + 1000 / (t2 - t1 + 1)) / 5; }
@Override public BufferedImage getImage() { ByteBuffer buffer = getImageBytes(); if (buffer == null) { LOG.error("Images bytes buffer is null!"); return null; } byte[] bytes = new byte[size.width * size.height * 3]; byte[][] data = new byte[][] { bytes }; buffer.get(bytes); DataBufferByte dbuf = new DataBufferByte(data, bytes.length, OFFSET); WritableRaster raster = Raster.createWritableRaster(smodel, dbuf, null); BufferedImage bi = new BufferedImage(cmodel, raster, false, null); bi.flush(); return bi; }
/** * 等比缩放,居中剪切 * * @param src * @param dest * @param w * @param h * @throws IOException */ public static void scale(String src, String dest, int w, int h) throws IOException { if (notImageExtName(src)) { throw new IllegalArgumentException("只支持如下几种图片格式:jpg、jpeg、png、bmp"); } Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(getExtName(src)); ImageReader reader = (ImageReader) iterator.next(); InputStream in = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(in); reader.setInput(iis); BufferedImage srcBuffered = readBuffereImage(reader, w, h); BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics graphics = targetBuffered.getGraphics(); graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图 graphics.dispose(); srcBuffered.flush(); save(targetBuffered, dest); targetBuffered.flush(); }
/** * 生成该图片对应的 Base64 编码的字符串 * * @param targetFile * 图片文件 * @return 图片对应的 Base64 编码的字符串 */ public static String encodeBase64(File targetFile) { BufferedImage image = null; try { image = ImageIO.read(targetFile); } catch (IOException e) { throw Lang.wrapThrow(e); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(baos); image.flush(); try { ImageIO.write(image, Files.getSuffixName(targetFile), bos); bos.flush(); bos.close(); } catch (IOException e) { throw Lang.wrapThrow(e); } byte[] bImage = baos.toByteArray(); return Base64.encodeToString(bImage, false); }
public void dispose() { getEscherGraphics().dispose(); getG2D().dispose(); getImg().flush(); }
@Override public void open() { if (!open.compareAndSet(false, true)) { return; } LOG.debug("Opening GStreamer device"); init(); starting.set(true); Dimension size = getResolution(); image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); image.setAccelerationPriority(0); image.flush(); if (caps != null) { caps.dispose(); } caps = Caps.fromString(String.format("%s,framerate=30/1,width=%d,height=%d", format, size.width, size.height)); filter.setCaps(caps); LOG.debug("Using filter caps: {}", caps); pipelinePlay(); LOG.debug("Wait for device to be ready"); // wait max 20s for image to appear synchronized (this) { try { this.wait(20000); } catch (InterruptedException e) { return; } } }
image.flush();
resizedImage.flush();
src.flush();
src.flush();
g.drawImage(img, 0, 0, null); g.dispose(); img.flush(); img = argbImg;
@Override public void out(OutputStream os) { try { GifEncoder gifEncoder = new GifEncoder(); // gif编码类,这个利用了洋人写的编码类,所有类都在附件中 // 生成字符 gifEncoder.start(os); gifEncoder.setQuality(180); gifEncoder.setDelay(100); gifEncoder.setRepeat(0); BufferedImage frame; char[] rands = alphas(); Color[] fontcolor = new Color[len]; for (int i = 0; i < len; i++) { fontcolor[i] = new Color(20 + num(110), 20 + num(110), 20 + num(110)); } for (int i = 0; i < len; i++) { frame = graphicsImage(fontcolor, rands, i); gifEncoder.addFrame(frame); frame.flush(); } gifEncoder.finish(); } finally { try { os.close(); } catch (IOException e) { log.error(e.getMessage()); } } }
private static BufferedImage createImage(Iterable<?> objects, Visualizer visualizer, Rectangle2D bounds, Color backgroundColor) { final int width = (int) Math.round(bounds.getWidth()); final int height = (int) Math.round(bounds.getHeight()); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = img.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (backgroundColor != null) { g.setColor(backgroundColor); g.fillRect(0, 0, width, height); } g.translate(-bounds.getX(), -bounds.getY()); visualizer.draw(g, objects); img.flush(); return img; }
public BufferedImage asImage() { Rect bounds = getBounds(); BufferedImage img = new BufferedImage((int) Math.round(bounds.getWidth()), (int) Math.round(bounds.getHeight()), BufferedImage.TYPE_INT_ARGB); Graphics2D g = img.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.translate(-bounds.getX(), -bounds.getY()); draw(g); img.flush(); return img; }
/** * Provides a hint that this {@link ImageWorker} will no longer be accessed from a reference in * user space. The results are equivalent to those that occur when the program loses its last * reference to this image, the garbage collector discovers this, and finalize is called. This * can be used as a hint in situations where waiting for garbage collection would be overly * conservative. * * <p>Mind, this also results in disposing the JAI Image chain attached to the image the worker * is applied to, so don't call this method on image changes (full/partial) that you want to * use. * * <p>{@link ImageWorker} defines this method to remove the image being disposed from the list * of sinks in all of its source images. The results of referencing an {@link ImageWorker} after * a call to dispose() are undefined. */ public final void dispose() { if (commonHints != null) { this.commonHints.clear(); } this.commonHints = null; this.roi = null; if (this.image instanceof PlanarImage) { ImageUtilities.disposePlanarImageChain(PlanarImage.wrapRenderedImage(image)); } else if (this.image instanceof BufferedImage) { ((BufferedImage) this.image).flush(); this.image = null; } }
frame.flush();