我有一个图像(147 KB),我想缩小到100KB以下。下面的代码试图做到这一点,但是当图像出现在另一侧时,widht和height会缩小,但图像上的磁盘空间从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;
}
你可以在你写图片的地方显示代码吗?也许你正在写的格式不能像原始格式那样压缩。 – dnault
保存的大小主要是用于保存的图像格式和设置。你是否可能阅读JPG并以其他格式保存? – kiheru
刚刚添加了保存代码。文件文件是一个.png结尾的图像 –