0

I have a program that sends the image from frontend (angularjs) to java controller. In controller I am geting a byte array. I can save this image but I would like to resize this image before I saveing. The problem is that I want to set fixed height of the picture, and the change of width should take place proportionately to the height. This procedure should be universal so that it can be applicable to different photos.

Below is my code:

@RequestMapping(value = "/rest/bookImage", method = RequestMethod.POST)
    public @ResponseBody MessageDTO UploadFile(
            MultipartHttpServletRequest request, HttpServletResponse response) {

        Iterator<String> itr = request.getFileNames();
        MultipartFile file = request.getFile(itr.next());
        FileOutputStream fos;
        fos = new FileOutputStream(urlImage);
        fos.write(file.getBytes());
            fos.close();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2590727
  • 441
  • 1
  • 6
  • 25
  • Don't fix any dimension while resizing an image cause this will break the aspect ratio. Then your image may not look nice as original image. – Razib Mar 07 '15 at 09:57
  • Where possible avoid getScaledInstance, for a different approach try having a look [at this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) – MadProgrammer Mar 07 '15 at 11:36

1 Answers1

1

Since you have the image already, you can use the getScaledInstance function:

yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

You say you want the width to be proportionate to the height, by choosing if you want it to be double or whatever (just set the appropriate height and width respectively!)

See more info here : http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html

public Image getScaledInstance(int width,
                      int height,
                      int hints)

Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.

Parameters: width - the width to which to scale the image. height - the height to which to scale the image. hints - flags to indicate the type of algorithm to use for image resampling. Returns: a scaled version of the image.

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • 1
    See also [The Perils of Image.getScaledInstance()](https://today.java.net/article/2007/03/30/perils-imagegetscaledinstance). – Andrew Thompson Mar 07 '15 at 10:41
  • 1
    pretty cool. didn't know that. If I understand correctly it is better to use Graphics.drawImage() ? – adrCoder Mar 07 '15 at 10:47
  • *"it is better to use Graphics.drawImage()"* Yes, that is the route I usually use. For one thing, we can set the image resizing hints (presuming we use a `Graphics2D` object). – Andrew Thompson Mar 07 '15 at 10:49
  • 1
    Nice. I gave you a +1 for your CardLayout answer :) – adrCoder Mar 07 '15 at 10:51
  • Lol ok, I thought you had to do it tens of times for crappy answers to the same person to have that problem :D I don't think you have a problem with that cause you already got like 120k reputation :D – adrCoder Mar 07 '15 at 10:57