If you ever wanted to do image manipulation in Java you will probably know AWT. This ancient toolkit is unfortunately the only way to work with images in Java SE. While Androids Bitmap class is quite up-to-date and offers a nice API, the AWT equivalent is very outdated. Additionally it is super slow: try loading an image taken with any modern phone and you will know what I’m talking about.
For one of my personal projects I needed a way to create thumbnail images for my image gallery. I started using imgscalr which offers a pure java solution to create thumbnails. The problem with imgscalr as well as with libraries like thumbnailator is that they are all based on AWT. This comes with some drawbacks:
- not a lot of supported formats
- it’s slow
- it’s buggy for certain combinations of formats, color spaces etc.
Due to the reasons above I decided to write a wrapper around the convert utility shipped as part of ImageMagick. The advantage of this approach is that it supports virtually every format (even HEIF!), is comparable in speed to pure Java libraries and is updated more regularly than AWT.
byte thumbnail = ThumbnailCreator.createThumbnail(1024, inputStream);
It’s as simple as that! And the best part: no BufferedImage, no AWT 🙂