0

the png

I can open the png but used code to read it failed. The exception is 'java.io.EOFException: Unexpected end of ZLIB input stream' and the line is 4 where use ImageIO.read() function.

I succeded reading other png by using the same code.

public static void cut(String srcImageFile, String result, int x, int y, int width, int height) {
    try {
        // 读取源图像
        BufferedImage bi = ImageIO.read(new File(srcImageFile));
        int srcWidth = bi.getHeight(); // 源图宽度
        int srcHeight = bi.getWidth(); // 源图高度
        if (srcWidth > 0 && srcHeight > 0) {
            Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
            // 四个参数分别为图像起点坐标和宽高
            // 即: CropImageFilter(int x,int y,int width,int height)
            ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
            Image img = Toolkit.getDefaultToolkit()
                    .createImage(new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
            g.dispose();
            // 输出为文件
            ImageIO.write(tag, "PNG", new File(result));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Please tell me how to solve the problem.

  • 2
    Sounds like the image is corrupted or to a valid format for `ImageIO` – MadProgrammer Jul 10 '19 at 06:14
  • 1
    Image could have been corrupted. Open the image in some editor , save it back and try again. – Kris Jul 10 '19 at 06:19
  • 1
    _I can open the png_ Do you mean you can view the image if you open it in something like "Paint" on Windows ? – Abra Jul 10 '19 at 06:19
  • 2
    *I succeded reading other png by using the same code.* It means, that only 1 image cannot be open? Maybe the wrong size or file was corrupted? Try to look [here](https://stackoverflow.com/q/24531089/9719337) – Dred Jul 10 '19 at 06:21
  • Thanks for your help! I have learned some similar questions but didn't solve my problem. The png is saved in server and has a url. I can view it in browser like chrom, IE, fire fox.... I used UrlConnection to get inputstream then used 'read' but failed. I saved it from chrom then use the code to read failed too. – user10125164 Jul 10 '19 at 06:41
  • So, is there any method that we can use to read corrupted png? – user10125164 Jul 10 '19 at 06:46
  • I uploaded the png to some websites which have the function to cut img, I tried and succeded. It made me confused. – user10125164 Jul 10 '19 at 06:52

1 Answers1

0

If you open the image in Chrome or other tool, you will see that the lower part of the image (part of the QR code) is missing, or just black. This is because the PNG file is indeed corrupted, or rather truncated it seems. There is no way to "solve" this, other than getting a new copy of the file, without the truncation.

However, it is possible to partially read the PNG file using Java, just like in other tools. It just can't be done using ImageIO.read(...) convenience methods, because you will get an exception and no return value.

Instead, use the full verbose code:

BufferedImage image;

try (ImageInputStream input = ImageIO.createImageInputStream(file)) {
    ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle no reader case

    try {
        reader.setInput(input);

        int width = reader.getWidth(0);
        int height = reader.getHeight(0);

        // Allocate an image to be used as destination
        ImageTypeSpecifier imageType = reader.getImageTypes(0).next();
        image = imageType.createBufferedImage(width, height);

        ImageReadParam param = reader.getDefaultReadParam();
        param.setDestination(image);

        try {
            reader.read(0, param); // Read as much as possible into image
        }
        catch (IOException e) {
            e.printStackTrace(); // TODO: Handle
        }
    }
    finally {
        reader.dispose();
    }
}

// image should now contain the parts of the image that could be read
Harald K
  • 26,314
  • 7
  • 65
  • 111