One of the attractions of Java is the way it abstracts and simplifies many programming constructs. In place of Tiffs, PNGs, JPEGs and other Image formats, you get a simple BufferedImage object. ImageIO and other third-party libraries such as our own JDeli image library provide methods to read and write a BufferedImage.
We have spent a lot of time working with different images formats in the process of writing the JDeli Image library as a replacement for ImageIO and the aim of this series is to share that knowledge to a wider audience.
Java makes images simple to use. You can work with a BufferedImage and just load or save this to any supported image file format. A BufferedImage includes lots of functionality which allows you to render and process the image, with all the complexity and implementation hidden by Java. A BufferedImage can even be used as a Graphics2D canvas which can be drawn on. Here is some example code.
//load image with ImageIO or JDeli BufferedImage image = ImageIO.read(new File("image.png")); BufferedImage image = JDeli.read(new File("image.png")); //draw a red diagonal line on it Graphics2D g2 = image.createGraphics(); g2.setColor(Color.red); g2.drawLine(0, 0, image.getWidth(), image.getHeight()); //save image with ImageIO or JDeli ImageIO.write(image, "PNG", new File("image.png")); JDeli.write(image, "PNG", new File("image.png"));
While Java removes a lot of Image complexity, it is worth understanding in more detail how images work. In this series of articles, we will be diving deep into how BufferedImage provides this abstraction, how different types of images work and how you can access the low-level Image data.
See you next time when we will look at ColorSpaces.
Why use JDeli?
If you are working with Images in Java, JDeli offers a range of advantages over ImageIO and alternatives, including:
- prevents heap related JVM crashes
- implements unsupported image formats
- reduce output file size
- improve read/write performance
- supports threading
- superior image scaling algorithms