0

This is my code:

private Rectangle getImageUriImageSize(String uri) {
        Matcher matcher = dataUriPattern.matcher(uri);
        if (!matcher.find()) return null;
        byte[] bytes = DatatypeConverter.parseBase64Binary(matcher.group(2));
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        Rectangle rectangle = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            if (bufferedImage != null) {
                rectangle = new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
            }
        } catch (IOException e) {
            logger.warn("Error occurred when reading base64 Data URI: '{}'", e.getMessage());
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        return rectangle;
    }

it gives error

exception: I/O error reading PNG header!

i changed to this with the suggestion of

https://stackoverflow.com/a/27108892

private Rectangle getImageUriImageSize(String uri) {
    Matcher matcher = dataUriPattern.matcher(uri);
    if (!matcher.find()) return null;
    byte[] bytes = DatatypeConverter.parseBase64Binary(matcher.group(2));

    InputStream in = new ByteArrayInputStream(bytes);


  //  ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    Rectangle rectangle = null;
    try {
        BufferedImage bufferedImage = ImageIO.read(in);
        if (bufferedImage != null) {
            rectangle = new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        }
    } catch (IOException e) {
        logger.warn("Error occurred when reading base64 Data URI: '{}'", e.getMessage());
    } finally {
        IOUtils.closeQuietly(in);
    }
    return rectangle;


}

but this time i got errors. I use docker and this container exits.

So what to do?

also this

 public Rectangle getImageSize(String currentMerchant, String relativeUrl) {logger.info("getImageSize");
        String imageUrl = getImageUrl(currentMerchant, relativeUrl);
        Rectangle rect = null;
        FileImageInputStream imageInputStream = null;
        try {
            File downloadedImageFile = restTemplate.execute(
                imageUrl,
                HttpMethod.GET,
                null,
                response -> {
                    InputStream inputStream = response.getBody();
                    String fileName = getFileName(relativeUrl);
                    return saveInputStreamAs(inputStream, fileName);
                }
            );

            Iterator<ImageReader> imageReaders = getImageReaders(downloadedImageFile);
            if (!imageReaders.hasNext()) {
                logger.warn("Cannot find any image reader for file {}", downloadedImageFile);
            }
            ImageReader imageReader = imageReaders.next();
            imageInputStream = new FileImageInputStream(downloadedImageFile);
            imageReader.setInput(imageInputStream, true, true);
            rect = new Rectangle(0, 0, imageReader.getWidth(0), imageReader.getHeight(0));
        } catch (HttpStatusCodeException ex) {
            logger.warn("Error getting image resource from url '{}', status: {}, message: {}", imageUrl, ex.getStatusCode(), ex.getResponseBodyAsString());
        } catch (IOException ex) {
            logger.warn("Could not read image by url '{}', exception: {}", imageUrl, ex.getMessage(), ex);
        } finally {
            IOUtils.closeQuietly(imageInputStream);
        }
        return rect;
    }

error is here

catch (IOException ex) {
        logger.warn("Could not read image by url '{}', exception: {}", imageUrl, ex.getMessage(), ex);
    }
Community
  • 1
  • 1
mark
  • 727
  • 3
  • 14
  • 35
  • 1
    Please edit your question, and add the full stack trace and a link to the image causing the exception. Also, it's not clear to me why you need 3 different ways of doing this... Do they all fail with the same exception?The 3rd version will be the fastest, as it only needs to read the headers, not decode the pixel data. – Harald K Jan 11 '17 at 12:43
  • 1
    PS: Checked the source code `PNGImageReader`, and it seems that the PNG is corrupted. You should get more info from the stack trace. It could be the original soure file, or it could be due to a bug in the file download code. – Harald K Jan 11 '17 at 12:47
  • I can see image on web but when i try to open in ubuntu, it sas not a png file. When i change to jpg, i can open.I debugged 20 times but there are lots of traces to follow. I am not sure if i am looking at true place. Also in windows they can be opened without error. – mark Jan 11 '17 at 13:24
  • 1
    Add a link to the image, please. You might need to change your code, so that it prints the stack trace to the log, not only the exception message. – Harald K Jan 11 '17 at 13:43
  • 2
    Sounds like your image is really a JPEG mislabeled as PNG. What do the first few bytes look like (result of xxd file.png | head -10)? – Glenn Randers-Pehrson Jan 11 '17 at 15:30
  • @GlennRanders-Pehrson it says ÿØÿàJFIFÿþ – mark Jan 12 '17 at 05:57
  • 1
    @GlennRanders-Pehrson Seems you are spot on. However, I don't understand how that file could produce the exception mentioned from the `PNGImageReader`, as `ImageIO` will look up a suitable reader by inspecting the bytes, *not* by file extension. There's nothing in those bytes that looks like PNG. – Harald K Jan 12 '17 at 09:23
  • So that is why, we can open it in web because we dont need binary data to preview it. So, corrupted image and no need to read it – mark Jan 12 '17 at 10:41

1 Answers1

1

Please check the file extension that you pass. It should be PNG.

Suresh Patil
  • 121
  • 1
  • 5