0

I have a JPEG Master Image which is of a dimension and an input Jpeg image of another dimension . So , I have written the following code to convert an image from one dimension to the other dimension. This code works fine when the input images are of dimensions around 900 * 700 and the master image of dimension 1200 * 800 . It does not work when the master/ input images are around dimensions like 3400*1900 and 4400*2400. Can anyone tell me where I went wrong!

public void checkSizeandResize(String file1, String file2){

   BufferedImage scaled = null;
   BufferedImage image1 = null;
   BufferedImage image2 = null;
   Graphics2D graphics2d = null;
   try{
    image1 = imageToBufferedImage(loadJPG(file1));
    image2 = imageToBufferedImage(loadJPG(file2));
    if(image1.getHeight()!=image2.getHeight() || image1.getWidth()!=image2.getWidth()){

            scaled = new BufferedImage(image2.getWidth(),image2.getHeight(), image2.getType());
            graphics2d =  scaled.createGraphics();
            graphics2d.setComposite(AlphaComposite.Src);
            graphics2d.drawImage(image1, 0, 0,null);
            graphics2d.dispose();
            ImageIO.write(image1, extension,new File(file2));

        }
    }
    catch(Exception exception)
    {
        exception.printStackTrace();
    }
    finally
    {
        scaled = null;
        image1 = null;
        image2 = null;
        graphics2d = null;
    }
}
byreddy
  • 23
  • 1
  • 3
  • 9
  • 1
    What does the imageToBufferedImage(loadJPG(...)) do? You do realise that this isn't actually scaling anything? Do you get any error output (like maybe out of memory)? – MadProgrammer May 29 '15 at 22:58
  • This method just coverts Image to buffered java.awt.Image like BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); – byreddy May 29 '15 at 23:00
  • Any reason why you can't use ImageIO.read? – MadProgrammer May 29 '15 at 23:01
  • I did not see any error , but It does not convert to the required dimension – byreddy May 29 '15 at 23:02
  • You might like to take a look at [this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer May 29 '15 at 23:03
  • Please clarify "it does not work". It creates an empty image? The image is not resized? – Jongware May 29 '15 at 23:31
  • The image is not Resized. – byreddy May 29 '15 at 23:34

1 Answers1

1

So, I did a quick test (using ImageIO.read as you've not provided all the code) with an image going from 6144x4096 to 7680x4800 without a problem

But I did notice that you are writing image1 instead of scaled

ImageIO.write(image1, extension,new File(file2));

Should probably be

ImageIO.write(scaled, extension,new File(file2));

You should also beware, that this isn't actually "scaling" the image so much as just changing the size of the resulting bitmap (image1 will remain at it's original size pixel size and painted within the scaled boundaries unmodified)

If you're actually interested in changing the physical size of the image to match the corresponding boundaries, you might consider having a look at Java: maintaining aspect ratio of JPanel background image and Quality of Image after resize very low -- Java

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Do you have any idea on how OP's code appeared to work with the smaller images? – Jongware May 29 '15 at 23:33
  • 1
    @Jongware I think "appeared to work" is the phrase there. I had to scratch my head a few times trying to figure out why I wasn't getting the results I was expecting till I spotted the problem :P – MadProgrammer May 29 '15 at 23:34
  • I tried from the first link , It shows error as unsupported operation. scaled = image1.getScaledInstance(image2.getWidth(null),image2.getHeight(null), Image.SCALE_SMOOTH); graphics2d = scaled.getGraphics(); – byreddy May 29 '15 at 23:35
  • @BYKR On which line? There are two there? And you're importing `java.awt.Image`? – MadProgrammer May 29 '15 at 23:38
  • The answer which you gave worked . But you give 2 links in the previous comments . So, I tried using the first link to achieve the task . The first link did not work for me . http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928 – byreddy May 29 '15 at 23:41
  • @BYKR Yes, I understand, but you pasted two lines of code you comment, and I was trying to figure out which one didn't work. The fact that it's not working for you is surprising because I've been using it for 15+ years. – MadProgrammer May 29 '15 at 23:45
  • Forgive me If I am wrong , As I am just a beginner. – byreddy May 29 '15 at 23:57
  • Image scaled = null; Graphics graphics2d = null; Image image1 = loadJPG(file1); Image image2 = loadJPG(file2); scaled = image1.getScaledInstance(image2.getWidth(null),image2.getHeight(null), Image.SCALE_SMOOTH); graphics2d = scaled.getGraphics(); graphics2d.drawImage(scaled, image2.getWidth(null),image2.getHeight(null),null); throws java.lang.UnsupportedOperationException: getGraphics() not valid for images created with createImage(producer) . I have to work without using applets. – byreddy May 29 '15 at 23:58
  • Well, if you're able to use `ImageIO.write`, why not use `ImageIO.read` instead? – MadProgrammer May 30 '15 at 00:21
  • Finally after using graphics2d.drawImage(image1, 0, 0,image2.getWidth(),image2.getHeight(),null); It worked. Thank you – byreddy May 30 '15 at 05:52