Friends of OpenJDK Today

How does Java handle different Images and ColorSpaces – Part 1

June 20, 2020

Author(s)

  • Mark Stephens

    Entrepreneur and Java software developer, founder and CEO of IDRsolutions

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.

Topics:

Author(s)

  • Mark Stephens

    Entrepreneur and Java software developer, founder and CEO of IDRsolutions

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard