我有一张图片 (147 KB),我想将其缩小到 100KB 以下。下面的代码尝试这样做,但是当图像从另一侧出来时,宽度和高度会按比例缩小,但图像上的磁盘空间会从 147 变为 250!它应该变得更小而不是更高......
你能告诉我为什么这段代码没有这样做吗?
谢谢
//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;
}