Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PNG file corrupted after calling ImageReader getHeigth

I got my png file corrupted after calling :

BufferedImage im = ImageIO.read(inputStream);

You will find the service life cycle below :

InputStream inputStream = multipartFile.getInputStream();
throwExceptionIfImageDimensionsNotValid(inputStream);
storeIntoFileSystem(inputStream);

implementation

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void throwExceptionIfImageDimensionsNotValid(InputStream inputStream) throws IOException, StoreFileException {
        
        BufferedImage im = ImageIO.read(inputStream);
        
        if(isImageWidthHeightRatioOk(im.getHeight(), im.getWidth())) {
            return;
        } else {
            throw new StoreFileException("Le rapport hauteur / largeur de limage doit etre compris entre 0.55 et 0.625");
        }
    }

Store file implementation :

void storeIntoFileSystem(InputStream inputStreamIn) throws IOException{
        try (InputStream inputStream = inputStreamIn) {
            Path targetDirectoryPath = Paths.get("Files-Upload/");
            Path filePath = Paths.get("Files-Upload/").resolve("pacman_preview.png");

            Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);

        } catch (IOException ioe) {
            throw new IOException("Could not save file: " , ioe);
        }
    }

The problem is :
When I open the file in my OS image viewer, I’ve got this message :

Could not load image … Fatal error reading PNG image file. Not a PNG
file

NOTE : When the throwExceptionIfImageDimensionsNotValid(inputStream) is commented, the image open’s correctly.
enter image description here

At debug time I found that the inputStream object is modified when ImageIO.read(inputStream); is executed.

At this moment in variable eclipse view I can see bb and bs modified :
enter image description here

>Solution :

InputStream is a stream – if you read it in first method (ImageIO.read(inputStream)), it’s done, there is nothing left in the stream. You can’t read it again. In that case you have to open the stream again, as below, or do some refactor to do both operations by reading InputStream only once.

InputStream inputStream = multipartFile.getInputStream();
throwExceptionIfImageDimensionsNotValid(inputStream);
inputStream = multipartFile.getInputStream();
storeIntoFileSystem(inputStream);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading