Skip to content
Ralph Niemitz edited this page May 18, 2017 · 3 revisions

Reading images

You can read images by calling the correct constructors of SimpleImage.

The SimpleImage(int[][]) constructor takes in a two dimensional integer array. Each integer should represent a pixel in the ARGB color model. If one of the arrays in the second dimension are shorter or longer than others, errors may occur.

The SimpleImage(Image) constructor takes an Image object and converts it into a SimpleImage.

The SimpleImage(File) constructor reads an image from a file.

The SimpleImage(URL) constructor reads an image from an URL.

The SimpleImage(InputStream) constructor reads an image from an InputStream. However, the InputStream will not be closed. Let's say we want to read an image from the class path. We would do it like this:

SimpleImage myImage = null;

// MyClass.class.getClassLoader inside of static methods
try(InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("full/name/of/my/image.png")) {

	myImage = new SimpleImage(inputStream);

} catch(IOException exception) {

	exception.printStackTrace();
}

Writing images

Writing images is simple. You just have to give the OutputStream instance on which the image should be written to the SimpleImage.write(OutputStream,String,int) method. If you have a JPEG you should also specify the color model and format.

Example:

SimpleImage image = new SimpleImage(400, 400);
image.paint(graphics -> {
	graphics.setColor(Color.RED);
	graphics.fillRect(20, 20, 60, 60);
	graphics.setColor(Color.BLUE);
	graphics.drawString("My Image", 100, 100):
});
File outputFile = new File("myImage.png");

try(FileOutputStream outputStream = new FileOutputStream(outputFile)) {

	image.write(outputStream, "PNG", BufferedImage.TYPE_INT_ARGB);

} catch(IOException exception) {

	exception.printStackTrace();
}

Converting a SimpleImage to a Java object

There are several methods that allow you to manipulate the image data outside of SimpleImage. One of them is SimpleImage.toBufferedImage(int). This method converts the SimpleImage instance into a BufferedImage with the given color model.

It is also possible to get the binary data of the image directly with SimpleImage.toBinary(int).

If you want the binary data of the image encoded in Base64, you can call SimpleImage.toBase64(int).

Example:

SimpleImage image = new SimpleImage(400, 400);
image.paint(graphics -> {
	graphics.setColor(Color.RED);
	graphics.fillRect(20, 20, 60, 60);
	graphics.setColor(Color.BLUE);
	graphics.drawString("My Image", 100, 100):
});

JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(image.toBufferedImage())));
frame.pack();
frame.setLocatioonRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

JavaFX

For JavaFX support you have to have the simple-image-fx_vX.X.X.jar in your class path. The only difference to the core library is that you have to use SimpleImageFX instead of SimpleImage.

The show method

The show method creates a window with the image instance that called it in it. This only serves debugging purposes. I thought it was annoying to always code a JFrame in order to test filters and stuff, so I just added this method.