Img

Introduction

This article introduces a set of methods for processing images in AWT (Abstract Window Toolkit). The methods include scaling, cropping, converting to black and white, adding watermarks, and other operations.

Method Introduction

Image Crop

The following code segment demonstrates how to cut an image using Img, a class from the AWT library. The image “face.jpg” is located in the directory “e:/pic/”. The image is cropped from the (0, 0) position to a width of 200 pixels and saved as “face_radis.png” in the same directory.

Img.from(FileUtil.file("e:/pic/face.jpg"))
    .cut(0, 0, 200) //
    .write(FileUtil.file("e:/pic/face_radis.png"));

Image Compression

Image compression is only supported for Jpg files. The following code segment demonstrates how to compress an image using Img, a class from the AWT library. The image “1111.png” is located in the directory “e:/pic/”. The compression ratio is set to 0.8, and the compressed image is saved as “1111_target.jpg” in the same directory.

Img.from(FileUtil(FileUtil.file("e:/pic/1111.png")))
    .setQuality(0.8) // Compression ratio
    .write(FileUtil(FileUtil.file("e:/pic/1111_target.jpg")));