/** * * Directly specify the compressed width: * @param oldFile * The file to be compressed * @param width * Compressed After the height of the * @param height * Compressed after * @return return the full path of the compressed file Path */public static file Zipimagefile (file oldFile, int width, int height) {if (oldFile = = null) {return null;} File NewImage = null;try {/** handles temporary files on the server */image srcfile = Imageio.read (oldFile);/** width, high set */bufferedimage tag = new B Ufferedimage (width, height,bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (srcfile, 0, 0, width, height, NULL),/** compressed file name can then be customized */newimage = oldfile;/** compressed after the temporary storage location */fileoutputstream out = new FileOutputStream (newimage); JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (tag),/** compression quality */jep.setquality (n, True); Encoder.encode ( tag, Jep); Out.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return newimage;}
Compress picture Size (Java source code)