Java implementation to create a thumbnail, scalable picture proportional generation method _java

Source: Internet
Author: User
Tags save file

This article describes the Java implementation of the creation of thumbnails, scaling image scaling method. Share to everyone for your reference. The implementation methods are as follows:

This instance supports scaling the width, height of the image to the specified width, heights, and saving the proportions of the picture from the size and standard (specified) size of the target object in the specified directory, setting the picture scaling quality, and zooming the picture according to the width you specify.

The specific code looks like this:

Copy Code code as follows:



Package com.hoo.util;





Import Java.awt.Image;


Import Java.awt.image.BufferedImage;


Import Java.io.File;


Import Java.io.FileOutputStream;


Import java.io.IOException;


Import java.net.MalformedURLException;


Import Java.net.URL;


Import Javax.imageio.ImageIO;


Import com.sun.image.codec.jpeg.ImageFormatException;


Import Com.sun.image.codec.jpeg.JPEGCodec;


Import Com.sun.image.codec.jpeg.JPEGEncodeParam;


Import Com.sun.image.codec.jpeg.JPEGImageEncoder;





/**


* <b>function:</b> Zoom Picture Tool class, create thumbnails, telescopic picture proportions


* @author Hoojo


* @createDate 2012-2-3 10:08:47


* @file Scaleimageutils.java


* @package Com.hoo.util


* @version 1.0


*/


