Java 使BuffereImage变小的代码出错

Java 使BuffereImage变小的代码出错,java,image,image-processing,bufferedimage,Java,Image,Image Processing,Bufferedimage,我有一个图像(147KB),我想把它缩小到100KB以下。下面的代码试图做到这一点,但是当图像在另一侧显示时,宽度和高度会缩小,但是图像上的磁盘空间会从147变为250!它应该变小而不是变高 你能告诉我为什么这个代码没有这样做吗 谢谢 //Save image BufferedImage resizeImagePng = resizeImage(originalImage, type, newLargeImageLocation); //Resize and save

我有一个图像(147KB),我想把它缩小到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;
        }

2个问题,我也有一个调整大小的程序,但我调整图像的大小有点不同:

首先,我从一个名为“原始”的BuffereImage对象开始创建一个Dimension对象(实际上不需要,但从设计角度看似乎更好)

final int width = original.getWidth();
final int height = original.getHeight();
final Dimension d = new Dimension(width, height);
在我的例子中,它不是关于文件大小,而是关于最大宽度和/或高度,因此在不深入讨论细节的情况下,我根据上面创建的维度对象计算一个新的维度对象。 新标注对象称为“缩放”

这是我获得新的缩放缓冲图像的方式:

public BufferedImage resizeExact(final BufferedImage original, final Dimension scaled, final Dimension offset) {
    final Image newImage = original.getScaledInstance(scaled.width, scaled.height, Image.SCALE_SMOOTH);
    final BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null),
                                                          newImage.getHeight(null),
                                                          BufferedImage.TYPE_INT_BGR);
    bufferedImage.createGraphics().drawImage(newImage, offset.width, offset.height, null);
    return bufferedImage;
}
使用此代码,我获得了一个名为resizedImage的BuffereImage,需要将其写入OutputStream out,为此,我使用以下代码:

ImageIO.write(resized, "jpg", out);
out.close();
这里我讲的是第二个问题:我使用标准ImageIO类将BuffereImage作为jpeg文件写入。这里的问题是,JPEG编码器通常使用压缩因子,该因子越高,生成的文件越小,但生成的文件的质量损失越大。该代码使用70%的默认压缩因子。这很适合我。但是,如果您想更改此设置,此参考说明了如何执行此操作:。请注意,这里将其更改为100%基本上是一个较低的压缩系数

您的代码片段没有显示最终如何创建jpeg文件(我假设您也使用jpeg)。如果你的原始图像被压缩了,比如说压缩率为40%(不太可能,因为我假设你得到的图像很糟糕),那么你有100%的未压缩图像,如果你想将文件大小减少到80%,你就将图像大小减少到80%,如果你用70%的压缩因子压缩,你的最终结果将是56%,比40%大


希望这有帮助。但是,如果您指定如何编写jeg图像(可能使用javax.imageio标准之外的另一个库),则可能有另一种方法指定压缩因子。

保存的大小主要与用于保存的图像格式和设置有关。您可能正在阅读JPG并以其他格式保存吗?
downMult
甚至应该更像
downMult=Math.sqrt(最大图像大小,单位为KYLOBITES/kbytes)
但当然不能保证低于MAX_IMAGE_SIZE_(单位:KB)。请尝试在不重新缩放的情况下保存。
png
也支持多种模式。使用
buffereImage进行比较。键入_INT_ARGB
将生成具有24位颜色和8位alpha通道的PNG。除非您的图像已经是这种格式,否则很可能会因此而增加文件大小。此外,调整大小所引入的伪影可能会降低PNG压缩的效率,实际压缩比会因图像而异。