0

I have never worked with pictures in java and I am a beginner in this. I need to make a function that will crop the images according to a certain ratio of width and height in the middle of the image.

enter image description here

Through REST Api I receive a MultipartFile which I pass to the image cropping function. I forward the image using file.getBytes().

Here is how I wrote the code for image crop function:

public static byte[] cropImage(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        try {
            BufferedImage img = ImageIO.read(bais);
            int width = img.getWidth();
            int height = img.getHeight();
            float aspectRatio = (float) 275 / (float) 160;
            int destWidth;
            int destHeight;
            int startX;
            int startY;

            if(width/height > aspectRatio) {
                destHeight = height;
                destWidth = Math.round(aspectRatio * height);
                startX = Math.round(( width - destWidth ) / 2);
                startY = 0;
            } else if (width/height < aspectRatio) {
                destWidth = width;
                destHeight = Math.round(width / aspectRatio);
                startX = 0;
                startY = Math.round((height - destHeight) / 2);
            } else {
                destWidth = width;
                destHeight = height;
                startX = 0;
                startY = 0;
            }
            BufferedImage dst = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_ARGB);
            dst.getGraphics().drawImage(img, 0, 0, destWidth, destHeight, startX, startY, startX + destWidth, startY + destHeight, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(dst, "png", baos);
            return baos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("IOException in scale");
        }
    }

But when I crop the image the result is an image with a much larger size than the received image. I need help on how to solve this problem.

EDIT:

According to this answer, the size increases on this part of the code:

ImageIO.read (bais)

Is there any other way to convert an image from byte array to buffered image but keep the size of the original image?

Ante Ereš
  • 623
  • 4
  • 8
  • 24
  • 1
    I wonder if there's a characteristic of the resulting image that is different from the original. For example the number of bits per color. One approach might be to use a standard tool and do the same resizing then compare the characteristics of their resulting image and your resulting image. Gimp or imagemagick might be good for this. – Don Branson May 03 '21 at 12:22
  • 2
    width / height is an integer division, use float or double variables instead – Rocco May 03 '21 at 12:23
  • 1
    and, adding to @DonBranson comment, AFAIK you're using default (i.e. medium) png compression, don't know if and how much it affects the result, but some useful information in this old answer: https://stackoverflow.com/questions/2721303/how-to-compress-a-png-image-using-java – Rocco May 03 '21 at 12:44
  • 1
    First of all, it would help the question if you made it clear that you are talking about *file* size, not image "size" (ie. image dimensions). Second, in-memory representation of an image is not (directly) related to file size, so the linked answer/code isn't that relevant. Finally, the format you have chosen to store your images is PNG. If your inputs are "natural images" (ie. photos) in JPEG format, a size increase should be expected, as the compression is much less efficient for such images (but lossless). You question should specify the input format and an example image. – Harald K May 03 '21 at 14:26
  • PS: You might want to have a look at `BufferedImage.getSubimage(x, y, w, h)`. – Harald K May 03 '21 at 14:31

1 Answers1

0

I don’t know why, but the problem was in part ImageIO.write(dst, "png", baos);

I was trying with different types of images (png, jpg, jpeg) and only with png did it reduce my image size. While in the situation when I changed to jpeg it reduced the size in all images.

Ante Ereš
  • 623
  • 4
  • 8
  • 24