Skip to content

Core functionalities

Tomek edited this page Aug 3, 2014 · 7 revisions

Image draw core functionalities

Open and save

Before starting any image processing, ImageProcessing object needs to be created and image should be loaded.

ImageProcessing image = new ImageProcessing();
image.open("/home/user/city.jpg");

After image processing is finished, final image can be saved to file.

image.save("/home/user/output.jpg");

If you want to save file in different format, than original image, use overloaded save method.

image.save("/home/user/output.png", ImageType.PNG);

Resize

In order to change image size, use resize method and provide at least one dimension.

If only one dimension is defined, other is calculated automatically, so image aspect ratio won't change.

image.resize(800, null);

resize example

image.resize(null, 400);

resize example

If both dimensions are defined, image is resized to exact size, even if it will change image aspect ratio.

image.resize(200, 200);

resize example

Crop

To cut part of the image use crop method. First two parameters define top-left corner. Third and fourth parameters are width and height of cropped area.

image.crop(1400, 420, 120, 180);

crop example

Rotate

There are two ways to rotate image. First method allows rotating only by multiplicity of 90 degrees.

image.rotate(Rotation.HALF);

rotate example

Second allows rotating by any amount of degrees, but requires to provide background color to use around rotated image.

image.rotate(20, Color.BLUE);

rotate example

Flip

To flip image horizontally or vertically use one of two provided methods: flipHorizontally() and flipVertically().

image.flipHorizontally();

thumbnail example

Text

To render text on image use drawText method and provide text to render, define its color, font, and placement.

image.drawText("Gdansk", Color.BLACK, new Font("SansSerif", Font.BOLD, 64), 100, 100);

thumbnail example

To help with text allignment you can use overloaded method, that allows to control horizontal and vertical alignment of rendered text.

Font textFont = new Font("SansSerif", Font.BOLD, 96);
image.drawText("Gdansk", Color.WHITE, textFont, 800, 500, TextHorizontalPosition.CENTER, TextVerticalPosition.MIDDLE);

thumbnail example

Draw image

To draw one image on another first you need to load image, but instead of using open() method which would replace image used for processing, use load() method, that will return ImageDrawEntity object with loaded image. After that you can use it in drawImage() method.

ImageDrawEntity coatOfArms = image.load("/home/user/coat-of-arms.png");
image.drawImage(coatOfArms, 480, 20);

thumbnail example

Thumbnails

There are three methods for generating thumbnails, each providing different result.

Thumbnail scale

First one scales down image to fit defined size, but if thumbnail image aspect ratio is different, than original image, it will make it smaller horizontally or vertically.

image.thumbnailScale(200, 200);

thumbnail example

Thumbnail scale with background

Second works similar, but if image aspect ratio is different, it fills top/bottom or left/right bars with defined background color at the image edges in order to create thumbnail with exact defined width and height.

image.thumbnailScale(200, 200, Color.YELLOW);

thumbnail example

Thumbnail crop

Third method will fit image to provided dimensions and cut off any edges outside thumbnail area if original image has different aspect ratio.

image.thumbnailCrop(200, 200);

thumbnail example

Clone this wiki locally