Requirements:
There is a position size picture. Now you need to get a picture of the specified size according to the original picture, and the obtained picture should conform to the proportion of the original picture, that is, after the picture is obtained by equal scaling on the basis of the original picture, you can cut it, so as to ensure that the obtained picture is a part of the original picture, rather than stretching or compressing the original picture to the specified size, Such pictures will be seriously distorted and uncoordinated.
For example:
The original picture is 600 × 400. Now the requirements are as follows:
- One 500 × 300 pictures
- One 700 × 400 pictures
- One 400 × 500 pictures
Note: the obtained picture cannot be the person or scene in the original picture, which has the feeling of stretching or compression.
Idea:
five hundred × 300 image: it can be seen that the width and height are within the size of the original image, but in order to get more information about the original image, you can first compress the original image according to a certain ratio, the compression ratio min (500 / 600300 / 400). Why choose such a compression ratio? Because if you compress according to the width ratio, although the width of the picture is consistent with the requirements, what about the height? It is possible that the height compression is indeed consistent before, that is, it is greater than the height of the target image. However, after the shackle, the height may be smaller than the required height, resulting in the inability to install and the requirement to intercept the image. Therefore, it is necessary to compress after comparison, so it will not exceed the range.
Similarly, whether the required image size exceeds the size of the original image or is within the size range of the original image, it should be compared first and then compressed, so as to ensure that the obtained image is enlarged or reduced to the most appropriate size and contains the most original image information without deformation.
The core of calculating compression ratio algorithm:
/* * The core algorithm calculates the compression ratio of the picture */ int w= buffer.getWidth(); int h=buffer.getHeight(); double ratiox = 1.0d; double ratioy = 1.0d; ratiox= w * ratiox / width; ratioy= h * ratioy / height; if( ratiox >= 1){ if(ratioy < 1){ ratiox = height * 1.0 / h; }else{ if(ratiox > ratioy){ ratiox = height * 1.0 / h; }else{ ratiox = width * 1.0 / w; } } }else{ if(ratioy < 1){ if(ratiox > ratioy){ ratiox = height * 1.0 / h; }else{ ratiox = width * 1.0 / w; } }else{ ratiox = width * 1.0 / w; } } /* * When the magnification or reduction factor of the picture is calculated, if the ratiox is greater than 1, it means to enlarge, otherwise it means to reduce */
In this way, the calculated ratio is the ratio to be compressed. w. h is the width and height of the original image, while the width and height in the program are the width and height of the image to be obtained.
The procedures for generating pictures and other places refer to others. I forgot the specific address. Thank the author again. Here is the source code:
import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class UploadImg { String fromFileStr; String saveToFileStr; String sysimgfile; int width; int height; String suffix; /** * @param fromFileStr * Full path of original picture * @param saveToFileStr * Thumbnail picture save path * @param sysimgfilenNow * Processed picture file name prefix * */ public UploadImg(String fromFileStr, String saveToFileStr, String sysimgfile,String suffix,int width,int height) { this.fromFileStr = fromFileStr; this.saveToFileStr = saveToFileStr; this.sysimgfile = sysimgfile; this.width=width; this.height=height; this.suffix=suffix; } public boolean createThumbnail() throws Exception { // fileExtNmae is the image format gif JPG or png // String fileExtNmae=""; File F = new File(fromFileStr); if (!F.isFile()) throw new Exception(F + " is not image file error in CreateThumbnail!"); File ThF = new File(saveToFileStr, sysimgfile +"."+suffix); BufferedImage buffer = ImageIO.read(F); /* * The core algorithm calculates the compression ratio of the picture */ int w= buffer.getWidth(); int h=buffer.getHeight(); double ratiox = 1.0d; double ratioy = 1.0d; ratiox= w * ratiox / width; ratioy= h * ratioy / height; if( ratiox >= 1){ if(ratioy < 1){ ratiox = height * 1.0 / h; }else{ if(ratiox > ratioy){ ratiox = height * 1.0 / h; }else{ ratiox = width * 1.0 / w; } } }else{ if(ratioy < 1){ if(ratiox > ratioy){ ratiox = height * 1.0 / h; }else{ ratiox = width * 1.0 / w; } }else{ ratiox = width * 1.0 / w; } } /* * When the magnification or reduction factor of the picture is calculated, if the ratiox is greater than 1, it means to enlarge, otherwise it means to reduce */ AffineTransformOp op = new AffineTransformOp(AffineTransform .getScaleInstance(ratiox, ratiox), null); buffer = op.filter(buffer, null); //Screenshot from enlarged image center buffer = buffer.getSubimage((buffer.getWidth()-width)/2, (buffer.getHeight() - height) / 2, width, height); try { ImageIO.write(buffer, suffix, ThF); } catch (Exception ex) { throw new Exception(" ImageIo.write error in CreatThum.: " + ex.getMessage()); } return (true); } public static void main(String[] args) { UploadImg UI; boolean ss = false; try { UI = new UploadImg("C:\\Users\\Administrator\\Pictures\\111.jpg", "C:\\Users\\Administrator\\Pictures\\", "ps_low2","png",280,280); ss = UI.createThumbnail(); if (ss) { System.out.println("Success"); } else { System.out.println("Error"); } } catch (Exception e) { System.out.print(e.toString()); } } }