-2

I need to scale down an JPG image so that it fits in a 1024x640 rectangle and save it so that it's size is no more than 20 kB. With some googling, I managed to do both. For the scaling I use

private static BufferedImage scale(BufferedImage input, int width, int height) {
    final int type = input.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    final BufferedImage result = new BufferedImage(width, height, type);
    final Graphics2D g2 = result.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(input, 0, 0, width, height, null);
    g2.dispose();
    return result;
}

and for the size-reduction, I use ImageWriter with JPEGImageWriteParam and repeat with decreased quality until the file size gets below the limit.

It both works rather nicely on my machine, but on the test server, it does terrible things:

original file size:    174548
scaled down file size:  16995 - on my machine
                       228218 - on the server

So the scaled-down image is bigger than the original and the quality is awful.

I though it could be related to the headless java problem, but the java installation is not headless (and I never get an HeadlessException):

java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~16.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

Any idea what's going on?

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • The problem could be a large amount of meta data. I do not know Java, but in command line you can use -strip to remove most if not all meta data. Also in command line, there is a define that allows Imagemagick to output your desired file size. See -define jpeg:extent={size} at https://imagemagick.org/Usage/formats/#jpg_write – fmw42 Oct 26 '19 at 04:10

2 Answers2

0

We used ImageMagick for image resizing and re-formatting. It can give you flexibility. We managed to use it earlier for similar need to as of yours.

ImageMagick utilizes multiple computational threads to increase performance and can read, process, or write mega-, giga-, or tera-pixel image sizes.

It is free an open-source. You can refer official website for more detils : https://imagemagick.org/index.php

Sample program works like as below:

ConvertCmd cmd = new ConvertCmd();

// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage("source_picture.jpg"); // source file
op.resize(800,600);
// of op.resize(800); // and height calculate automatically
op.addImage("resized_picture.jpg"); // destination file file

// execute the operation
cmd.run(op);

Check related stackoverflow thread Image magick java

Hope it helps!

www.hybriscx.com
  • 1,129
  • 4
  • 22
0

The problem was the resizing done using Graphics2D. Not idea why it did what it did, but after replacing it with java-image-scaling, the problem is gone.

It's quite possible that there are better libraries out there, but for current needs this one works good enough.

maaartinus
  • 44,714
  • 32
  • 161
  • 320