Instance method of java image clipping and thumbnail generation

1、 Thumbnail

When browsing the album, you may need to generate corresponding thumbnails.

Direct code:

Directly run the main method and fill in the corresponding parameters.

2、 Generate cropped pictures with JS

When we modify personal microblog and QQ data, we can upload personal avatars, cut personal avatars and upload them. The size style of the clipped image is implemented through JavaScript, but it does not generate a new image. However, JS clip image provides the X, y coordinates, width and height of the image. Through these four parameters, we can generate a new clip image according to the original image.

Steps:

1. First of all, I use js to realize the image cutting and browsing function through the page. I refer to the information provided by the website and make a slight change. In addition, we can also use plug-ins. For example, jcrop is a very good picture clipping plug-in.

Download address: http://download.csdn.net/detail/u012385190/9733480

The final effect image is as above. You can drag and drop on the left and preview on the right.

2. Java generates and saves cut pictures

This method can be run directly in main. The four parameters passed in are image path, left value, top value, length and width.

Let's analyze how to obtain these four parameters:

1. Taking my JS cut as an example, the following is obtained through F12:

The red part in the picture is the div cut by the picture. We can observe which parameters have been changed by dragging the size and width of the clipping area, and then determine which parameter values correspond to the specific parameter values. As shown in the picture, my x / Y / width / height are 40, 28224228 respectively.

Note: in JS, I defined the length and width of the div and the picture as 300 * 300. In order to cooperate with the test, the picture I downloaded is also 300 * 300. If the size of the picture you tested is not 300 * 300, the effect you tested directly in the Java above will be different from that you saw in the front end, because I define the width and height of the picture in your front end as 300 * 300, while your actual picture (that is, the picture in Java) is not.

So what if we deal with this problem?

Get the length and width of the original picture in your Java code, and then judge whether the length and width of the original picture is 300 * 300. If not, generate a 300 * 300 thumbnail of the picture, and then use the 300 * 300 thumbnail as the cropped picture prototype. (it's not handled in my code. I need to handle it myself. Delete the thumbnail after using up the pictures)

2. Jcrop get parameters

@H_ 403_ 70@

As shown in the figure, jcrop provides parameters directly and can be used directly. However, it has a disadvantage that the image size area on the front page is not fixed. If you have a large pixel image, it will be very ugly. For example, I have an image Soga in the corresponding file_ bak. Jpg, it's not good to change to this picture.

Therefore, to sum up, it is recommended to use the first JS, and then judge whether the length and width of the original image is 300. If not, generate 300 * 300 thumbnails, and then use the thumbnails as the cutting prototype image, and delete the thumbnails when they are used up.

The above is the example method of java image clipping and thumbnail generation introduced by Xiaobian. I hope it will be helpful to you if you have any questions. Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>