Skip to content

Instantly share code, notes, and snippets.

@xexes
Created January 27, 2020 10:43
Show Gist options
  • Save xexes/601e8b2e695613c5267707b5fb58dcfc to your computer and use it in GitHub Desktop.
Save xexes/601e8b2e695613c5267707b5fb58dcfc to your computer and use it in GitHub Desktop.
public static void main(String[] args) throws IOException {
File file = new File(args[0]);
try (ImageInputStream input = ImageIO.createImageInputStream(file)) {
ImageReader reader = ImageIO.getImageReaders(input).next();
try {
reader.setInput(input);
ImageReadParam param = reader.getDefaultReadParam();
BufferedImage image = reader.getImageTypes(0).next().createBufferedImage(reader.getWidth(0), reader.getHeight(0));
param.setDestination(image);
try {
reader.read(0, param); // Will return image
} catch (IOException e) {
e.printStackTrace();
}
// image now contains as much of the decoded file as possible, even if an exception occured
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame(file.getName());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(new JScrollPane(new JLabel(new ImageIcon(image))));
frame.pack();
frame.setVisible(true);
}
});
}
finally {
reader.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment