Package Com.up.util;import Java.awt.image;import Java.awt.geom.affinetransform;import Java.awt.image.affinetransformop;import Java.awt.image.bufferedimage;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.FileOutputStream; Import Java.io.ioexception;import javax.imageio.imageio;/** * Picture Tool class */public final class Imageutils {/** * picture zoom * @param org Image path * @param dest Zoom path * @param height Height * @param width Width */public static Boo Lean Resize (String org,string dest, int height, int width) {Boolean bol = false;//whether compressed//logrecord.reco De (Imageutils.class, "resize", org); String pictype= ""; if (! "". Equals (org) &&org!=null) {Pictype = org.substring (Org.lastindexof (".") +1,org.length ()); }//logrecord.recode (Imageutils.class, "Height:", "" +height); Logrecord.recode (Imageutils.class, "width:", "" +width); Double ratio = 0; Scale file o = new file (org); File D = new file (dest); BufferedImage bi;try {bi = imageio.read (o); Image itemp = bi.getscaledinstance (width, height, bufferedimage.scale_smooth); int itempwidth = Bi.getwidth (); int itempheight = Bi.getheight (); Logrecord.recode (Imageutils.class, "Itempheight:", "" +itempheight); Logrecord.recode (Imageutils.class, "Itempwidth:", "" +itempwidth); Calculate proportions if (itempheight > height) | | (Itempwidth > width)) {//logrecord.recode (Imageutils.class, "" Start picture Scaling "-----pictype:", pictype); ratio = Math.min (New Integer (Heig HT). Doublevalue ()/Itempheight, (new Integer (width)). Doublevalue ()/itempwidth); Affinetransformop op = new Affinetransformop (affinetransform.getscaleinstance (ratio, ratio), null); itemp = Op.filter ( BI, NULL); Imageio.write ((BufferedImage) itemp,pictype, d); bol = true; }} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} return BOL; } public static void Resizewidth (String org,string dest, int height, int width) {//logrecord.recode (Imageutils.clas S, "Resizewidth", org); String pictype= ""; if (! "". Equals (org) &&org!=null) {Pictype = org.substring (Org.lastindexof (".") +1,org.length ()); } double ratio = 0; Scale file o = new file (org); File D = new file (dest); BufferedImage bi;try {bi = imageio.read (o); Image itemp = bi.getscaledinstance (width, height, bufferedimage.scale_smooth); int itempwidth = Bi.getwidth (); Logrecord.recode (Imageutils.class, "Itempwidth:", "" +itempwidth); Calculate the scale if (itempwidth! = width) {//logrecord.recode (Imageutils.class, "Start picture zoom width"-----pi CType: ", pictype); ratio = ((New Integer (width)). Doublevalue ()/itempwidth); Affinetransformop op = new Affinetransformop (affinetransform. getscaleinstance (ratio, ratio), null); itemp = Op.filter (bi, NULL); Imageio.write ((BufferedImage) itemp,pictype, D); }} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} } public static void CopyFile (String sourcepath, String TargetPath) {Bufferedinputstream inbuff=null; Bufferedoutputstream Outbuff=null; try {file sourcefile = new File (SourcePath); File TargetFile = new file (TargetPath); if (!targetfile.exists ()) {Targetfile.getparentfile (). Mkdirs (); }//Create a new file input stream and buffer it inbuff=new bufferedinputstream (new FileInputStream (sourcefile)); Create a new file output stream and buffer it outbuff=new bufferedoutputstream (new FileOutputStream (targetfile)); Buffer array byte[] b=new byte[1024 * 5]; int Len; while ((Len=inbuff.read (b))! =-1) {outbuff.write (b, 0, Len); }//Refresh this buffered output stream Outbuff.flush (); } catch (IOException e) {//logrecord.recode (Imageutils.class, "Picture copy exception:", E.getmessage ()); } finally {//closes the stream try {if (Inbuff! = null) inbuff.close (); if (outbuff! = null) Outbuff.close ();} CA TCH (IOException e) {//logrecord.recode (imageutils.class, "file stream Close exception:", E.getmessage ());}}} public static void Main (string[] args) throws ioexception{//pressimage ("D:\\images\\444.jpg", "D:\\images\\wmlogo". GIF ", 0.5f);//Presstext (" D:\\\\images\\\\444.jpg "," Wang Zi's Seal "," Song Body ", font.bold| Font.Italic, Color.Red, 50,.8f); Resizewidth ("C:\\test\\vip3.png", "C:\\test\\vip3_1.png", 90, 245); Resize ("E:\\testdata\\1.jpg", "e:\\testdata\\2.jpg", 200, 200); String TargetPath = "111.jpg". Substring (0, "111.jpg". IndexOf (". jpg")); System.out.println (TargetPath); }}
Java Upload Image compression Tool class