Production and example of General JPEG miniature map bean in Java
Description:
A fewer time, I saw a net friend to post 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)
First, 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 challenged the JPG / JPEG image file.
* You give a JPG file, you can generate a zoom image file of the JPG file (JPG format)
* Provides three methods for generating a microcosm:
* 1, set the width of the regulation file, determine the length of the new episode file according to the size of the set width and source image file to generate a zoom shadow image
* 2, set the length of the regimen file, determine the width of the new episode file according to the size of the set length and the source image file to generate a zoom shadow image
* 3, set the proportional size of the regimen file relative to the source image file, determine the size of the new episode file according to the size of the source image file, and the size of the settings.
* 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 JPEGTOEXCEPTION
* /
Public class jpegtool {
// Whether the object is initialized
Private boolean isinitflag = false;
/ / Define the file name of the belt path directory in the source picture
PRIVATE STRING PIC_BIG_PATHFILENAME = NULL;
/ / Generate a file name of a small picture
PRIVATE STRING PIC_SMALL_PATHFILENAME = NULL;
// Define the width and height of the small picture, give it one.
Private int smallpicWidth = 0;
Private int smallpicHeight = 0;
/ / Define the proportion of the original picture
Private double picscale = 0;
/ **
* Constructor
* @Param no parameters
* /
Public JPEGTOOL () {
THISINITFLAG = FALSE;
}
/ **
* Private functions, reset all parameters
* @Param no parameters
* @return has no return parameters
* /
Private void resetjpegtoolparams () {
THIS.PICSCALE = 0;
THIS.SMALLPICWIDTH = 0;
THIS.SMALLPICHEIGHT = 0;
THISINITFLAG = FALSE;
}
/ **
* @Param Scale Sets the size ratio of the zoom image relative to the source image, 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 numbers!");
}
THIS.RESETJPEGTOOLPARAMS ();
THIS.PICSCALE = Scale;
THISINITFLAG = TRUE;
}
/ **
* @Param SmallPicWidth Sets the width of the zoom image
* @Throws JPEGTOOLEXCEPTION
* /
Public void setsmallwidth (int smallpicwidth) THROWS JPEGTOEXCEPTION {
IF (SmallPiIDTH <= 0) {
Throw new JPEGTOEXCEPTION ("The width of the registra image cannot be 0 and negative!");
}
THIS.RESETJPEGTOOLPARAMS ();
THIS.SMALLPICWIDTH = smallpicwidth;
THISINITFLAG = TRUE;
}
/ **
* @Param SmallPicHeight Sets the height of the epitometric image
* @Throws JPEGTOOLEXCEPTION
* /
Public void setsmallheight (int smallpicheight) throws JPEGTOEXCEPTION {
IF (SmallPicHeight <= 0) {
Throw new JPEGTOOLEXCEPTION ("The height of the epitome picture cannot be 0 and negative!");
}
THIS.RESETJPEGTOOLPARAMS ();
THIS.SMALLPICHEIGHT = SMALLPICHEIGHT;
THISINITFLAG = TRUE;
}
/ **
* Zeveloped image of the source image
* @PARAM PIC_BIG_PATHFILENAME Source Image File Name, contains paths (such as Windows C: //pic.jpg; Linux /Home /abner/PIC/PIC.JPG)
* @Param pic_small_pathFileName generated zotation image file name, including path (such as 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 ("The object parameters are not initialized!");
}
IF (PIC_BIG_PATHFILENAME == Null || Pic_small_pathfilename == NULL) {
Throw new JPEGTOEXCEPTION ("Contains the path to the file name is empty!");
}
IF ((! PIC_BIG_PATHFILENAME.TOLOWERCASE (). Endswith ("JPG")) && (! pic_big_pathfilename.tolowercase (). endswith ("jpeg")) {throw new jpegtoolexception ("can only handle JPG / JPEG files!") ;
}
IF ((! pic_small_pathfilename.tolowercase (). Endswith ("JPG")) && (! pic_small_pathfilename.tolowercase (). Endswith ("JPEG"))) {
Throw new JPEGTOOLEXCEPTION ("You 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 transform object
AffineTransform Transform = New AffineTransform ();
// Read the source image file by buffering
BufferedImage BSRC = NULL;
Try {
BSRC = imageio.read (fi);
} catch (ioException ex) {
Throw new JPEGTOOLEXCEXCEPTION ("Read the source image file error!");
}
INT SRCW = BSRC.GETWIDTH (); // The length of the original image
INT SRCH = BSRC.GetHeight (); // Width of the original image
Double scale = (double) srcw / srch; // aspect ratio
IF (this.smallpicWidth! = 0) {// Depending on the width of the set
Smallw = this.smallpicWidth; // 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) {// request the width according to the set length
Smallh = this.smallpicHeight; // The length of the newly generated thumbnail image
Smallw = (smallh * srcw) / srch; // The width of the newly generated thumbnail image
} else if (this.picscale! = 0) {// Set the length and width of the image according to the reduction ratio set
Smallw = (int) (FLOAT) SRCW * this.picscale);
Smallh = (int) (FLOAT) SRCH * THISPICSCALE);
} else {
Throw new JPEGTOOLEXCEPTION ("The object parameters are 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 / SRCH; // Small / large image Height ratio transform.Settoscale (SX, SY); // Setting the ratio of image conversion
/ / Generate an image conversion operation object
AffineTransformop ATO = New AffineTransformoP (Transform, NULL);
/ / Generate a buffer object of the reduced image
BufferedImage Bsmall = New BufferedImage (Smallw, Smallh, BufferedImage.Type_3byTe_bgr);
/ / Generate a small image
ATO.FILTER (BSRC, BSMALL);
// Output small image
Try {
Imageio.write (BSMall, "JPEG", FO);
} catch (ioexception ex1) {
Throw new JPEGTOEXCEPTION ("Write a thumbnail image file error!");
}
}
}
2, JPEGTOOLEXCEPTION.JAVA
// jpegtoolexception.java
Package com.abner.jpegtool;
/ **
*
Description: Anomaly class used by JPEGTOOL 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 "JPEGTOEXCEPTION:" this.errmsg;
}
}
Second, JAR package production
Compile the above two files to form two class files, all in the directory: the current directory / COM / ABNER / JPEGTOOL directory below. Enter the current directory, execute: JAR CVF JPEGTOOL.JAR COM / *; generates a JPEGTOOL.jar file in the current directory. Release this JAR package, you can use this tool as other JAR package, and we will give an example.
Third, use example
For convenience, we give example JSP code in the JSP file, first, set your class library path, put the JPEGTOOL.jar file into your class library path. Then set the class library path for your application server, put JPEGTOOL.jar into the class library path of your application server. Then test the following JSP code:
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "com.abner.jpegtool.jpegtool"%>
<%
Try {
JPEGTOOL JPEGTOOL = New JPEGTOOL ();
JPEGTOOL.SETSMALLWIDTH (100);
JPEGTOOL.DOFINAL ("c: //big.jpeg", "c: //small.jpeg");} catch (exception e) {
E.PrintStackTrace ();
}
%>
body>
html>
For use in servlet and Java programs, similar to the JSP above.
Note: The above programs must be run on the platform that supports JDK1.4. Otherwise some class libraries cannot be found.