Java Generate thumbnails

Source: Internet
Author: User

@author Gzkui

@date 2015/1/15

@ Friends Circle Project

Package Qflag.ucstar.plugin.ucmbfriends.utils;import Java.awt.image;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.fileoutputstream;import Javax.imageio.imageio;import Com.sun.image.codec.jpeg.jpegcodec;import Com.sun.image.codec.jpeg.jpegimageencoder;public Class ucmimagechangetest {public static void main (string[] args) throws Exception {FileOutputStream fileoutputstream = null; JPEGImageEncoder encoder = null; BufferedImage Tagimage = Null;image srcimage = null; String dstimagefilename = "E:\\sss\\sgseg.jpeg"; File Imbig = new file ("C:\\users\\gzkui\\desktop\\0019d18398280c9fb6020a.jpg"); if (Imbig.exists () && Imbig.isfile ()) {srcimage = Imageio.read (imbig); int srcwidth = Srcimage.getwidth (null);//original picture width int srcheight = Srcimage.getheight (null);//original image height int dstmaxsize = 120;//target thumbnail maximum width/height, width and height proportionally abbreviated int dstwidth = srcwidth;//thumbnail width int Dstheight = srcheight;//thumbnail height float scale = 0;           if (srcwidth>dstmaxsize) {dstwidth = dstmaxsize;     Scale = (float) srcwidth/(float) dstmaxsize;            Dstheight = Math.Round ((float) srcheight/scale);            } srcheight = Dstheight;                if (srcheight>dstmaxsize) {dstheight = dstmaxsize;                Scale = (float) srcheight/(float) dstmaxsize;            Dstwidth = Math.Round ((float) dstwidth/scale);            }//Generate thumbnail image tagimage = new BufferedImage (DSTWIDTH,DSTHEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);            Tagimage.getgraphics (). DrawImage (Srcimage,0,0,dstwidth,dstheight,null);            FileOutputStream = new FileOutputStream (dstimagefilename);            Encoder = Jpegcodec.createjpegencoder (FileOutputStream);            Encoder.encode (Tagimage);            Fileoutputstream.close ();            FileOutputStream = null;                if (fileoutputstream!=null) {try{fileoutputstream.close (); }catch (Exception e) {} FileOutputStream = nUll            } encoder = null;            Tagimage = null;            Srcimage = null; System.GC (); return;} SYSTEM.OUT.PRINTLN ("File not Found");}}

Java Generate thumbnails

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.