Answer the question
In order to leave comments, you need to log in
Why did the image size decrease during overwriting?
I uploaded a file, converted it to an array of bytes and simply wrote it back. In theory, the file size should have remained unchanged, but in practice this is not the case. If compression has occurred, then in what part of the code and can it be avoided?
public static void main(String[] args) throws IOException {
String sourceImage = "C:\\Hydrangeas.jpg";
File newImage = new File(sourceImage);
byte[] bytes = Files.readAllBytes(newImage.toPath());
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
File outputfile = new File("C:\\out.jpg");
ImageIO.write(img, "jpg", outputfile);
}
Answer the question
In order to leave comments, you need to log in
In theory, ImageIO.write presses it, there is no one else. Apparently, it is impossible to control the degree of compression in this method.
ImageReader reader = ...; //ImageIO.getImageReaders(ImageIO.createImageInputStream(stream))
reader.setInput(...);
IIOImage image = reader.readAll(0, null);
RenderedImage renderedImage = image.getRenderedImage();
ImageWriter writer = ImageIO.getImageWriter(reader);
ImageWriteParam param = writer.getDefaultWriteParam();
paran.setCompressionMode(MODE_COPY_FROM_METADATA);
writer.setOutput(...);
writer.write(null, image, param);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question