3

I'm using a RenderedImage to display tiffs in a DisplayJAI in my app.

Somebody know how to resize an instance of RenderedImage??

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
VictorSB
  • 128
  • 1
  • 6

3 Answers3

3

It's been a long time since I've done anything with JAI, but wouldn't the "Scale" or "Affine" operations suffice?

Edit: Here are a couple of links into the Programmer's Guide for "Scale" and "Affine".

kschneid
  • 5,626
  • 23
  • 31
3
public static RenderedImage scale(RenderedImage image, float scaleFactor)
    RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
      RenderingHints.VALUE_RENDER_QUALITY);

    RenderedOp resizeOp = SubsampleAverageDescriptor.create(image,
      Double.valueOf(scaleFactor), Double.valueOf(scaleFactor), hints);

    BufferedImage bufferedResizedImage = resizeOp.getAsBufferedImage();

    return bufferedResizedImage;
}
Pops
  • 30,199
  • 37
  • 136
  • 151
gresli
  • 41
  • 2
-1

There is an example code posted here to just that: http://answers.yahoo.com/question/index?qid=20090827075608AA12kEZ

Relevant code:

BufferedImage img = ImageIO.read(new File("~/your/file/system/example.jpeg"));
BufferedImage thumb = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
thumb.createGraphics().drawImage(
img.getScaledInstance(w2, h2, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
File file = new File(fullpath + filename);
ImageIO.write(thumb, "png", file);
Community
  • 1
  • 1
jzd
  • 23,473
  • 9
  • 54
  • 76
  • A RenderedImage can not be converted to a BufferedImage by a simple type cast. – Robert Jan 04 '11 at 12:55
  • @Robert, I should have tested it, instead of assuming the poster was correct. However, the rest might still be useful. – jzd Jan 04 '11 at 12:58
  • @Robert. Huh? The JavaDocs for BufferedImage claim "All Implemented Interfaces: *RenderedImage,* WritableRenderedImage, Transparency". Have I misunderstood something? – Andrew Thompson Jan 04 '11 at 13:45
  • 2
    Yes BufferedImage is a RenderedImage but not necessarily the other way around. – jzd Jan 04 '11 at 13:48
  • 1
    @Andrew: Well that may be true for the plain JRE but if you have for example Java Advanced Imaging (JAI) installed you have dozen of other implementations (e.g. PlanarImage). Furthermore VictorSB explicitly asked for RenderedImage in the context of JAI. – Robert Jan 04 '11 at 14:14