Creation and example of common JPEG thumbnail bean in Java

Source: Internet
Author: User
Creation and example of common JPEG thumbnail bean in Java


Note:
Some time ago, I saw a netizen posted a method for generating a JPEG image thumbnail across platforms using Java. Recently, a general class has been formed. Create a jar package. To facilitate reuse. Examples are provided. (Author: abnerchai; contact: josserchai@yahoo.com)

I. Source Code
1. Using tool. Java
// Producer tool. Java

Package com.abner.jpeg tool;

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.jpeg tool. Export toolexception;

Import java. Io .*;

/**

* This class implements a microcosm Processing Method for JPG/JPEG image files.

* If a JPG file is specified, a thumbnail image file (in JPG format) of the JPG file can be generated)

* Three methods are provided to generate a microcosm image:

* 1. Set the width of the miniature file, and determine the length of the new miniature File Based on the configured width and source image file size to generate the miniature image.

* 2. Set the length of the miniature file, and determine the width of the new miniature File Based on the configured length and the size of the source image file to generate the miniature image.

* 3. Set the ratio of the mini-file to the source image file, and determine the size of the new micro-file based on the size of the source image file and the proportion set to generate the micro-image.

* The newly generated miniature image can be larger than the original image. In this case, a large source image is placed.

* @ Author abnerchai contact: josserchai@yahoo.com

* @ Version 1.0

* @ Exception when toolexception

*/

Public class development tool

// Whether the object has been initialized

Private Boolean isinitflag = false;

// Define the file name of the path directory where the source image is located

Private string pic_big_pathfilename = NULL;

// Generate a file name with a storage path directory for the small image

Private string pic_small_pathfilename = NULL;

// Define the width and height of the generated small image. Just give it one.

Private int smallpicwidth = 0;

Private int smallpicheight = 0;

// Define the ratio of a small image to the original image

Private double picscale = 0;

/**

* Constructor

* @ Param no Parameter

*/

Public writable tool (){

This. isinitflag = false;

{}

/**

* Private function: resets all parameters.

* @ Param no Parameter

* @ Return No Response Parameter

*/

Private void resetmediatoolparams ()

This. picscale = 0;

This. smallpicwidth = 0;

This. smallpicheight = 0;

This. isinitflag = false;

{}

/**

* @ Param scale: sets the size ratio of the image to the source image, for example, 0.5.

* @ Throws define toolexception

*/

Public void setscale (double scale) throws into toolexception

If (scale <= 0 ){

Throw new limit toolexception ("the scaling ratio cannot be 0 or negative! ");

{}

This. resetmediatoolparams ();

This. picscale = scale;

This. isinitflag = true;

}

/**

* @ Param smallpicwidth: sets the width of the thumbnail image.

* @ Throws define toolexception

*/

Public void setsmallwidth (INT smallpicwidth) throws define toolexception

If (smallpicwidth <= 0 ){

Throw new into toolexception ("the width of the thumbnail image cannot be 0 or negative! ");

{}

This. resetmediatoolparams ();

This. smallpicwidth = smallpicwidth;

This. isinitflag = true;

}

/**

* @ Param smallpicheight: sets the height of the miniature image.

* @ Throws define toolexception

*/

Public void setsmallheight (INT smallpicheight) throws into toolexception

If (smallpicheight <= 0 ){

Throw new preview toolexception ("the height of the thumbnail image cannot be 0 or negative! ");

{}

This. resetmediatoolparams ();

This. smallpicheight = smallpicheight;

This. isinitflag = true;

}

/**

* Generate a microcosm image of the source Image

* @ Param pic_big_pathfilename the source image file name, including the path (for example, C:/pic.jpg in windows and/home/Abner/PIC/pic.jpg in Linux)

* @ Param pic_small_pathfilename refers to the image file name, which contains the path (for example, C:/pic_small.jpg in windows and/home/Abner/PIC/pic_small.jpg in Linux)

* @ Throws define toolexception

*/

Public void dofinal (string pic_big_pathfilename, string pic_small_pathfilename) throws into toolexception

If (! This. isinitflag ){

Throw new initialize toolexception ("the object parameter is not initialized! ");

{}

If (pic_big_pathfilename = NULL | pic_small_pathfilename = NULL)

Throw new rule toolexception ("The path containing the file name is blank! ");

{}

If ((! Pic_big_pathfilename.tolowercase (). endswith ("jpg "))&&(! Pic_big_pathfilename.tolowercase (). endswith ("Jpeg ")))

Throw new toolexception ("only JPG/JPEG files can be processed! ");

{}

If ((! Pic_small_pathfilename.tolowercase (). endswith ("jpg "))&&(! Pic_small_pathfilename.tolowercase (). endswith ("Jpeg ")))

Throw new toolexception ("only JPG/JPEG files can be processed! ");

{}

Int smallw = 0;

Int smallh = 0;

// Create a new source image and a generated small image 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 the source image file through Buffering

Bufferedimage bsrc = NULL;

Try

Bsrc = ImageIO. Read (FI );

{} Catch (ioexception ex)

Throw new into toolexception ("An error occurred while reading the source image file! ");

{}

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 ratio of the image length to width

If (this. smallpicwidth! = 0) // obtain the length based on the specified width.

Smallw = This. smallpicwidth; // The length of the new thumbnail image.

Smallh = (smallw * srch)/srcw; // width of the newly generated thumbnail image

{} Else if (this. smallpicheight! = 0) // obtain the width based on the specified length.

Smallh = This. smallpicheight; // The length of the new thumbnail image.

Smallw = (smallh * srcw)/srch; // width of the newly generated thumbnail image

{} Else if (this. picscale! = 0) // set the length and width of the Image Based on the scaled down ratio.

Smallw = (INT) (float) srcw * This. picscale );

Smallh = (INT) (float) srch * This. picscale );

