Production and example of General JPEG miniature map bean in Java

zhaozj2021-02-12  150

JAVA Multi-purpose JPEG Motural Chart Bean Production and Example Description: Previous time, I saw a net friend to post a method of generating a JPEG image transvestment with 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.

// definition of the source file with the path where the picture directory; josserchai@yahoo.com * @version 1.0 * @exception JpegToolException * / public class JpegTool {// if the object is already initialized isInitFlag = false private boolean: * @author abnerchai contact 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; // Defines the proportion of the small picture primate double 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 =;} / ** * @param smallpicwidth disposed miniature image width * @throws JpegToolException * / public void SetSmallWidth (int smallpicwidth) throws JpegToolException of {if (smallpicwidth <= 0) {throw new JpegToolException ( "Image Width miniature Can't be 0 and negative! ");} 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 JPEGTOEXCEPTION ("The height of the episode picture cannot be 0 and negative!

");} this.resetjpegtoolparams (); this.smallpicHeight = SmallPicHeight; this.isinitflag = true;} / ** * Generating source image The epitometric image of the source image * @PARAM PIC_BIG_PATHFILENAME Source image file name, containing paths (such as Windows C: //pic.jpg; Linux /Home/abner/pic/pic.jpg) * @Param pic_small_pathFileName The generated miniature image file name, including the path (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 ( "path including the file name is empty!");}!. if ((pic_big_pathfilename.toLowerCase () endsWith ( "jpg"))! && (pic_big_pathfilename .tolowercase (). Endswith ("JPEG"))) {throw new JPEGTOEXCEPTION ("JPG / JPEG file can only be handled!");} if ((! pic_small_pathfilename.tolowercase (). Endswith ("JPG") && ! pic_small_pathfilename.tolowercase (). Endswith ("JPEG")) {throw new jpegtoolexception (" Can only handle JPG / JPEG files! ");} 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 = NUFFEREDIMAGE BSRC = NUFFEREDIMAGE BSRC = NUFFEREDIMAGE BSRC = NUFFEREDIMAGE BSRC = NUFFEREDIMAGE BSRC = NUFFEREDIMAGE BSRC = NULL; Try {BSRC = Imageio.Read (} catch (});} catch (ion) {throw new jpegtoolexception 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 of the length SmallW = this.smallpicWidth; // according to the set width; // The length of the newly generated thumbnail image Smallh = (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; // The length smallw = (smallh * srcw) / srch; // of the newly generated thumbnail image} Else if (this.picscale! = 0) {// Set the image according to the reduction ratio of the setting Long and wide smallw = ((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 jpegtoolexception ("Write thumbnail image file error! ");}}} 2, jpegtoolexception.java // jpegtoolexception.java package com.abner.jpegtool; / ** *

Description: JPEGTOOL uses exception class * @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 package Making compilation above two files, forming two class files, all in the directory:

转载请注明原文地址:https://www.9cbs.com/read-6390.html

New Post(0)