greenlaw110 / blog

support writing blogs
0 stars 0 forks source link

Using OSGL Img utility to manipulate images #1

Open greenlaw110 opened 6 years ago

greenlaw110 commented 6 years ago

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

This is the first article about OSGL art of coding series, and we will introduce how to use 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, and we call them img1, img2 and img3:

Get the image sources

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

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

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

    private static 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, first one is resize by giving absolute width and height of the new image dimension; and the second is to resize by a factor which applied to both widht and height.

We still use img1 as the testbed:

2.1 resize to 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 image by scale using resize operator. This time we will operate 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 support water mark with different settings, e.g, the following code put watermark 200 pixels up and applied it with dark gray colour:

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

img1_watermark

4. Flipping

We support flip image horizontally (default) or vertically

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

img1_flip_h

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

img1_flip_v

5. Blurring

Checkout blur with different levels and their effects:

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

img1_blur_default

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

img2_blur_10

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

img2_blur_2

source(img3()).blur(5).writeTo("/tmp/img3_blur_5.jpg");

img2_blur_10

6. Concatenate images

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

img_concat_2_3

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

img_concat_3_2

Concatenate veritically 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 organization:

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 blog demonstrates how to use 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 pipeline

To get 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.1.