Proportional scaling of pictures

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.