Ejemplo n.º 1
0
  /**
   * 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;
    }
  }
Ejemplo n.º 2
0
  /**
   * 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;
  }
Ejemplo n.º 3
0
  /**
   * Returns a Filter that represents an raster image (JPG or PNG).
   *
   * @param ctx the bridge context
   * @param primitiveRegion the primitive region
   * @param purl the url of the image
   */
  protected static Filter createRasterFeImage(
      BridgeContext ctx, Rectangle2D primitiveRegion, ParsedURL purl) {

    // Need to fit the raster image to the filter region so that
    // we have the same behavior as raster images in the <image> element.
    Filter filter = ImageTagRegistry.getRegistry().readURL(purl);

    Rectangle2D bounds = filter.getBounds2D();
    AffineTransform scale = new AffineTransform();
    scale.translate(primitiveRegion.getX(), primitiveRegion.getY());
    scale.scale(
        primitiveRegion.getWidth() / (bounds.getWidth() - 1),
        primitiveRegion.getHeight() / (bounds.getHeight() - 1));
    scale.translate(-bounds.getX(), -bounds.getY());

    return new AffineRable8Bit(filter, scale);
  }