[Java instance] use thumbnail to generate thumbnails (zoom, rotate, crop, watermark)

Keywords: Java github Mobile network

1 demand

Cousin needs to sign up for the exam for his son. The system requires uploading no more than 30KB of pictures. Now the mobile phone is just a few MB of pictures. How to get a picture of only 30KB?

A simple way is to reduce the size of the picture on the computer, and then screen capture small pictures, but now the computer screen resolution is very high, and the screen capture size is not easy to control; the same resolution in different image formats, the size is also very different. Try the wechat screenshot tool, the output image is relatively large. So he gave up the idea.

Another way is through other tools. We found the online image compression website and a mobile phone software, and found that the compression is invalid when the image is smaller than a certain size. If we continue to look for other tools, it will be a waste of time and time, so we give up this method.

Finally, I wrote a code to compress the image to 29KB according to the original scale, using thumbnail library, two or three lines of code.

2 what can thumbnails do?

Thumbnails are widely used, such as avatars, picture messages, product pictures, etc.

For example, when you have a new wechat friend, you can see his avatar, which is a smaller thumbnail than the original image in the beginning. When you click to view the original image, the wechat client will download the original image for you. Because you will not be interested in everyone's Avatar, you will check the clear original image, and a small thumbnail can be satisfied. This can reduce the burden of network transmission and speed up the response speed.

The same is true for wechat pictures and videos. First, I'll send you a smaller preview, and then you can click to view the original picture or video playback to send you a larger file.

3 open source library of thumbnails

There are many open source libraries for thumbnails:

(1)Thumbnailator

GitHub: https://github.com/coobird/th...

Independent of external library, light and efficient, applicable to any platform, supporting scaling, rotation, interception and watermark.

(2)Imgscalr

GitHub: https://github.com/rkalla/img...

All based on Java 2D, independent of external libraries, light and efficient, applicable to any platform, supporting scaling, rotation, interception, and watermark.

This article focuses on the use of thumbnail. The latest version is 0.4.8. maven is introduced as follows:

<dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
  <version>0.4.8</version>
</dependency>

4 common operations

4.1 specified size scaling

The original picture is a 4:3 scale picture, 4032x3024 (the picture is too large for the website to upload the original picture), as follows:

When scaling with the specified size method, the original scale is maintained by default.

//The parameter is small and the scale is the same as the original scale
//Then output the result according to the parameter
Thumbnails.of(originalPic)
  .size(400, 300)
  .toFile(picturePath + "climb-up.size.400X300.jpeg");
//Large parameter and unequal proportion
//Then scale up and take the minimum value
Thumbnails.of(originalPic)
  .size(4400, 3400)
  .toFile(picturePath + "climb-up.size.4400X3300.jpeg");
//Small parameters and unequal proportions
//Then reduce by proportion and take the minimum value
Thumbnails.of(originalPic)
  .size(200, 300)
  .toFile(picturePath + "climb-up.size.200X150.jpeg");
//Do not maintain scale
//Then output the result according to the parameter
Thumbnails.of(originalPic)
  .size(200, 300)
  .keepAspectRatio(false)
  .toFile(picturePath + "climb-up.size.notKeepRatio.200X300.jpeg");
//Force size
//Then output the result according to the parameter, as in the previous example
Thumbnails.of(originalPic)
  .forceSize(200, 300)
  .toFile(picturePath + "climb-up.forceSize.200X300.jpeg");

Show two of the results:

(1) size(200, 300), the result is a picture of 200X150, the proportion is still 4:3.

(2) forceSize(200, 300), the result is 200X300 picture, if the original scale is different, there will be deformation.

4.2 scaling

Scaling by scale refers to scaling by width and height at the same time. See the following code:

//Scale less than 1, reduce
//Width and height reduced to 0.1 times of the original at the same time
Thumbnails.of(originalPic)
  .scale(0.1f)
  .toFile(picturePath + "climb-up.scale.403X302.jpeg");
//Scale greater than 1, zoom in
//The width and height are enlarged by 1.1 times at the same time
Thumbnails.of(originalPic)
  .scale(1.1f)
  .toFile(picturePath + "climb-up.scale.4435X3326.jpeg");

4.3 rotation by angle

When rotating according to the angle, when the angle is positive, clockwise; when the angle is negative, anticlockwise. The code is as follows:

Thumbnails.of(originalPic)
  .size(400,300)
  .rotate(45)
  .toFile(picturePath + "climb-up.rotate.45.jpeg");

The compressed and rotated result image is as follows:

4.4 add watermark

It is also very convenient to add a watermark. In our example, we put the watermark in the upper right corner, and the code is as follows:

Thumbnails.of(originalPic)
  .size(2000,1500)
  .watermark(Positions.TOP_RIGHT, ImageIO.read(
    new File(picturePath + "pkslow.size.400X300.jpeg")), 0.5f)
  .toFile(picturePath + "climb-up.watermark.jpeg");

The image with watermark is as follows:

4.5 tailoring

The code is as follows:

Thumbnails.of(originalPic)
  .sourceRegion(Positions.TOP_RIGHT, 1800, 1800)
  .size(400, 400)
  .toFile(picturePath + "climb-up.crop.jpeg");

The results are as follows:

4.6 batch operation of files under directory

This function is also very useful. You can operate all pictures in the directory and specify the filename output, such as specifying a prefix. The code is as follows:

Thumbnails.of(new File("/pictures/201912/").listFiles())
  .size(400, 400)
  .toFiles(Rename.PREFIX_DOT_THUMBNAIL);

The result of the operation is as follows:

5 Summary

The thumbnail library is easy to operate, supports scaling, rotation, cropping, watermark and other functions, and has no other dependence, so it is worth learning.

Welcome to the public number "pumpkin slow talk", which will continue to update for you.

Read more, share more; write more, organize more.

Posted by sulin on Fri, 06 Dec 2019 09:09:54 -0800