0

Here are my two implementations. Would both give the same result? I know that BufferedImage is the child class of the Image.

  1. First implementation: writeImage takes an Image Object and uses RenderedImage in the ImageIO.write method.

    public void writeImage(Image img, String outputFile) {                  
        try {
            ImageIO.write((RenderedImage) img, "jpg", new File(outputFile));
        } catch (IOException e) {
        }
        ...
    
  2. Second Implementation: writeImage takes a BufferedImage`` object and uses the BufferedImage object in theImageIO.write method.

    public void writeImage(BufferedImage img, String outputFile){
        try {
            ImageIO.write(img, "jpg", new File(outputFile));
        } catch (IOException e) {
        }
        ...
    ...
    

Also, try to tell what's the difference between the two ways of writing an image.

Teocci
  • 7,189
  • 1
  • 50
  • 48
  • First example will fail if the `Image` isn't an instance of `RenderedImage`, so I'd say your answer is "no" – MadProgrammer Mar 25 '17 at 01:07
  • possible duplicate: http://stackoverflow.com/q/11810370/3858121 – Japu_D_Cret Mar 25 '17 at 01:08
  • `BufferedImage` is a descendant of `Image`, which also implements the `RenderedImage` and `WritableRenderedImage` interfaces, so I can be used any where those requirements are need. `Image` extends from `Object` and implements nothing, so you can't guarantee it won't fail in your first example – MadProgrammer Mar 25 '17 at 01:09
  • @MadProgrammer do you think the 2nd one will work? – Shubham Jayswal Mar 25 '17 at 01:14
  • @ShubhamJayswal Considering the fact that `BufferedImage` implements `RenderedImage` and basically, that's how I've been using it for over six years, yes – MadProgrammer Mar 25 '17 at 01:19
  • I think this question has [been already solve](http://stackoverflow.com/a/11810385/5372008). As you know `Image` is an abstract class and `BufferedImage` is its subclass, wich describes an `Image` class with an accessible buffer of image data. – Teocci Apr 22 '17 at 11:38

0 Answers0