Production and example of General JPEG miniature map bean in Java
Note: The previous period of time, I saw a netizen on the online netizens posted a method of generating a JPEG picture transvestment map using Java cross-platform. Recently, I have formed a universal class. And make a JAR package. To reuse convenience. And give an example of use. (Author: abnerchai; Contact: josserchai@yahoo.com) a source code 1, JpegTool.java // JpegTool.java package com.abner.jpegtool; import javax.imageio.ImageIO; import javax.imageio.IIOException; import java .awt.image.BufferedImage; import java.awt.Image; import java.io.File; import java.awt.image.AffineTransformOp; import java.awt.geom.AffineTransform; import com.abner.jpegtool.JpegToolException; import java .io. *; / ** * This class implements a method of changing the JPG / JPEG image file * is given a JPG file, which can generate a miniature image file (JPG format) of the JPG file * provides three Methods of generating a micro-shadow image: * 1 Set the width of the regimist file, determine the length of the new zoom shadow file to generate a zoom shadow image * 2 according to the size of the set width and source image file, set the length of the transvestment file, according to the length of the setting and The sizes of the source image file determine the width of the new regam file to generate a zoom shadow image * 3, set the proportion of the regulation file relative to the source image file, determine the size of the new episode file according to the size of the source image file. Regaminal image * The newly generated zotation image can be larger than the original image, that is, an enlarged source image.
* @Author abnerchai contact: josserchai@yahoo.com * @version 1.0 * @exception JpegToolException * / public class JpegTool // object is already initialized private boolean isInitFlag = false; // belt path to define the source directory where the picture file name Private string pic_big_pathfilename = NULL; // Generate a small picture of the file name private string pic_small_pathfilename = null; // Define the width and height of the small picture, give it a private int smallpicWidth = 0; private INT SmallPicHeight = 0; // Define the proportion of the small picture compared to the original picture PicsCale = 0; / ** * Constructor * @Param no parameters * / public jpegtool () {this.isinitflag = false; {} / * * * Private functions, reset all parameters * @Param no parameters * @return no return parameter * / private void resetjpegtoolparams () this.picscale = 0; this.smallPICWIDTH = 0; this.smallPicHEight = 0; this.isinitflag = false; {} / ** * @param scale miniature image disposed with respect to the source image size ratio, such as 0.5 * @throws JpegToolException * / public void setScale (double scale) throws JpegToolException if (scale <= 0) {throw new JpegToolException ( "Zoom ratio cannot be 0 and negative!"); {} This.resetjpegtoolparams (); this.picscale = scale; this.isi nitFlag = true; Width} / ** * @param smallpicwidth disposed miniature image width * @throws JpegToolException * / public void SetSmallWidth (int smallpicwidth) throws JpegToolException if (smallpicwidth <= 0) {throw new JpegToolException ( "miniature picture can not be 0 and negative numbers! "); {} This.resetJpegToolParams (); this.smallpicwidth = smallpicwidth; this.isInitFlag = true;} / ** * @param smallpicheight set height * @throws JpegToolException miniature image * / public void SetSmallHeight (int smallpicheight) throws JPEGTOOLEXCEPTION IF (SmallPicHeight <= 0) {throw new jpegtoolexception ("The height of the microcosmic picture cannot be 0 and negative numbers!
"); {} This.resetjpegtoolparams (); this.smallpicHeight = smallpicHeight; this.isinitflag = true;} / ** * generated source images * @PARAM PIC_BIG_PATHFILENAME Source image file name, including path (such as Windows C) : /pic.jpg; Linux /Home/abner/pic/pic.jpg) * @PAram pic_small_pathFileName The generated zoom image file name, contains paths (under Windows C: /Pic_Small.jpg; Linux / Home / Abner / pic / pic_small.jpg) * @throws JpegToolException * / public void doFinal (String pic_big_pathfilename, String pic_small_pathfilename) throws JpegToolException if (this.isInitFlag) {throw new JpegToolException ( "object parameter is not initialized!");! {} if (pic_big_pathfilename == null || pic_small_pathfilename == null) throw new jpegtoolexception ("The path containing the file name is empty!"); {} if (((}). Endswith ("JPG") && ("JPG") && (! pic_big_pathfilename. TOLOWERCASE (). Endswith ("JPEG")) "" JPG / JPEG File! "); {} if ((! pic_small_pathfilename.tolowercase (). Endswith (" JPG ") && (! Pic_small_pathfilename.tolowercase (). endswith ("jpeg")) "throw new JPEGTOOLEXCEPTION (" can only Handle JPG / JPEG file! "); {} Int smallw = 0; int smallh = 0; // New source picture and generated small picture file object file fi = new file (PIC_BIG_PATHFILENAME); file fo = new file (pic_small_pathfilename); // Generate an image Transformation object AffineTransform Transform = new affineTransform (); // Read In Source Image File BufferedImage Bsrc = Null; Try Bsrc = Imageio.READ (} Catch (IOException EX) Throw new JPEGTOEXCEPTION ("Read source image File error! "); {} Int srcw = bsrc.getWidth (); // The length of the original image INT SRCH = bsrc.getHeight (); // The width of the original image Double scale =
(Double) SRCW / SRCH; // The long-width ratio IF (this.smallpicwidth! = 0) // determines the length SmallW = this.smallpicWidth; // of the newly generated thumbnail image according to the set width = (Smallw * srch) / srcw; // The width of the newly generated thumbnail image {} else if (this.smallpicHeight! = 0) // Depending on the set length, the width smallh = this.smallPICHEIGHT; / / New The length of the generated thumbnail image is smallw = (SMALLH * SRCW) / srch; / / The width of the newly generated thumbnail image {} else if (this.picscale! = 0) // Set the length of the image according to the set reduction ratio And wide smallw = (int) ((float) srcw * this.picscale); smallh = (int) (float) srch * this.picscale); {} else throw new JPEGTOOLEXCEPTION ("The object parameter is initialized!") ; {} System.out.println ("Source File Size:" SRCW "X" SRCH); System.out.Println ("New File Size:" Smallw "X" Smallh); Double SX = (Double ) smallw / srcw; // small / large image Width ratio double Sy = (double) Smallh / srch; // Small / large image height scale transform.Settoscale (Sy); // Setting the ratio of image conversion / / image converter generates the operation target AffineTransformOp ato = new AffineTransformOp (transform, null); // generates a reduced image of the object buffer BufferedImage bsmall = new BufferedImage (smallw, smallh, BufferedImage.TYPE_3BYTE_BGR); // make small images ato.filter (bsrc , bsmall); // Output small image TRY imageIO.WRITE (BSMALL, "JPEG", FO); {} Catch (IOException EX1) Throw new JPEGTOEXCEPTION ("Write thumbnail image file error! "); {}}} 2, jpegtoolexception.java // jpegtoolexception.java package com.abner.jpegtool; / ** *
Description: JPEGTOOL uses exception class p> * @Author abnerchai * @version 1.0 * / public class JpegToolException extends Exception private String errMsg = ""; public JpegToolException (String errMsg) {this.errMsg = errMsg; {} public String getMsg () return "JpegToolException:" this.errMsg; {}} two, jar Packages make two files above, form two Class files, all in the directory: