0

Say I have 2 TIF images and I read one of them into a BufferedImage instance:

ImageReader reader = ImageIO.getImageReadersByFormatName("tif").next();
reader.setInput(inputStream, false);   // inputStream is the first image.
BufferedImage bufferedImage = reader.read(0);

Now I want to create a new BufferedImage without reading the other image. It should be the same as the previous one, but only different in size. imageType seems to be 0 for TIF images, but the following doesn't work.

BufferedImage largeBufferedImage = new BufferedImage(newWidth, newHeight, 0);

Is there any way to clone the existing BufferedImage and only change its size?

BTW I want to be able to do it for any image format. I don't want to deal with details like imageType if possible.

Utku Ufuk
  • 318
  • 1
  • 10
  • 24

3 Answers3

1
BufferedImage deepCopy(BufferedImage bi)/*method to clone BufferedImage*/ {
   ColorModel cm = bi.getColorModel();
   boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
   WritableRaster raster = bi.copyData(null);
   return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
BufferedImage newImg = deepCopy(oldImg);//clone it
Graphics2D g = newImg.createGraphics();
g.drawImage(newImg, 0, 0, width, height, null);//newImg will be resized
Jeremy
  • 776
  • 1
  • 7
  • 18
  • Won't it change my original `bufferedImage`? I don't want to change it, I want to copy it. – Utku Ufuk Jan 20 '15 at 13:37
  • I couldn't understand the difference between `bufferedImage` and `originalImage`. If they are the same thing, it doesn't work. – Utku Ufuk Jan 20 '15 at 14:05
  • Sorry, I made few mistakes. I fixed all of it. – Jeremy Jan 20 '15 at 14:21
  • nearly all - just a missing bracket in your first line (it has moved into the comment ^^) – Martin Frank Jan 20 '15 at 14:24
  • @Jeremy That didn't exactly work, but I found a solution using some of your code. So I upvoted your answer :) The problem is you have to change the raster to resize the image. `drawImage()` won't do the trick... – Utku Ufuk Jan 20 '15 at 15:30
0

When you draw in your paint method, you can add more parameters to stretch and scale image, see g.drawImage at this link.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

After some trial and error, I found a working solution for my problem.

private BufferedImage copyAndResize(BufferedImage source, int width, int height)
{
    ColorModel cm = source.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = source.copyData(null);
    SampleModel sm = raster.getSampleModel().createCompatibleSampleModel(width, height);
    WritableRaster newRaster = WritableRaster.createWritableRaster(sm, null);
    BufferedImage newBi = new BufferedImage(cm, newRaster, isAlphaPremultiplied, null);
    return newBi;
}
Utku Ufuk
  • 318
  • 1
  • 10
  • 24
  • This is far more complicated than what you actually need. Take a look at the following article which describes several options: https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html – Brett Okken Jan 20 '15 at 17:46
  • @BrettOkken Why is my code complicated? You should know that I just want to get a `BufferedImage` instance to write a new image. I don't care about its content/quality at all, I only care about its size and properties. – Utku Ufuk Jan 21 '15 at 07:51