private static final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF}; private static final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]); // ... String sUrl="http://img01.taobaocdn.com/imgextra/i1/449400070/T2hbVwXj0XXXXXXXXX_!!449400070.jpg"; URL url = new URL(sUrl); Image img = Toolkit.getDefaultToolkit().createImage(url); PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); pg.grabPixels(); int width = pg.getWidth(), height = pg.getHeight(); DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight()); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null); BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null); String to = "D:/temp/result.jpg"; ImageIO.write(bi, "jpg", new File(to));
DirectGif89Frame(Image img) throws IOException { PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); String errmsg = null; try { if (!pg.grabPixels()) { errmsg = "can't grab pixels from image"; } } catch (InterruptedException e) { errmsg = "interrupted grabbing pixels from image"; } if (errmsg != null) { throw new IOException(errmsg + " (" + getClass().getName() + ")"); } theWidth = pg.getWidth(); theHeight = pg.getHeight(); argbPixels = (int[]) pg.getPixels(); ciPixels = new byte[argbPixels.length]; }
private static final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF}; private static final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]); ... BufferedImage image = componentToImage(imagePanel, true); saveJPeg(image, "c:/saved.jpg"); private void saveJPeg(BufferedImage image, String name) { PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true); try { pg.grabPixels(); } catch (InterruptedException ex) { ex.printStackTrace(); } int width = pg.getWidth(), height = pg.getHeight(); DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight()); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null); BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null); try { ImageIO.write(bi, "jpg", new File(name)); } catch (IOException ex) { ex.printStackTrace(); } }
PixelGrabber pg = new PixelGrabber(imageIcon.getImage(),0,0, w,h,true); pg.grabPixels(); // You may need to wait here until all pixels are copied (see ImageObserver) Object buffer = pg.getPixels();
/** Returns the array of pixels, whose type depends on the bi.getType(); for example, for a BufferedImage.TYPE_BYTE_INDEXED, returns a byte[]. */ static public final Object grabPixels(final BufferedImage bi) { final PixelGrabber pg = new PixelGrabber(bi, 0, 0, bi.getWidth(), bi.getHeight(), false); try { pg.grabPixels(); return pg.getPixels(); } catch (InterruptedException e) { IJError.print(e); } return null; }
Image img = Toolkit.getDefaultToolkit().createImage(filename); PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, false); pg.grabPixels(); // Throws InterruptedException width = pg.getWidth(); height = pg.getHeight(); int[] pixels = (int[])pg.getPixels();
public Buffer getData(Image image, Buffer optionalExistingOutput, boolean directBuffer, boolean allowDeoptimizingDirectRead, ByteOrder byteOrder) { int[] intData = null; int width = image.getWidth(null), height = image.getHeight(null); PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, true); try { grabber.grabPixels(); intData = (int[])grabber.getPixels(); } catch (InterruptedException ex) { throw new RuntimeException("Pixel read operation was interrupted", ex); } IntBuffer output = IntBuffer.wrap(intData); if (directBuffer) return NIOUtils.directCopy(output, byteOrder); else return output; } },
DirectGif89Frame(Image img) throws IOException { PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); String errmsg = null; try { if (!pg.grabPixels()) errmsg = "can't grab pixels from image"; } catch (InterruptedException e) { errmsg = "interrupted grabbing pixels from image"; } if (errmsg != null) throw new IOException(errmsg + " (" + getClass().getName() + ")"); theWidth = pg.getWidth(); theHeight = pg.getHeight(); argbPixels = (int[]) pg.getPixels(); ciPixels = new byte[argbPixels.length]; }
public Buffer getData(Image image, Buffer optionalExistingOutput, boolean directBuffer, boolean allowDeoptimizingDirectRead, ByteOrder byteOrder) { int[] intData = null; int width = image.getWidth(null), height = image.getHeight(null); PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, true); try { grabber.grabPixels(); intData = (int[])grabber.getPixels(); } catch (InterruptedException ex) { throw new RuntimeException("Pixel read operation was interrupted", ex); } IntBuffer output = IntBuffer.wrap(intData); if (directBuffer) return NIOUtils.directCopy(output, byteOrder); else return output; } },
DirectGif89Frame(Image img) throws IOException { PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); String errmsg = null; try { if (!pg.grabPixels()) { errmsg = "can't grab pixels from image"; } } catch (InterruptedException e) { errmsg = "interrupted grabbing pixels from image"; } if (errmsg != null) { throw new IOException(errmsg + " (" + getClass().getName() + ")"); } theWidth = pg.getWidth(); theHeight = pg.getHeight(); argbPixels = (int[]) pg.getPixels(); ciPixels = new byte[argbPixels.length]; }
public static int[] getImageIntPixels(Image image, int x, int y, int width, int height, boolean allowDeoptimizingDirectRead) { if (image instanceof BufferedImage) { BufferedImage bim = (BufferedImage)image; WritableRaster raster = bim.getRaster(); if (allowDeoptimizingDirectRead && raster.getParent() == null && raster.getDataBuffer().getNumBanks() == 1) { DataBuffer b = bim.getRaster().getDataBuffer(); if (b instanceof DataBufferInt) { int[] array = ((DataBufferInt)b).getData(); return array; } } return bim.getRGB(x, y, width, height, null, 0, width); } PixelGrabber grabber = new PixelGrabber(image, x, y, width, height, true); try { grabber.grabPixels(); return (int[])grabber.getPixels(); } catch (InterruptedException ex) { throw new RuntimeException("Pixel read operation was interrupted", ex); } }
/** Construct an DirectGif89Frame from a Java image. * * @param img * A java.awt.Image object that supports pixel-grabbing. * @exception IOException * If the image is unencodable due to failure of pixel-grabbing. */ public DirectGif89Frame(Image img) throws IOException { PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); String errmsg = null; try { if (!pg.grabPixels()) errmsg = "can't grab pixels from image"; } catch (InterruptedException e) { errmsg = "interrupted grabbing pixels from image"; } if (errmsg != null) throw new IOException(errmsg + " (" + getClass().getName() + ")"); theWidth = pg.getWidth(); theHeight = pg.getHeight(); argbPixels = (int[]) pg.getPixels(); ciPixels = new byte[argbPixels.length]; }
public ImageFunction2D(Image image, int edgeAction, boolean alpha) { PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, null, 0, -1); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("interrupted waiting for pixels!"); } if ((pg.status() & ImageObserver.ABORT) != 0) { throw new RuntimeException("image fetch aborted"); } init((int[])pg.getPixels(), pg.getWidth(), pg.getHeight(), edgeAction, alpha); }
public ImageFunction2D(Image image, int edgeAction, boolean alpha) { PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, null, 0, -1); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("interrupted waiting for pixels!"); } if ((pg.status() & ImageObserver.ABORT) != 0) { throw new RuntimeException("image fetch aborted"); } init((int[])pg.getPixels(), pg.getWidth(), pg.getHeight(), edgeAction, alpha); }
public ImageFunction2D(Image image, int edgeAction, boolean alpha) { PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, null, 0, -1); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("interrupted waiting for pixels!"); } if ((pg.status() & ImageObserver.ABORT) != 0) { throw new RuntimeException("image fetch aborted"); } init((int[])pg.getPixels(), pg.getWidth(), pg.getHeight(), edgeAction, alpha); }
public ImageFunction2D(Image image, int edgeAction, boolean alpha) { PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, null, 0, -1); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("interrupted waiting for pixels!"); } if ((pg.status() & ImageObserver.ABORT) != 0) { throw new RuntimeException("image fetch aborted"); } init((int[])pg.getPixels(), pg.getWidth(), pg.getHeight(), edgeAction, alpha); }
/** * Cretae a BufferedImage from an ImageProducer. * @param producer the ImageProducer * @return a new TYPE_INT_ARGB BufferedImage */ public static BufferedImage createImage(ImageProducer producer) { PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Image fetch interrupted"); } if ((pg.status() & ImageObserver.ABORT) != 0) throw new RuntimeException("Image fetch aborted"); if ((pg.status() & ImageObserver.ERROR) != 0) throw new RuntimeException("Image fetch error"); BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB); p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth()); return p; }
/** * Cretae a BufferedImage from an ImageProducer. * @param producer the ImageProducer * @return a new TYPE_INT_ARGB BufferedImage */ public static BufferedImage createImage(ImageProducer producer) { PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Image fetch interrupted"); } if ((pg.status() & ImageObserver.ABORT) != 0) throw new RuntimeException("Image fetch aborted"); if ((pg.status() & ImageObserver.ERROR) != 0) throw new RuntimeException("Image fetch error"); BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB); p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth()); return p; }
/** * Cretae a BufferedImage from an ImageProducer. * @param producer the ImageProducer * @return a new TYPE_INT_ARGB BufferedImage */ public static BufferedImage createImage(ImageProducer producer) { PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0); try { pg.grabPixels(); } catch (InterruptedException e) { throw new RuntimeException("Image fetch interrupted"); } if ((pg.status() & ImageObserver.ABORT) != 0) throw new RuntimeException("Image fetch aborted"); if ((pg.status() & ImageObserver.ERROR) != 0) throw new RuntimeException("Image fetch error"); BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB); p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth()); return p; }
public static BufferedImage toBufferedImage(final Image image) throws InterruptedException { if (image instanceof BufferedImage) return (BufferedImage) image; MediaTracker tracker = new MediaTracker(new Component() { }); tracker.addImage(image, 0); tracker.waitForAll(); PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, false); pixelGrabber.grabPixels(); ColorModel cm = pixelGrabber.getColorModel(); final int w = pixelGrabber.getWidth(); final int h = pixelGrabber.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(w, h); final BufferedImage renderedImage = new BufferedImage(cm, raster, cm .isAlphaPremultiplied(), new Hashtable()); renderedImage.getRaster().setDataElements(0, 0, w, h, pixelGrabber.getPixels()); return renderedImage; } }