I have an image (147 KB), and I want to downsize it to under 100KB. The code below attempts to do this, however when the image comes out on the other side, the widht and height are scaled down, but the disk space on the image goes from 147 to 250! It's supposed to get smaller not higher...
Can you please tell me why this code isn't doing this?
Thanks
//Save image
BufferedImage resizeImagePng = resizeImage(originalImage, type, newLargeImageLocation);
//Resize and save
ImageIO.write(resizeImagePng, "png", new File(newSmallImageLocation));
//Create new image
private static BufferedImage resizeImage(BufferedImage originalImage, int type, String newLargeLocation) throws Exception{
//Get size of image
File file =new File(newLargeLocation);
//File size in KBs
double bytes = file.length();
double kiloBytes = bytes/1024;
double smallWidth = originalImage.getWidth();
double smallHeight = originalImage.getHeight();
double downMult = 1;
//If image is too large downsize to fit the size
if (kiloBytes>MAX_IMAGE_SIZE_IN_KYLOBITES) {
downMult = MAX_IMAGE_SIZE_IN_KYLOBITES/kiloBytes;
smallWidth *= downMult;
smallHeight *= downMult;
}
//Final dimensions
int finalHeight = (int)smallHeight;
int finalWidth = (int)smallWidth;
//Init after
BufferedImage after = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_ARGB);
//Scale
AffineTransform at = new AffineTransform();
at.scale(downMult, downMult);
//Scale op
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
after = scaleOp.filter(originalImage, after);
//Return after
return after;
}