有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中检测损坏的图像(PNG、JPG)

我需要检测图像文件是否在Java中损坏。我只处理PNG和JPG图像。这可能与梵语有关吗?或者可以用ImageIO完成吗?我试过使用ImageIO。读书似乎很管用。但我不确定它是否能检测出图像中的各种错误。我想知道最好的做法是什么


共 (4) 个答案

  1. # 1 楼答案

    这是我的解决方案,可以处理检查损坏的GIF、JPG和PNG。它使用JPEG EOF标记检查截断的JPEG、使用索引越界异常检查的GIF和使用EOFEException的PNG

    public static ImageAnalysisResult analyzeImage(final Path file)
            throws NoSuchAlgorithmException, IOException {
        final ImageAnalysisResult result = new ImageAnalysisResult();
    
        final InputStream digestInputStream = Files.newInputStream(file);
        try {
            final ImageInputStream imageInputStream = ImageIO
                    .createImageInputStream(digestInputStream);
            final Iterator<ImageReader> imageReaders = ImageIO
                    .getImageReaders(imageInputStream);
            if (!imageReaders.hasNext()) {
                result.setImage(false);
                return result;
            }
            final ImageReader imageReader = imageReaders.next();
            imageReader.setInput(imageInputStream);
            final BufferedImage image = imageReader.read(0);
            if (image == null) {
                return result;
            }
            image.flush();
            if (imageReader.getFormatName().equals("JPEG")) {
                imageInputStream.seek(imageInputStream.getStreamPosition() - 2);
                final byte[] lastTwoBytes = new byte[2];
                imageInputStream.read(lastTwoBytes);
                if (lastTwoBytes[0] != (byte)0xff || lastTwoBytes[1] != (byte)0xd9) {
                    result.setTruncated(true);
                } else {
                    result.setTruncated(false);
                }
            }
            result.setImage(true);
        } catch (final IndexOutOfBoundsException e) {
            result.setTruncated(true);
        } catch (final IIOException e) {
            if (e.getCause() instanceof EOFException) {
                result.setTruncated(true);
            }
        } finally {
            digestInputStream.close();
        }
        return result;
    }
    
    public class ImageAnalysisResult {
        boolean image;
        boolean truncated;
        public void setImage(boolean image) {
            this.image = image;
        }
        public void setTruncated(boolean truncated) {
            this.truncated = truncated;
        }
     }
    }
    
  2. # 2 楼答案

    下面的代码验证。jpeg。巴布亚新几内亚。jpg。java中的tiff图像

    public boolean isValidImage(File f) {
      boolean isValid = true;
      try {
        ImageIO.read(f).flush();
      } catch (Exception e) {
        isValid = false;
      }
      return isValid;
    }
    
  3. # 3 楼答案

    如果图像为JPEG格式,请使用以下方法:

    JPEGImageDecoder decoder = new JPEGImageDecoder(new FileImageSource(f) ,new FileInputStream(f));
    decoder.produceImage();
    

    如果它抛出异常;这意味着图像已损坏

    其他情况,;只需使用new ImageIcon(file)检查有效性

  4. # 4 楼答案

    如果无法解析图像,则文件已损坏,否则文件应有效,但包含错误的像素,正如Andrzej指出的那样。如果无法定义如何找到“错误”像素,那么检测这一点可能相当困难

    如果您有关于基础图像的信息,例如直方图或甚至原始像素,您可以尝试将其与读取的图像进行比较。但是,请注意,由于压缩,可能会出现一些错误,因此需要添加一些公差值

    另一个附加说明:Sanselan不会读取JPEG图像,因此您必须在此处使用ImageIO