Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Get the Edge with a Professional Java IDE. 30-day free trial.

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;}

    @Override
    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:

newSunglass(0.7f)
        .process(img2())
        .pipeline()
        .resize(0.5f)
        .writeTo("/tmp/img2_sunglass_style_a.jpg");

image

Style B:

source(img2())
        .resize(0.3f)
        .pipeline(newSunglass())
        .writeTo("/tmp/img2_sunglass_style_b.png");

image

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() {
        super();
    }

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

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

Make it lighter:

source(img2())
        .resize(0.3f)
        .pipeline(FluentSunglass.class)
        .lighter()
        .writeTo("/tmp/img2_f_sunglass_lighter.png");

image

Make it darker:

source(img2())
        .resize(0.3f)
        .pipeline(FluentSunglass.class)
        .dark()
        .writeTo("/tmp/img2_f_sunglass_darker.png");

image

Summary

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:

<dependency>
    <groupId>org.osgl</groupId>
    <artifactId>osgl-tool</artifactId>
    <version>${osgl-tool.version}</version>
</dependency>


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

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,fluent api ,osgl ,image processing ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}