OSGL Tool (Part 2): Extending the Power of Image Processing

DZone 's Guide to

OSGL Tool (Part 2): Extending the Power of Image Processing

Being able to process and alter images is a good start, but let's talk about how to extend OSGL Img with a customized image processor.

· Java Zone ·
Free Resource

In the previous article, OSGL Tool: The Art of Image Processing, we introduced how to uset he OSGL Img utility to process images, including:

  • Cutting
  • Resizing
  • Watermarking
  • Flipping
  • Blurring
  • Concatenation
  • Using a pipeline to process images

In this article, we will talk about how to extend OSGL Img with a customized image processor.

Suppose we need to darken an image. We can define a processor Sunglass as below:

public class Sunglass extends Img.Processor {
    private float alpha = 0.3f;

    Sunglass() {}
    Sunglass(float alpha) {this.alpha = alpha;}

    protected BufferedImage run() {
        int w = sourceWidth;
        int h = sourceHeight;
        Graphics2D g = g();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g.drawImage(source, 0, 0, w, h, null);
        return target;

Here is the code using the Sunglass processor:

Style A:



Style B:



Note that we have to pass the alpha level into the constructor (or use the default constructor) of theSunglass class. If we want to get Sunglass perfectly integrated into the OSGL Img API framework, we need to do a bit more work to adapt it:

public class FluentSunglass extends Img.Processor<FluentSunglass, FluentSunglass.Stage> {

    public static class Stage extends Img.ProcessorStage<Stage, FluentSunglass> {
        public Stage(BufferedImage source, FluentSunglass processor) {
            super(source, processor);
        public Stage alpha(float alpha) {
            processor.alpha = alpha;
            return this;

        public Stage dark() {
            return alpha(0.3f);

        public Stage light() {
            return alpha(0.7f);

        public Stage darker() {
            return alpha(0.1f);

        public Stage lighter() {
            return alpha(0.9f);

    float alpha;

    public FluentSunglass() {

    protected BufferedImage run() {
        return new Sunglass(alpha).source(source).run();

Now we can enjoy Fluent API calls with the FluentSunglass processor:

Make it lighter:



Make it darker:




This article demonstrates how to extend the OSGL Img utility with your own customized image processor. This flexibility of the OSGL Img API framework makes it possible to extend the OSGL Img utility to a professional image library with an elegant and fluent API.

In the next article in the OSGL tool series, we will introduce how to use the OSGL S utility to manipulate Java String objects.

To get the OSGL project, add the following dependency to your pom.xml file:


At the moment the osgl-tool.version is 1.10.0.

fluent api, image processing, java, osgl, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}