Java image compression Implementation ideas and code _java

Source: Internet
Author: User
Java Picture Compression Code
Copy Code code as follows:

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.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*
* @author Cola plus sugar
*/
public class Compresspicdemo {
Private file File = null; File objects
Private String InputDir; Input diagram Path
Private String OutputDir; Output graph Path
Private String InputFileName; Enter diagram file name
Private String OutputFileName; Output Map File name
private int outputwidth = 100; Default output picture width
private int outputheight = 100; High default output picture
Private Boolean proportion = true; is equal to the zoom mark (default ratio scaling)
Public Compresspicdemo () {//initialization variables
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;
}
/*
* Get picture size
* Incoming parameter String path: Picture path
*/
Public long Getpicsize (String path) {
File = new file (path);
return File.length ();
}
Picture processing
Public String Compresspic () {
try {
Get source files
File = new File (InputDir + inputfilename);
if (!file.exists ()) {
Return "";
}
Image img = imageio.read (file);
Determine if the picture is in the correct format
if (img.getwidth (null) = = 1) {
System.out.println ("Can ' t read,retry!" + "<BR>");
Return "No";
} else {
int newwidth; int newheight;
Determine if scaling is equal
if (this.proportion = = True) {
Calculates the output width and height of the picture for equal-ratio scaling
Double rate1 = ((double) img.getwidth (NULL))/(double) outputwidth + 0.1;
Double Rate2 = ((double) img.getheight (NULL))/(double) outputheight + 0.1;
Zoom control based on large scaling 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); Picture width of output
Newheight = Img.getheight (null); Picture height of output
}
BufferedImage tag = new BufferedImage (int) newwidth, (int) newheight, BUFFEREDIMAGE.TYPE_INT_RGB);
/*
* Image.scale_smooth thumbnail algorithm to generate the smoothness of the thumbnail image
* Higher priority than the speed of the resulting picture quality is relatively good but slow
*/
Tag.getgraphics (). DrawImage (Img.getscaledinstance (Newwidth, Newheight, Image.scale_smooth), 0, 0, NULL);
FileOutputStream out = new FileOutputStream (OutputDir + outputfilename);
JPEGImageEncoder can be used for conversions of other picture types
JPEGImageEncoder encoder = Jpegcodec.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) {
Input diagram Path
This.inputdir = InputDir;
Output graph Path
This.outputdir = OutputDir;
Enter diagram file name
This.inputfilename = InputFileName;
Output Map File name
This.outputfilename = OutputFileName;
return Compresspic ();
}
public string Compresspic (string InputDir, String OutputDir, String inputfilename, string outputfilename, int width, int h Eight, Boolean GP) {
Input diagram Path
This.inputdir = InputDir;
Output graph Path
This.outputdir = OutputDir;
Enter diagram file name
This.inputfilename = InputFileName;
Output Map File name
This.outputfilename = OutputFileName;
Set Picture width
Setwidthandheight (width, height);
is the equal-ratio scaling mark
This.proportion = GP;
return Compresspic ();
}
Main test
Compresspic (big picture path, generate small picture path, large picture filename, generate small picture Wennan, generate small picture width, generate small picture height, whether equal scaling (default is True))
public static void Main (string[] arg) {
Compresspicdemo mypic = new Compresspicdemo ();
SYSTEM.OUT.PRINTLN ("Input picture size:" + mypic.getpicsize ("e:\\1.jpg")/1024 + "KB");
Mypic.compresspic ("e:\\", "e:\\test\\", "1.jpg", "R1.jpg", (), and false);
}
}

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.