Java-only Image Compression

Source: Internet
Author: User

Java Image Compression Code

Package com. IMG;

Import java. AWT. image;
Import java. AWT. image. bufferedimage;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import javax. ImageIO. ImageIO;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
/**
*
* @ Author cola sugar
*/
Public class compresspicdemo {
Private file = NULL; // file object
Private string inputdir; // input graph path
Private string outputdir; // output graph path
Private string inputfilename; // name of the input graph File
Private string outputfilename; // name of the output graph File
Private int outputwidth = 100; // default output image width
Private int outputheight = 100; // The default output Image Height
Private Boolean proportion = true; // whether to scale proportionally (proportional scaling by default)
Public compresspicdemo () {// initialization variable
Inputdir = "";
Outputdir = "";
Inputfilename = "";
Outputfilename = "";
Outputwidth = 100;
Outputheight = 100;
}
Public void setinputdir (string inputdir ){
This. inputdir = inputdir;
}
Public void setoutputdir (string outputdir ){
This. outputdir = outputdir;
}
Public void setinputfilename (string inputfilename ){
This. inputfilename = inputfilename;
}
Public void setoutputfilename (string outputfilename ){
This. outputfilename = outputfilename;
}
Public void setoutputwidth (INT outputwidth ){
This. outputwidth = outputwidth;
}
Public void setoutputheight (INT outputheight ){
This. outputheight = outputheight;
}
Public void setwidthandheight (INT width, int height ){
This. outputwidth = width;
This. outputheight = height;
}

/*
* Obtain the image size.
* Input parameter string path: Image path
*/
Public long getpicsize (string path ){
File = new file (PATH );
Return file. Length ();
}

// Image Processing
Public String compresspic (){
Try {
// Obtain the source file
File = new file (inputdir + inputfilename );
If (! File. exists ()){
Return "";
}
Image IMG = ImageIO. Read (File );
// Determine whether the image format is correct
If (IMG. getwidth (null) =-1 ){
System. Out. println ("can't read, retry! "+" <Br> ");
Return "no ";
} Else {
Int newwidth; int newheight;
// Determine whether it is proportional Scaling
If (this. proportion = true ){
// Calculate the width and height of the output image by proportional Scaling
Double rate1 = (double) IMG. getwidth (null)/(double) outputwidth + 0.1;
Double rate2 = (double) IMG. getheight (null)/(double) outputheight + 0.1;
// Perform scaling Control Based on the zoom Ratio
Double rate = rate1> rate2? Rate1: rate2;
Newwidth = (INT) (double) IMG. getwidth (null)/rate );
Newheight = (INT) (double) IMG. getheight (null)/rate );
} Else {
Newwidth = IMG. getwidth (null); // The width of the output image.
Newheight = IMG. getheight (null); // The height of the output image.
}
Bufferedimage tag = new bufferedimage (INT) newwidth, (INT) newheight, bufferedimage. type_int_rgb );

/*
* The image. scale_smooth scaling algorithm generates the smoothness of the thumbnail.
* The quality of images generated with a higher priority than that of images generated with a lower priority
*/
Tag. getgraphics (). drawimage (IMG. getscaledinstance (newwidth, newheight, image. scale_smooth), 0, 0, null );
Fileoutputstream out = new fileoutputstream (outputdir + outputfilename );
// Optional imageencoder can be used for conversion of other image types
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (TAG );
Out. Close ();
}
} Catch (ioexception ex ){
Ex. printstacktrace ();
}
Return "OK ";
}
Public String compresspic (string inputdir, string outputdir, string inputfilename, string outputfilename ){
// Enter the image path
This. inputdir = inputdir;
// Output graph path
This. outputdir = outputdir;
// Input the image file name
This. inputfilename = inputfilename;
// Output graph file name
This. outputfilename = outputfilename;
Return compresspic ();
}
Public String compresspic (string inputdir, string outputdir, string inputfilename, string outputfilename, int width, int height, Boolean GP ){
// Enter the image path
This. inputdir = inputdir;
// Output graph path
This. outputdir = outputdir;
// Input the image file name
This. inputfilename = inputfilename;
// Output graph file name
This. outputfilename = outputfilename;
// Set the image length and width
Setwidthandheight (width, height );
// Whether it is an proportional scaling flag
This. proportion = gp;
Return compresspic ();
}

// Main test
// Compresspic (large image path, generate small image path, large image file name, generate small image file name, generate small image width, generate small image height, and perform proportional Scaling (true by default ))
Public static void main (string [] Arg ){
Compresspicdemo mypic = new compresspicdemo ();
System. Out. println ("input image size:" + mypic. getpicsize ("E: \ 1.jpg")/1024 +" kb ");
Mypic. compresspic ("E: \", "E: \ test \", "1.jpg"," r1.jpg ", 120,120, false );

}
}

Related Article

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.