M
M
Mimocodil2021-09-15 15:33:29
Java
Mimocodil, 2021-09-15 15:33:29

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);
}

Properties:
6141e7c9cd189829942730.png
...
6141e7d610b05512444532.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-09-15
@Ezekiel4

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);

It should be something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question