Java automatically generates thumbnail images __java

Source: Internet
Author: User

First, automatically generate thumbnails method:

Package writeimg;
Import Java.awt.geom.AffineTransform;
Import java.awt.image.AffineTransformOp;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;

Import Javax.imageio.ImageIO; public class Jpegtool {private Boolean isinitflag = false;//Whether the object has already been initialized private String Pic_big_ Pathfilename = null; Defines the filename with the path directory that contains the source picture private String pic_small_pathfilename = null; Create a small picture of the file name with the path directory private int smallpicwidth = 0; 
        Define the width and height of the generated small picture, give it one can be private int smallpicheight = 0;
        private int pic_big_width=0;
        private int pic_big_height=0; Private double Picscale = 0;
                Defines the proportions of a small picture compared to the original picture/** * constructor * @param no parameters/public Jpegtool () { 
        This.isinitflag = false; /** * Private Function, reset all parameters * @param no parameters * @return no return parameters/private void Resetjpegtoolparams () {This.piCscale = 0; 
                this.smallpicwidth = 0; 
                this.smallpicheight = 0; 
        This.isinitflag = false; /** * @param scale set miniature image relative to the source image size ratio such as 0.5 * @throws jpegtoolexception * * PU 
                                Blic void Setscale (double scale) throws Jpegtoolexception {if (scale<=0) { throw new Jpegtoolexception ("Scaling cannot be 0 and negative.") 
                "); 
                } this.resetjpegtoolparams (); 
                This.picscale = scale; 
        This.isinitflag = true; /** * @param smallpicwidth Set the width of the miniature image * @throws jpegtoolexception * * Public
                void setsmallwidth (int smallpicwidth) throws Jpegtoolexception {if (smallpicwidth<=0) {throw new Jpegtoolexception ("Miniature pictures cannot be 0 and negative.") 
                "); } this.resetjpegtoolparams (); 
                This.smallpicwidth = Smallpicwidth; 
        This.isinitflag = true; /** * @param smallpicheight Sets the height of the miniature image * @throws jpegtoolexception * * Publ
                IC void setsmallheight (int smallpicheight) throws Jpegtoolexception {if (smallpicheight<=0) {throw new Jpegtoolexception ("miniature pictures cannot have a height of 0 and a negative number.") 
                "); 
                } this.resetjpegtoolparams (); 
                This.smallpicheight = Smallpicheight; 
        This.isinitflag = true;
                /** * Returns the large picture path/public String Getpic_big_pathfilename () {
        return this.pic_big_pathfilename; /** * Returns the small picture path/public String Getpic_small_pathfilename () {R
        Eturn This.pic_small_pathfilename;
                public int GETSRCW () {return this.pic_big_width;
        public int Getsrch () {return this.pic_big_height; /** * Generates a miniature image of the source image * @param pic_big_pathfilename source image file name containing the path (such as Windows c:\\pic.jpg under Linux/ home/abner/pic/pic.jpg) * @param pic_small_pathfilename generated miniature image file name containing the path (such as Windows c:\\pic_small.jpg under Linux /home/abner/pic/pic_small.jpg) * @throws jpegtoolexception */public void dofinal (String pic_b 
                    Ig_pathfilename,string pic_small_pathfilename) throws Jpegtoolexception {if (!this.isinitflag) { throw new Jpegtoolexception ("Object parameter not initialized.") 
                "); } if (Pic_big_pathfilename==null | | pic_small_pathfilename==null) {throw new Jpegtool Exception ("The path containing the filename is empty.) 
                "); The If ((!pic_big_pathfilename.tolowercase (). EndsWith ("JPG")) && (!pic_big_pathfilename.tolowercase ( ). EndsWith ("JPEG")) { 
                    throw new Jpegtoolexception ("Only jpg/jpeg files can be processed.") 
                "); if (!pic_small_pathfilename.tolowercase (). EndsWith ("JPG")) &&!pic_small_pathfilename.tolowerca Se (). EndsWith ("JPEG")) {throw new Jpegtoolexception ("Only jpg/jpeg files can be processed.") 
                ");
                } this.pic_big_pathfilename=pic_big_pathfilename;
                This.pic_small_pathfilename=pic_small_pathfilename; 
                int SMALLW = 0; 
                int SMALLH = 0; 
                A new source picture and a generated small picture of the file object files fi = new file (pic_big_pathfilename); 
                File fo = new file (pic_small_pathfilename); 
                Generate image Transform object AffineTransform transform = new AffineTransform (); 
                Read into the source image file by buffering bufferedimage bsrc = null; 
                try {bsrc = Imageio.read (FI); }catch (IOException ex) {throw new JpegtOolexception ("Error reading source image file. 
                "); This.pic_big_width= bsrc.getwidth ()//The length of the original image this.pic_big_height = Bsrc.getheight (); /The width of the original image double scale = (double) pic_big_width/pic_big_height;//the long width ratio of the image if (this.smallpic 
                        width!=0) {//Length SMALLW = this.smallpicwidth;//The length of the newly generated thumbnail image based on the set width SMALLH = (smallw*pic_big_height)/pic_big_width//The width of the newly generated thumbnail image} else if (thi 
                        s.smallpicheight!=0) {//Width SMALLH = this.smallpicheight;//The length of the newly generated thumbnail image according to the set length
                SMALLW = (smallh*pic_big_width)/pic_big_height;//the width of the newly generated thumbnail image} else if (this.picscale!=0) {//sets the length and width smallw = (int) (float) of the image according to the set reduction scale Pic_big_wid 
                        Th*this.picscale); 
 SMALLH = (int) ((float) pic_big_height*this.picscale);               else {throw new Jpegtoolexception ("Object parameter initialization is not correct.") 
                "); Double SX = (double) smallw/pic_big_width;//width ratio of small/large images double sy = (double) Smallh/pic_bi 
                g_height;//the height ratio of small/large images transform.settoscale (sx,sy);//Set the scale of the image conversion//Generate image conversion manipulation objects 
                Affinetransformop ato = new Affinetransformop (transform,null); 
                The buffer object that produces the shrinking image bufferedimage Bsmall = new BufferedImage (SMALLW,SMALLH,BUFFEREDIMAGE.TYPE_3BYTE_BGR); 
                Generate small Image ato.filter (Bsrc,bsmall); 
                Output small image try{Imageio.write (bsmall, "jpeg", FO);  The catch (IOException Ex1) {throw new Jpegtoolexception ("error writing to the thumbnail image file. 
                "); } 
        }
}

Second, the exception processing class:

    Package jpegtool;

    public class Jpegtoolexception extends Exception {
            private String errmsg = ""; 
            Public jpegtoolexception (String errmsg) 
            { 
                    this.errmsg = errmsg; 
            } 

            Public String getmsg () {return 
                "jpegtoolexception:" +THIS.ERRMSG; 
            } 
    }

Third, the invocation of the example:

Package writeimg;

public class T {public


	static void Main (string[] args) {

		Jpegtool j = new Jpegtool ();
		try {
			J.setscale (0.7);
			J.setsmallheight (m);
			J.dofinal ("D:\\305\\c\\javatest\\src\\11.jpg", "d:\\305\\c\\javatest\\src\\22.jpg");
		} catch (Jpegtoolexception e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		}
	}

}




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.