JAVA General JPEG miniature map of the production and Examples Bean



  JAVA General JPEG miniature map of the production and Examples Bean 

  Description: 

  Some time ago, a netizen posted online to see a cross-platform Java by the formation Jpeg picture miniature map approach.    Recently compiled to form a common category.    And the production into a jar packets.    To facilitate reuse.    And gives examples of the 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 an JPG / JPEG image files microcosm of the method of handling 

  * That is, to set a JPG file, and can generate a microcosm of the JPG document image files (JPG format) 

  * Providing a microcosm of images generated three different ways: 

  * 1, set up miniature paper width, the width of the basis set and the size of the source image file to determine the length of the new miniature paper to generate miniature image 

  * 2, set the length of microfilm documents, according to the source, and set the length of the size of the image file to determine the width of the new miniature paper to generate miniature image 

  * 3, set up miniature paper compared to the ratio of the source image file size, according to the size of the source image files and settings to determine the proportion of the size of the new miniature paper to generate miniature image 

  * The new generation of miniature images than the original image, then that is the source image to enlarge. 

  * @ Author abnerchai contact: josserchai@yahoo.com 

  * @ Version 1.0 

  * @ Exception JpegToolException 

  * / 

  (Public class JpegTool 

  / / Initialization has been whether 

  Private boolean isInitFlag = false; 

  / / Definition of the source picture with the file name directory path 

  Private String pic_big_pathfilename = null; 

  / / Generate small picture of the store with the file name directory path 

  Private String pic_small_pathfilename = null; 

  / / Definition of generating small picture of a width and height, to one of its on the 

  Private int smallpicwidth = 0; 

  Private int smallpicheight = 0; 

  / / Definition of the small picture than the proportion of the original picture 

  Private double picscale = 0; 

  / ** 

  * Constructor 

  * @ Param no parameters 

  * / 

  Public JpegTool () ( 

  This.isInitFlag = false; 

  ) 

  / ** 

  * Private function, reset all the parameters 

  * @ Param no parameters 

  * @ Return did not return parameters 

  * / 

  Private void resetJpegToolParams () ( 

  This.picscale = 0; 

  This.smallpicwidth = 0; 

  This.smallpicheight = 0; 

  This.isInitFlag = false; 

  ) 

  / ** 

  * @ Param scale settings miniature image of the size of the source image as 0.5 percentage 

  * @ Throws JpegToolException 

  * / 

  Public void SetScale (double scale) throws JpegToolException ( 

  If (scale <= 0) ( 

  Throw new JpegToolException ( "scaling can not be negative and 0!"); 

  ) 

  This.resetJpegToolParams (); 

  This.picscale = scale; 

  This.isInitFlag = true; 

  ) 

  / ** 

  * @ Param smallpicwidth set the width of miniature images 

  * @ Throws JpegToolException 

  * / 

  Public void SetSmallWidth (int smallpicwidth) throws JpegToolException ( 

  If (smallpicwidth <= 0) ( 

  Throw new JpegToolException (the "epitome of the picture width can not be negative and 0!"); 

  ) 

  This.resetJpegToolParams (); 

  This.smallpicwidth = smallpicwidth; 

  This.isInitFlag = true; 

  ) 

  / ** 

  * @ Param smallpicheight set a high degree of miniature images 

  * @ Throws JpegToolException 

  * / 

  Public void SetSmallHeight (int smallpicheight) throws JpegToolException ( 

  If (smallpicheight <= 0) ( 

  Throw new JpegToolException (the "epitome of the high degree of picture can not be negative and 0!"); 

  ) 

  This.resetJpegToolParams (); 

  This.smallpicheight = smallpicheight; 

  This.isInitFlag = true; 

  ) 

  / ** 

  * Generation source image in miniature image 

  * @ Param pic_big_pathfilename source image file names, URLs (such as windows under C: \ \ pic.jpg; under Linux / home / abner / pic / pic.jpg) 

  * @ Param pic_small_pathfilename generation of miniature image file names, URLs (such as windows under C: \ \ pic_small.jpg; under 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 initialization parameters did not!"); 

  ) 

  If (pic_big_pathfilename == null | | pic_small_pathfilename == null) ( 

  Throw new JpegToolException ( "file name contains the path is empty!"); 

  ) 

  If ((! Pic_big_pathfilename.toLowerCase (). EndsWith ( "jpg")) & & (! Pic_big_pathfilename.toLowerCase (). EndsWith ( "jpeg "))){ 

  Throw new JpegToolException ( "can only deal with JPG / JPEG files!"); 

  ) 

  If ((! Pic_small_pathfilename.toLowerCase (). EndsWith ( "jpg")) & & (! Pic_small_pathfilename.toLowerCase (). EndsWith ( "jpeg "))){ 

  Throw new JpegToolException ( "can only deal with JPG / JPEG files!"); 

  ) 

  Int smallw = 0; 

  Int smallh = 0; 

  / / Generate new sources picture and the small picture of the document object 

  File fi = new File (pic_big_pathfilename); 

  File fo = new File (pic_small_pathfilename); 

  / / Object Generation Image Transform 

  AffineTransform transform = new AffineTransform (); 

  / / Read through the source buffer picture file 

  BufferedImage bsrc = null; 

  Try ( 

  Bsrc = ImageIO.read (fi); 

  ) Catch (IOException ex) ( 

  Throw new JpegToolException ( "read image files source mistakes!"); 

  ) 

  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; / / ratio of the length and width of the image 

  If (this.smallpicwidth! = 0) (/ / set the width calculated in accordance with the length of 

  Smallw = this.smallpicwidth; / / The new generation of the length as thumbnails 

  Smallh = (smallw * srch) / srcw; / / The new generation of the width as thumbnails 

  ) Else if (this.smallpicheight! = 0) (/ / set according to the length of the width obtained 

  Smallh = this.smallpicheight; / / The new generation of the length as thumbnails 

  Smallw = (* srcw smallh) / srch; / / The new generation of the width as thumbnails 

  ) Else if (this.picscale! = 0) (/ / set up in accordance with the proportion of narrow set the length and width of image 

  Smallw = (int) ((float) this.picscale srcw *); 

  Smallh = (int) ((float) this.picscale srch *); 

  Else () 

  Throw new JpegToolException ( "object initialization parameters incorrect!"); 

  ) 

  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 proportion of the width of image 

  Double sy = (double) smallh / srch; / / small / large proportion of the high degree of image 

  Transform.setToScale (sx and sy); / / image conversion ratio settings 

  / / Object generated image conversion 

  AffineTransformOp ato = new AffineTransformOp (transform, null); 

  / / Generate a buffer narrowed Image Object 

  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); 

  ) Catch (IOException ex1) ( 

  Throw new JpegToolException ( "into the document as thumbnails wrong!"); 

  ) 

  ) 

  ) 

  2, JpegToolException.java 

  / / JpegToolException.java 

  Package com.abner.jpegtool; 

  / ** 

  * <p> Description: JpegTool abnormal use of </ 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; 

  ) 

  ) 

  Second, the package produced jar 

  Compiler above two documents and two class files are stored in the directory: the current directory \ com \ abner \ jpegtool directory.    Access to the current directory, the Executive: jar cvf jpegtool.jar com / *; in the current directory will generate a jpegtool.jar document.    Published this jar packs, as the future will be able to use other jar, like using the tool kits, Below we give an example. 

  Third, the use of examples 

  In order to facilitate, we are in the JSP document examples of the use of the JSP code package, first of all, you set up the class library path, you will jpegtool.jar document Add to the class library path.    Then set up your application server class library path, jpegtool.jar Add to your application server class library path.    Then test the following JSP code: 

  <!-Test.jsp -> 

  <% @ Page contentType = "text / html; charset = GB2312"%> 

  <% @ Page import = "com.abner.jpegtool.JpegTool"%> 

  <html> <head> <title> Test </ title> </ head> 

  <body Bgcolor="#ffffff"> 

<%

  Try ( 

  JpegTool jpegTool = new JpegTool (); 

  JpegTool.SetSmallWidth (100); 

  JpegTool.doFinal ( "C: \ \ big.jpeg", "C: \ \ small.jpeg"); 

  ) Catch (Exception e) ( 

  E.printStackTrace (); 

  ) 

%>

  </ Body> 

  </ Html> 

  Servlet and Java in the use of procedures similar to the above JSP. 

  Note: The above procedures must be in support JDK1.4 running on the platform.    Otherwise, find some class library. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “JAVA General JPEG miniature map of the production and Examples Bean”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.