Clerk now has built-in support for the java.awt.image.BufferedImage
class, which is the native image format of the JVM.
When combined with javax.imageio.ImageIO/read
, one can easily load images in a variety of formats from a java.io.File
, an java.io.InputStream
, or any resource that a java.net.URL
can address.
For example, we can fetch a photo of De zaaier, Vincent van Gogh's famous painting of a farmer sowing a field from Wiki Commons like this:
We've put some effort into making the default image rendering pleasing. The viewer uses the dimensions and aspect ratio of each image to guess the best way to display it in classic DWIM fashion. For example, an image larger than 900px wide with an aspect ratio larger then two will be displayed full width:
On the other hand, smaller images are centered and shown using their intrinsic dimensions:
If you find yourself using a library that returns images as a ByteArray
, you can read the image into a BufferedImage
by wrapping it in a java.io.ByteArrayInputStream
before passing it to java.imageio.ImageIO/read
.
In this example, we'll load computer art pioneer Vera MolnΓ‘r's 1974 work (Des)Ordres into a byte array, then convert it to a BufferedImage
for display. π
In addition to being able to load and use images from many sources, one can also generate images from scratch with code. Here is an example mathematical butterfly: π¦
... which should finally let us implement this legendary emacs function:
Thanks for reading, and β as always β let us know what you make with Clerk!