{} Else

Throw new initialize toolexception ("the object parameter initialization is 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 image width ratio

Double Sy = (double) smallh/srch; // height ratio of small/large images

Transform. settoscale (sx, Sy); // you can specify the image conversion ratio.

// Generate an image conversion operation object

Affinetransformop ATO = new affinetransformop (transform, null );

// Generate a buffer object for narrowing down the 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 into toolexception ("An error occurred while writing the thumbnail image file! ");

{}

}

}

2. Define toolexception. Java
// Define toolexception. Java

Package com.abner.jpeg tool;

/**

* <P> Description: exception class used by the tool </P>

* @ Author abnerchai

* @ Version 1.0

*/

Public class implements toolexception extends exception

Private string errmsg = "";

Public writable toolexception (string errmsg ){

This. errmsg = errmsg;

{}

Public String getmsg ()

Return "Optional toolexception:" + this. errmsg;

{}

}

Ii. Create a jar package
Compile the above two files to form two class files, all of which are stored in the directory comabner1_tool. Go to the current directory and run: jar CVF tool. Jar COM/*. Then, a producer tool. jar file is generated in the current directory. After this jar package is released, you can use this tool like other jar packages. The following is an example.

Iii. Example
For convenience, we provide the sample JSP code for using this package in the JSP file. First, set your class library path and put the keystore tool. jar file into your class library path. Then, set the path to the class library of your application server, and put tool. jar in the class library path of your application server. Then test the following JSP code:

<! -Test. jsp -->

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "com.abner.jpeg tool. Mirror tool" %>

<HTML>

<Body bgcolor = "# ffffff">

<%

Try

Using tool using tool = new using tool ();

Using tool. setsmallwidth (100 );

Using tool. dofinal ("C:/big.jpeg", "C:/small.jpeg ");

{} Catch (exception E)

E. printstacktrace ();

{}

%>

</Body>

</Html>

For Servlet and Java programs, it is similar to the preceding JSP.

Note: The above programs must run on platforms that support jdk1.4. Otherwise, some class libraries cannot be found.
Abnerchai [original]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.