Java generate image thumbnail image

Source: Internet
Author: User

/** * */ Packagecom.fkhwl.fkhserver.core.utils;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJavax.imageio.ImageIO;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder;/*** @ClassName: Thumbnailtools * @Description: Thumbnail generation tool *@author* @date September 25, 2014 PM 5:18:33*/ Public classThumbnailtools {Private intfileSize; PrivateString Inpath;//Input Diagram Path    PrivateString Outpath;//Output Diagram Path    Private intwidth = 100;//Default output image width    Private intheight = 100;//default output Picture High    PrivateString Infilename;//input Diagram file name    PrivateString Outfilename;//output diagram file name    Private Booleanproportion =true;//is equal to the scaling mark (default ratio scaling)    PrivateString prefix = "Thumbnail_";  Public intgetwidth () {returnwidth; }      Public voidSetWidth (intwidth) {         This. width =width; }      Public intgetheight () {returnheight; }      Public voidSetHeight (intheight) {         This. Height =height; }          PublicThumbnailtools SetSize (intsize) {         This. width =size;  This. Height =size; return  This; }          PublicThumbnailtools SetSize (intWidthintheight) {         This. width =width;  This. Height =height; return  This; }          PublicString Getinpath () {returnInpath; }      Public voidSetinpath (String inpath) { This. Inpath =Inpath; }      PublicString Getoutpath () {returnOutpath; }      Public voidSetoutpath (String outpath) { This. Outpath =Outpath; }      Public Booleanisproportion () {returnproportion; }      Public voidSetproportion (Booleanproportion) {         This. proportion =proportion; }          Publicthumbnailtools (String path) { This. Inpath =path;  This. Outpath =path; }      Publicthumbnailtools (String inpath, String outpath) { This. Inpath =Inpath;  This. Outpath =Outpath; }         /*** Generate thumbnail image *@paramFileName *@returnBoolean*/     Public BooleanGenerate (String fileName)throwsexception{ This. Generate (FileName,NULL); returnboolean.true; }     /*** Generate thumbnail image *@paramFileName *@paramoutfilename Output File name *@returnBoolean*/     Public BooleanGenerate (String fileName, String outfilename)throwsexception{File File=NewFile (inpath+fileName);  This. Infilename =FileName;  This. Outfilename =NULL= = Outfilename? prefix+Infilename:outfilename;  This. Execute (Newfileinputstream (file)); returnboolean.true; }          Public BooleanGenerate (InputStream InputStream, String Outpath)throwsexception{ This. Execute (inputstream); returnboolean.true; }         Private voidExecute (InputStream inputstream)throwsException { This. fileSize =inputstream.available (); BufferedImage Oldimage=Imageio.read (InputStream); if(oldimage.getwidth () = = 1) {System.out.println ("Input image cant ' t read or format error."); return; }                 intNewwidth; intNewheight; //whether it is equal to scale        if( This. proportion = =true) {            //image width and height for equal to scale calculation            DoubleRate1 = ((Double) Oldimage.getwidth ())/(Double) width; DoubleRate2 = ((Double) Oldimage.getheight ())/(Double) height; //scale control based on large scale ratios            DoubleRate = rate1 > rate2?Rate1:rate2; Newwidth= (int) (((Double) Oldimage.getwidth ())/Rate ); Newheight= (int) (((Double) Oldimage.getheight ())/Rate ); } Else{newwidth= width;//picture width of outputNewheight = height;//picture height of output} bufferedimage Tag=NewBufferedImage ((int) Newwidth, (int) Newheight, BUFFEREDIMAGE.TYPE_INT_RGB); //Image.scale_smooth's thumbnail algorithm produces thumbnail images with higher priority than high-speed image quality but slowerTag.getgraphics (). DrawImage (Oldimage.getscaledinstance (Newwidth, Newheight, Image.scale_smooth), 0, 0,NULL); String Newfilepath= outpath+Outfilename; File NewFile=NewFile (Newfilepath); FileOutputStream out=NewFileOutputStream (NewFile); //JPEGImageEncoder can be used for conversion of other picture typesJPEGImageEncoder encoder =Jpegcodec.createjpegencoder (out);        Encoder.encode (tag);        Inputstream.close ();                 Out.close (); System.out.println (Newfilepath+ "OK, size:" + (filesize/1024) + "KB to" + (Newfile.length ()/1024) + "KB"); }          Public Static voidMain (string[] args) {Try {            NewThumbnailtools ("d:/"). SetSize. Generate ("1.jpg", "2.jpg"); } Catch(Exception e) {e.printstacktrace (); }    }}

You may get an error when running, because the program requires Java two JAR package support

working with images in eclipse requires the introduction of two packages:
import Com.sun.image.codec.jpeg.JPEGCodec;
import Com.sun.image.codec.jpeg.JPEGImageEncoder;
Error:
Access restriction:the type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6. 0_07\lib\rt.jar


eclipse By default sets these access-restricted APIs to error. Just put windows-preferences-java-complicer-errors/warnings inside the deprecated and restricted API in forbidden references (access Rules) can be compiled by selecting the warning.

Java generate picture thumbnails

Related Article

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.