import java.awt.color;import java.awt.graphics;import java.awt.image;import Java.awt.image.bufferedimage;import java.awt.image.convolveop;import java.awt.image.kernel;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.imageicon;import com.sun.image.codec.jpeg.jpegcodec;import com.sun.image.codec.jpeg.jpegencodeparam;import com.sun.image.codec.jpeg.jpegimageencoder;/** * Image scaling Tools * @Author vitoHuang * @Time 2015 August 12 * @Mark */ public class imageutil {/** * * @param originalfile Photo file * @param resizedfile after zooming file * @throws IOException */public static void Resize (file originalfile, file resizedfile) throws ioexception {resize (OriginalFile, resizedfile,640);} /** * * @param originalfile Original picture file * @param resizedfile After zooming file * @param newwidth width units:px * @throws ioexception */public static void resize (file originalfile, file Resizedfile,int newwidth) Throws ioexception {resize (originalfile, resizedfile, NEWWIDTH,1F);} /** * * @param originalFile Original picture file * @param resizedfile zoom file * @param newwidth Zoom after width Unit:px * @param quality quality 0-1 * @ Throws ioexception */public static void resize (file originalfile, file resizedfile,int newwidth, float quality) throws IOException {if ( quality > 1) throw new illegalargumentexception ("Quality must be between 0 and 1"); Imageicon ii = new imageicon (Originalfile.getcanonicalpath ()); Image i = ii.getimage (); image resizedimage = null; int Iwidth = i.getwidth (NULL); int Iheight = i.getheight (NULL); //according to the new picture width provided if ( Iwidth > iheight) { resizedimage = i.getscaledinstance (newwidth, (newwidth * iheight) / iwidth, image.scale_smooth); }else{ resizedImage = I.getscaledinstance ((NEWWIDTH *&NBsp;iwidth) / iheight,newwidth, image.scale_smooth); } //ensure that all pixels in the image are loaded Image temp = New imageicon (resizedimage). GetImage (); //Create a picture cache BufferedImage Bufferedimage = new bufferedimage (Temp.getwidth (null), temp.getheight (null), BUFFEREDIMAGE.TYPE_INT_RGB); //copy image buffer images Graphics g = Bufferedimage.creategraphics (); g.setcolor (CoLor.white); g.fillrect (0, 0, Temp.getwidth (NULL), temp.getheight (null)); G.drawimage (temp, 0, 0, null); G.dispose (); float softenFactor = 0.05f; float[] softenarray = { 0, softenfactor, 0, softenfactor,1 - (softenfactor * 4), softenfactor, 0, softenfactor, 0 }; kernel kernel = new kernel (3, 3, softenarray); convolveop cop = new convolveop (Kernel, convolveop. Edge_no_op, null); bufferedimage = Cop.filter (bufferedimage, null); fileoutputstream out = new fileoutputstream ( Resizedfile); // encoded image as JPEG data stream JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); Jpegencodeparam param = encoder.getdefaultjpegencodeparam (BufferedImage); param.setquality (quality, true); encoder.setjpegencodeparam (param); encoder.encOde (bufferedimage); }}
Proportional scaling of pictures