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

OSGL Tool (Part 1): The Art of Image Processing

DZone's Guide to

OSGL Tool (Part 1): The Art of Image Processing

This introductory post dives into the Open Source General Library and how to use it for image editing and processing.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

OSGL (Open Source General Library) is a set of Java toolkit libraries. It is designed for concise and expressive coding.

This is the first article about OSGL, and we will introduce how to use the OSGL tool Img utility to manipulate images.

Stream-Based Operation Framework

The Img utility applied a stream based operation framework so that we can concatenate multiple operations to an image and the final result will be a combination of operations.

Before we start, let's take a look at our three original images. We will call them img1, img2, and img3:

  • img1
    img1
  • img2
    img2
  • img3
    img3

Get the Image Sources

We will use the following factory method to get the images in the following part of this blog:

privatestatic InputStream img1() {
    URL url = ImgTest.class.getResource("/img/img1.png");
    return IO.is(url);
}

privatestatic InputStream img2() {
    URL url = ImgTest.class.getResource("/img/img2.jpg");
    return IO.is(url);
}

privatestatic InputStream img3() {
    return IO.is(ImgTest.class.getResource("/img/img3.png"));
}


1. Crop Image

We will crop img1 starting from (30, 30) to (100, 100). Here is how we did it:

Img.crop(source(img1()))
        .from(30, 30)
        .to(100, 100)
        .writeTo(new File("/tmp/img1_crop.gif"));

img1_crop

2. Resize Image

There are two different types of resizing. The first one is to resize by giving the absolute width and height of the new image dimension. The second is to resize by a factor that applied to both widht and height.

We still use img1 as the testbed.

2.1 Resize to a Given Height and Width

Img.resize(source(img1()))
        .to(100, 200)
        .writeTo(new File("/tmp/img1_resize.png"));

img1_resize

2.2 Resize by Scale

Img.resize(source(img1()))
        .to(2.0f)
        .writeTo(new File("/tmp/img1_resize_x2.png"));

img1_resize_x2

We can also shrink the image by scale using the resize operator. This time, we will operate on img2 instead of img1

source(img2())
        .resize(0.5f)
        .writeTo("/tmp/img2_resize_scale.png");

img2_resize_scale

2.3 Resize and Keep Ratio

Sometimes we want to specify the width or height but still want to keep the height/width ratio. Here is how we achieve it:

source(img1())
        .resize(100, 200)
        .keepRatio()
        .writeTo(new File("/tmp/img1_resize_keep_ratio.png"));

img1_resize_keep_ratio

3. Watermark an Image

To add a watermark to an image with default settings:

source(img1())
        .watermark("CONFIDENTIAL")
        .writeTo("/tmp/img1_watermark_def.png");

img1_watermark_def

It also supports watermarking with different settings, e.g, the following code puts the watermark 200 pixels up and applies it with a dark gray color:

source(img1())
        .watermark("CONFIDENTIAL")
        .offsetY(-200)
        .color(Color.DARK_GRAY)
        .writeTo("/tmp/img1_watermark.png");

img1_watermark

4. Flipping

We support flipping the image horizontally (default) or vertically:

source(img1())
        .flip()
        .writeTo("/tmp/img1_flip_h.png");

img1_flip_h

To flip the image vertically:

source(img1())
        .flipVertial()
        .writeTo("/tmp/img1_flip_v.png");

img1_flip_v

5. Blurring

Check out blurring with different levels and their effects:

source(img1()).blur().writeTo("/tmp/img1_blur_default.png");

img1_blur_default

Blur with level 10:

source(img2()).blur(10).writeTo("/tmp/img2_blur_10.jpg");

img2_blur_10

Blur with level 2:

source(img2()).blur(2).writeTo("/tmp/img2_blur_2.jpg");

img2_blur_2

6. Concatenate Images

By default, images get concatenated horizontally:

source(img2())
        .appendWith(source(img3()))
        .writeTo("/tmp/img_concat_2_3.png");

img_concat_2_3

appendTo is the opposite of appendWith:

source(img2())
        .appendTo(source(img3()))
        .writeTo("/tmp/img_concat_3_2.png");

img_concat_3_2

Concatenate vertically without scale adjustment:

source(img2()).appendWith(source(img1()))
        .noScaleFix()
        .vertically()
        .writeTo("/tmp/img_concat_2_1.png");

img_concat_2_1

Concatenate three images:

concat(source(img1()), source(img2()))
        .appendWith(source(img3()))
        .vertically()
        .writeTo("/tmp/img_concat_123.png");

img_concat_123

Concatenate three images with a different arrangement:

concat(source(img2()))
        .with(source(img3()))
        .vertically()
        .appendWith(source(img1()))
        .writeTo("/tmp/img_concat_231.png");

img_concat_231

7. Multiple Processing With Pipeline

source(img1())
        .resize(300, 400)
        .pipeline()
        .crop(50, 50, 250, 350)
        .pipeline()
        .watermark("HELLO OSGL")
        .writeTo("/tmp/img1_pipeline.png");

img1_pipeline

Summary

This article demonstrates how to use the OSGL Img utility to process your images including:

  1. Cropping
  2. Resizing
  3. Watermarking
  4. Flipping
  5. Blurring
  6. Concatenation
  7. Applying multiple operations with a pipeline

It is worth noting the OSGL Img utility's API is designed to be expressive and fluent to use. Moreover, the OSGL Img API framework allows a developer to easily extend the built-in processors as enumerated above. We will cover this part in the next article.

To use OSGL, 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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}