4

How to convert Image to BufferedImage in Java?

Note, that existing answer is apparently not correct, because it uses methods getWidth(null) and getHeight(null), which can return -1 if image is not loaded yet (Image is, by definition, an asynchronous object).

Please, provide either more correct answer, or more proofs that existing answer is already correct.

Community
  • 1
  • 1
Dims
  • 47,675
  • 117
  • 331
  • 600
  • You need to create an empty BufferedImage to copy the pixels to. Therefore you will need the dimensions of the original image. – Stefan Sep 22 '15 at 10:43
  • 1
    I can't know the dimensions of original image until it's loaded. – Dims Sep 22 '15 at 10:57
  • o gosh I tried converting two images to bufferedImages and I was getting really close to a tantrum cause I did not understand why one would convert and the other not :) I had to load both before as setIcon and setRolloverIcon to my JButton before altering, that did the trick thanks a lot :) – Georodin May 04 '21 at 17:59

1 Answers1

4

If it's important to you, you can use a MediaTracker to "wait" for the image to be loaded, then you don't need to care about supplying a ImageObserver

try {
    MediaTracker mt = new MediaTracker(new JPanel());
    Image image = Toolkit.getDefaultToolkit().createImage("...");
    mt.addImage(image, 0);
    System.out.println("Wait for...");
    mt.waitForAll();
    System.out.println("I be loaded");

    BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
} catch (InterruptedException ex) {
    ex.printStackTrace();
}

Have a look at MediaTracker JavaDocs for more details

I don't wish to add any GUI, I just need to download image or fail

Okay, if you "need to download" the image, then you can just use ImageIO.read(URL), have a look at Reading/Loading an Image for more details ... then you won't need to care about Image or MediaTracker, as ImageIO returns a BufferedImage

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • What is the purpose of `Component` in `MediaTracker`'s constructor? I don't wish to add any GUI, I just need to download image or fail – Dims Sep 22 '15 at 10:58
  • 1
    The `Component` is acting as the `ImageObserver` for the `MediaTracker`. Okay, if you "need to download" the image, then you can just use `ImageIO.read(URL)`, have a look at [Reading/Loading an Image](https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) for more details ... then you won't need to care about `Image` or `MediaTracker`, as `ImageIO` returns a `BufferedImage` – MadProgrammer Sep 22 '15 at 11:02
  • I don't have URL, I have an image in `Image` instance form – Dims Sep 22 '15 at 12:28
  • 1
    Then you need the `MediaTracker`, the creation of the `Component` won't "start" any UI, it's just using it to facilitate the `ImageObserver` API reqiurements – MadProgrammer Sep 22 '15 at 12:51