Production and example of General JPEG miniature map bean in Java

zhaozj2021-02-12  162

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

* @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"%>

test </ title> </ head></p> <p><body bgcolor = "# ffffff"></p> <p><%</p> <p>Try {</p> <p>JPEGTOOL JPEGTOOL = New JPEGTOOL ();</p> <p>JPEGTOOL.SETSMALLWIDTH (100);</p> <p>JPEGTOOL.DOFINAL ("c: //big.jpeg", "c: //small.jpeg");} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>}</p> <p>%></p> <p></ body></p> <p></ html></p> <p>For use in servlet and Java programs, similar to the JSP above.</p> <p>Note: The above programs must be run on the platform that supports JDK1.4. Otherwise some class libraries cannot be found.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6276.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="6276" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.053</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'mUWCJF7_2BW3ITX4tgR9skF7NdNet7V_2Buz4IvjPkc_2B_2FU1R43xfNh7JItlevW4wlMeQNp1p_2Fp8U7msUkgaaWc6LBQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>