Two possible solutions are zooming out, here's how you do it:
BufferedImage original = //your image here scaled = original.getScaledInstance(finalWidth, finalHeight, Image.SCALE_SMOOTH); // scale the image to a smaller one BufferedImage result = new BufferedImage(finalWidth, finalHeight, original.getType()); Graphics2D g = result.createGraphics(); g.drawImage(scaled, 0, 0, null); //draw the smaller image g.dispose();
Obviously, you need to calculate the scaled width and height so that the image remains at the same aspect ratio.
Once you draw it less, now you can turn this image into a JPEG file:
BufferedImage image = // this is the final scaled down image JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(output); JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); jpegEncodeParam.setXDensity(92); jpegEncodeParam.setYDensity(92); jpegEncodeParam.setQuality( 0.8F , false); jpegEncoder.encode(image, jpegEncodeParam);
These classes belong to the JAI package (more precisely, com.sun.image.codec.jpeg ), and the JVM may complain that they should not be used directly, but you can ignore it.
You can download JAI from here if it doesn’t work, I have github mirrors setup for two libraries, JAI core and JAI ImageIO .
Maurício Linhares
source share