/** * Loads an image from a URL * * @param bitmapFile the bitmap file * @return the bitmap as BufferedImage */ public static BufferedImage getImage(File bitmapFile) { try { InputStream in = new java.io.FileInputStream(bitmapFile); try { in = new java.io.BufferedInputStream(in); ImageTagRegistry reg = ImageTagRegistry.getRegistry(); Filter filt = reg.readStream(in); if (filt == null) { return null; } RenderedImage red = filt.createDefaultRendering(); if (red == null) { return null; } BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB); red.copyData(img.getRaster()); return img; } finally { IOUtils.closeQuietly(in); } } catch (IOException e) { return null; } }
/** * Loads an image from a URL * * @param url the URL to the image * @return the bitmap as BufferedImage TODO This method doesn't close the InputStream opened by * the URL. */ public static BufferedImage getImage(URL url) { ImageTagRegistry reg = ImageTagRegistry.getRegistry(); Filter filt = reg.readURL(new ParsedURL(url)); if (filt == null) { return null; } RenderedImage red = filt.createDefaultRendering(); if (red == null) { return null; } BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB); red.copyData(img.getRaster()); return img; }
/** * Converts a rendered image to a {@code BufferedImage}. This utility method has come from a forum * post by Jim Moore at: * * <p><a href="http://www.jguru.com/faq/view.jsp?EID=114602"> * http://www.jguru.com/faq/view.jsp?EID=114602</a> * * @param img the rendered image. * @return A buffered image. */ private static BufferedImage convertRenderedImage(RenderedImage img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys != null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); return result; }
/** * Returns a contiguous <code>Raster</code> of data over the specified <code>Rectangle</code>. If * the region is a sub-region of a single tile, then a child of that tile will be returned. If the * region overlaps more than one tile and has 8 bits per sample, then a pixel interleaved Raster * having band offsets 0,1,... will be returned. Otherwise the Raster returned by <code> * im.copyData(null)</code> will be returned. */ private static final Raster getContiguousData(RenderedImage im, Rectangle region) { if (im == null) { throw new IllegalArgumentException("im == null"); } else if (region == null) { throw new IllegalArgumentException("region == null"); } Raster raster; if (im.getNumXTiles() == 1 && im.getNumYTiles() == 1) { // Image is not tiled so just get a reference to the tile. raster = im.getTile(im.getMinTileX(), im.getMinTileY()); // Ensure result has requested coverage. Rectangle bounds = raster.getBounds(); if (!bounds.equals(region)) { raster = raster.createChild( region.x, region.y, region.width, region.height, region.x, region.y, null); } } else { // Image is tiled. // Create an interleaved raster for copying for 8-bit case. // This ensures that for RGB data the band offsets are {0,1,2}. SampleModel sampleModel = im.getSampleModel(); WritableRaster target = sampleModel.getSampleSize(0) == 8 ? Raster.createInterleavedRaster( DataBuffer.TYPE_BYTE, im.getWidth(), im.getHeight(), sampleModel.getNumBands(), new Point(im.getMinX(), im.getMinY())) : null; // Copy the data. raster = im.copyData(target); } return raster; }
/** @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform) */ public void drawRenderedImage(RenderedImage img, AffineTransform xform) { BufferedImage image = null; if (img instanceof BufferedImage) { image = (BufferedImage) img; } else { ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys != null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); image = result; } drawImage(image, xform, null); }