Public abstract class Scaleimageutils {





Private static final float default_scale_quality = 1f;


private static final String Default_image_format = ". jpg"; Format of image files


private static final String Default_file_path = "c:/temp-";





/**


* <b>function:</b> Set Image compression quality enumeration class;


* Some guidelines:0.75 High quality, 0.5 medium quality, 0.25 low quality


* @author Hoojo


* @createDate 2012-2-7 11:31:45


* @file Scaleimageutils.java


* @package Com.hoo.util


* @project Jquerymobile


* @version 1.0


*/


public enum ImageQuality {


Max (1.0f), High (0.75f), Medium (0.5f), Low (0.25f);





Private Float quality;


Public Float getquality () {


return this.quality;


}


ImageQuality (Float quality) {


this.quality = quality;


}


}





private static image Image;





/**


* <b>function:</b> calculates the percentage of picture reduction by target object size and standard (specified) size


* @author Hoojo


* @createDate 2012-2-6 04:41:48


* @param the width of the targetwidth target


* @param the height of the targetheight target


* @param standardwidth Standard (specified) width


* @param standardheight Standard (specified) height


* @return the minimum suitable proportion


*/


public static double getscaling (double targetwidth, double targetheight, double standardwidth, double standardheight) {


Double widthscaling = 0d;


Double heightscaling = 0d;


if (Targetwidth > StandardWidth) {


Widthscaling = standardwidth/(Targetwidth * 1.00d);


} else {


widthscaling = 1d;


}


if (Targetheight > StandardHeight) {


Heightscaling = standardheight/(Targetheight * 1.00d);


} else {


heightscaling = 1d;


}


Return Math.min (widthscaling, heightscaling);


}





/**


* <b>function:</b> zoom image width, height to specified width, heights, and save in Savepath directory


* @author Hoojo


* @createDate 2012-2-6 04:54:35


* Width of the zoom of @param width


* Height of @param height scale


* @param savepath Save Directory


* @param targetimage will be scaled to the target picture


* @return Picture save path, name


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, int height, String savepath, Image targetimage) throws Imageformatexception, Ioexce ption {


width = Math.max (width, 1);


Height = Math.max (height, 1);


BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);


Image.getgraphics (). DrawImage (targetimage, 0, 0, width, height, null);





if (Savepath = null | | "". Equals (Savepath)) {


Savepath = Default_file_path + system.currenttimemillis () + Default_image_format;


}





FileOutputStream fos = new FileOutputStream (new File (Savepath));


JPEGImageEncoder encoder = jpegcodec.createjpegencoder (FOS);


Encoder.encode (image);





Image.flush ();


Fos.flush ();


Fos.close ();





return savepath;


}





/**


* <b>function:</b> can set picture scaling quality, and can scale the picture according to the specified width and height


* @author Hoojo


* @createDate 2012-2-7 11:01:27


* Width of the zoom of @param width


* Height of @param height scale


* @param quality picture compression quality, the maximum value is 1; Use enumerated values: {@link imagequality}


* Some guidelines:0.75 High quality, 0.5 medium quality, 0.25 low quality


* @param savepath Save Directory


* @param targetimage will be scaled to the target picture


* @return Picture save path, name


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, int height, Float quality, String Savepath, Image targetimage) throws Imageformatex Ception, IOException {


width = Math.max (width, 1);


Height = Math.max (height, 1);


BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);


Image.getgraphics (). DrawImage (targetimage, 0, 0, width, height, null);





if (Savepath = null | | "". Equals (Savepath)) {


Savepath = Default_file_path + system.currenttimemillis () + Default_image_format;


}





FileOutputStream fos = new FileOutputStream (new File (Savepath));


JPEGImageEncoder encoder = jpegcodec.createjpegencoder (FOS);





JPEGEncodeParam Encodeparam = Jpegcodec.getdefaultjpegencodeparam (image);


if (quality = NULL | | Quality <= 0) {


quality = default_scale_quality;


}


/** Set Picture Compression quality * *


Encodeparam.setquality (quality, true);


Encoder.encode (image, Encodeparam);





Image.flush ();


Fos.flush ();


Fos.close ();





return savepath;


}





/**


* <b>function:</b> calculate the appropriate size of the picture by specifying the size and size of the picture


* @author Hoojo


* @createDate 2012-2-6 05:53:10


* Width specified for @param width


* Height specified by @param height


* @param image Image file


* @return returns an array of int with width and height


*/


public static int[] GetSize (int width, int height, image image) {


int targetwidth = Image.getwidth (null);


int targetheight = Image.getheight (null);


Double scaling = getscaling (Targetwidth, targetheight, width, height);


Long standardwidth = Math.Round (Targetwidth * scaling);


Long standardheight = Math.Round (Targetheight * scaling);


return new int[] {integer.parseint (long.tostring (standardwidth)), Integer.parseint (string.valueof (standardHeight)) };


}





/**


* <b>function:</b> returns an enlarged or reduced width, height by specifying the proportions and picture objects


* @author Hoojo


* @createDate 2012-2-7 10:27:59


* @param scale scaling ratio


* @param image Image Object


* @return return width, height


*/


public static int[] GetSize (float scale, image image) {


int targetwidth = Image.getwidth (null);


int targetheight = Image.getheight (null);


Long standardwidth = Math.Round (targetwidth * scale);


Long standardheight = Math.Round (targetheight * scale);


return new int[] {integer.parseint (long.tostring (standardwidth)), Integer.parseint (string.valueof (standardHeight)) };


}





public static int[] GetSize (int width, image image) {


int targetwidth = Image.getwidth (null);


int targetheight = Image.getheight (null);


Long height = Math.Round ((targetheight * width)/(Targetwidth * 1.00f));


return new int[] {width, integer.parseint (string.valueof (height))};


}





public static int[] Getsizebyheight (int height, image image) {


int targetwidth = Image.getwidth (null);


int targetheight = Image.getheight (null);


Long width = Math.Round ((targetwidth * height)/(Targetheight * 1.00f));


return new int[] {integer.parseint (string.valueof (width)), height};


}





/**


*


* <b>function:</b> shrink the specified targetfile picture file width, height greater than the specified width, height, and save in the Savepath directory


* @author Hoojo


* @createDate 2012-2-6 04:57:02


* @param width narrowed


* Height of @param height reduced


* @param savepath Save Directory


* @param targetimage changes to the target picture


* @return Picture save path, name


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, int height, String savepath, File targetfile) throws Imageformatexception, ioexcept Ion {


Image = Imageio.read (targetfile);


int[] size = getsize (width, height, image);


Return resize (size[0], size[1], savepath, image);


}





/**


*


* <b>function:</b> shrink the specified targeturl network picture file width, height greater than the specified width, height, and save in the Savepath directory


* @author Hoojo


* @createDate 2012-2-6 04:57:07


* @param width narrowed


* Height of @param height reduced


* @param savepath Save Directory


* @param targetimage changes to the target picture


* @return Picture save path, name


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, int height, String savepath, URL targeturl) throws Imageformatexception, Ioexceptio n {


Image = Imageio.read (TargetUrl);


int[] size = getsize (width, height, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b> Zoom a local picture file to a specified scale


* @author Hoojo


* @createDate 2012-2-7 10:29:18


* @param scale scaling ratio


* @param savepath Save file path, name


* @param targetfile Local picture file


* @return The new file name


* @throws imageformatexception


* @throws IOException


*/


public static string resize (float scale, String Savepath, File targetfile) throws Imageformatexception, IOException {


Image = Imageio.read (targetfile);


int[] size = getsize (scale, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b> zoom a network picture file to a specified scale


* @author Hoojo


* @createDate 2012-2-7 10:30:56


* @param scale scaling ratio


* @param savepath Save file path, name


* @param targetfile Local picture file


* @return The new file name


* @throws imageformatexception


* @throws IOException


*/


public static string resize (float scale, string savepath, URL targeturl) throws Imageformatexception, IOException {


Image = Imageio.read (TargetUrl);


int[] size = getsize (scale, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b> Zoom Local Picture According to the fixed width


* @author Hoojo


* @createDate 2012-2-7 10:49:56


* @param width fixed


* @param savepath save path, name


* @param targetfile Local Target file


* @return Return to save path


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, String savepath, File targetfile) throws Imageformatexception, IOException {


Image = Imageio.read (targetfile);


int[] size = getsize (width, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b> Zoom Network picture according to fixed width


* @author Hoojo


* @createDate 2012-2-7 10:50:52


* @param width fixed


* @param savepath save path, name


* @param targetfile Local Target file


* @return Return to save path


* @throws imageformatexception


* @throws IOException


*/


public static String Resize (int width, String savepath, URL targeturl) throws Imageformatexception, IOException {


Image = Imageio.read (TargetUrl);


int[] size = getsize (width, image);


Return resize (size[0], size[1], savepath, image);


}





/**


*


* <b>function:</b> Zoom Local Picture According to the fixed height


* @author Hoojo


* @createDate 2012-2-7 10:51:17


* @param height fixed altitude


* @param savepath save path, name


* @param targetfile Local Target file


* @return Return to save path


* @throws imageformatexception


* @throws IOException


*/


public static String resizebyheight (int height, String savepath, File targetfile) throws Imageformatexception, Ioexceptio n {


Image = Imageio.read (targetfile);


int[] size = getsizebyheight (height, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b> Zoom Network Picture According to the fixed height


* @author Hoojo


* @createDate 2012-2-7 10:52:23


* @param height fixed altitude


* @param savepath save path, name


* @param targetfile Local Target file


* @return Return to save path


* @throws imageformatexception


* @throws IOException


*/


public static String resizebyheight (int height, String savepath, URL targeturl) throws Imageformatexception, IOException {


Image = Imageio.read (TargetUrl);


int[] size = getsizebyheight (height, image);


Return resize (size[0], size[1], savepath, image);


}





/**


* <b>function:</b>


* @author Hoojo


* @createDate 2012-2-3 10:08:47


* @param args


* @throws IOException


* @throws malformedurlexception


* @throws imageformatexception


*/


public static void Main (string[] args) throws Imageformatexception, Malformedurlexception, IOException {





System.out.println (scaleimageutils.resize (140, 140, NULL, new URL ("yun_qi_img/logo.jpg"));


Scaleimageutils.resize (+, ImageQuality.high.getQuality (), NULL, Imageio.read (New URL ("Yun_qi_img/logo.jpg") );


}


}


I hope this article will help you with your Java programming.

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.