Package com.test;
Import Java.awt.image.BufferedImage;
Import Java.awt.geom.AffineTransform;
Import java.awt.image.AffineTransformOp;
Import Java.awt.Image;
Import Java.io.File;
Import java.io.IOException;
Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.imageio.ImageIO; public class Imageutil {public static void main (string arg[]) {string filePath = ' c:\\users\\lee\\desktop\\111.jpg ";
The position of the picture int height=300;
int width=300;
icon icon = null;
try {icon = Getfixedicon (filepath,width,height);
catch (Exception e) {System.out.println ("Exception:" +e); } System.out.println ("###" +icon); The location at which the new picture is generated;/** * Change the size of the picture by a wide scale * @param filePath picture path * @param width you need to change the width of the picture * @return * @throws Exception * p
Ublic static Icon getratiowidth (String filePath, int width) throws exception{file F = new file (FilePath);
BufferedImage bi = imageio.read (f); Double wratio = (New Integer (Width)). Doublevalue ()/bi.getwidth (); width ratio int height = (int) (Wratio * bi.getheight ()); The image is converted to a height of image image = Bi.getscaledinstance (Width,height,image.scale_smooth); Set the thumbnail of the image small Affinetransformop op = new Affinetransformop (Affinetransform.getscaleinstance (wratio,wratio), null);
Sets the zoom scale image = Op.filter (bi,null);
int lastlength = Filepath.lastindexof ("."); String Subfilepath = filepath.substring (0,lastlength); Get the picture output path String FileType = filepath.substring (lastlength);
Picture Type File Zoomfile = new file (Subfilepath + "_" + Width + "_" + height + fileType);
ICON ret = null;
try {imageio.write (bufferedimage) image, "JPG", zoomfile);
ret = new ImageIcon (Zoomfile.getpath ());
catch (Exception e) {e.printstacktrace ();
return ret; /** * Change picture size by a high proportion * @param filePath picture path * @param height to change the height of the picture * @return * @throws Exception/public static Icon getratioheight (String filePath, int height)Throws exception{File F = new file (FilePath);
BufferedImage bi = imageio.read (f); Double hratio = (new Integer (height)). Doublevalue ()/bi.getheight (); The height of the scale int width = (int) (Hratio * bi.getwidth ()); The image is converted to a height of image image = Bi.getscaledinstance (Width,height,image.scale_smooth); Set the thumbnail of the image small Affinetransformop op = new Affinetransformop (Affinetransform.getscaleinstance (hratio,hratio), null);
Sets the zoom scale image = Op.filter (bi,null);
int lastlength = Filepath.lastindexof ("."); String Subfilepath = filepath.substring (0,lastlength); Get the picture output path String FileType = filepath.substring (lastlength);
Picture Type File Zoomfile = new file (Subfilepath + "_" + Width + "_" + height + fileType);
ICON ret = null;
try {imageio.write (bufferedimage) image, "JPG", zoomfile);
ret = new ImageIcon (Zoomfile.getpath ());
catch (Exception e) {e.printstacktrace ();
return ret; /** * Change the size of the picture by any width and height of input * @param filePath * @param width * @param height * @return * @throws Exception/public static Icon Getfixedicon (String filePath, int wi
DTH, int height) throws exception{file F = new file (FilePath);
BufferedImage bi = imageio.read (f); Double wratio = (new Integer (width)). Doublevalue ()/bi.getwidth (); The proportions of the width double hratio = (new Integer (height)). Doublevalue ()/bi.getheight (); The height of the ratio image image = Bi.getscaledinstance (Width,height,image.scale_smooth); Set the thumbnail of the image small Affinetransformop op = new Affinetransformop (Affinetransform.getscaleinstance (wratio,hratio), null);
Sets the zoom scale image = Op.filter (bi,null);
int lastlength = Filepath.lastindexof ("."); String Subfilepath = filepath.substring (0,lastlength); Get the picture output path String FileType = filepath.substring (lastlength);
Picture Type File Zoomfile = new file (Subfilepath + "_" + Width + "_" + height + fileType);
ICON ret = null;
try {imageio.write (bufferedimage) image, "JPG", zoomfile); ret = new ImageIcon(Zoomfile.getpath ());
catch (Exception e) {e.printstacktrace ();
return ret;
}
}
The above comments and the use of the method written in detail, you can try, if you need to pass in IO flow and output IO flow, you can modify the method to the appropriate