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));
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(); } }
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 ImageInfo(PixelGrabber pixelGrabber) { this.pixelGrabber = pixelGrabber; this.width = pixelGrabber.getWidth(); this.height = pixelGrabber.getHeight(); this.widthInTwips = this.width * ISWFConstants.TWIPS_PER_PIXEL; this.heightInTwips = this.height * ISWFConstants.TWIPS_PER_PIXEL; }
public ImageInfo(PixelGrabber pixelGrabber) { this.pixelGrabber = pixelGrabber; this.width = pixelGrabber.getWidth(); this.height = pixelGrabber.getHeight(); this.widthInTwips = this.width * ISWFConstants.TWIPS_PER_PIXEL; this.heightInTwips = this.height * ISWFConstants.TWIPS_PER_PIXEL; }
public ImageInfo(PixelGrabber pixelGrabber) { this.pixelGrabber = pixelGrabber; this.width = pixelGrabber.getWidth(); this.height = pixelGrabber.getHeight(); this.widthInTwips = this.width * ISWFConstants.TWIPS_PER_PIXEL; this.heightInTwips = this.height * ISWFConstants.TWIPS_PER_PIXEL; }
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();
/** * 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; }
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]; }
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]; }
/** 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); }
System.out.println("Grabber interrupted by anohter thread!\n"); wh.width = grabber.getWidth(); wh.height = grabber.getHeight(); return (int[]) (grabber.getPixels());
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; } }
throw new IOException(MessageLocalization.getComposedMessage("java.awt.image.fetch.aborted.or.errored")); int w = pg.getWidth(); int h = pg.getHeight(); PdfTemplate tp = cb.createTemplate(w, h);