This article for you to introduce the detailed picture compression of the specific implementation of ideas and Java code, want to learn from you can refer to the HA, I hope to help you
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 = null; File Object
private String InputDir; Input graph Path
private String OutputDir; Output graph Path
private String InputFileName; Input diagram filename
private String OutputFileName; Output diagram filename
private int outputwidth = 100; Default output picture wide
private int outputheight = 100; Default output Picture High
Private Boolean proportion = true; is equal to the scaling mark (implied equal ratio scaling)
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;
}
/*
* 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 file
file = new file (InputDir + inputfilename);
if (!file.exists ()) {
return "";
}
Image img = imageio.read (file);
//Determine if the picture format is correct
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) {
//is the picture width and height of the output 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); The output of the picture height
}
bufferedimage tag = new BufferedImage (int) newwidth, (int) newheight, BUFFEREDIMAGE.TYPE_INT_RGB);
/*
* Image.scale_smooth's thumbnail algorithm generates 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 graph path
This.inputdir = InputDir;
//Output graph path
this.outputdir = OutputDir;
//input map filename
this.inputfilename = inputfilename;
//output graph filename
this.outputfilename = OutputFileName;
return Compresspic ();
}
public string Compresspic (string InputDir, String OutputDir, String inputfilename, string outputfilename, int width, int height, Boolean gp) {
//Input graph path
this.inputdir = InputDir;
//Output graph path
this.outputdir = OutputDir;
//input map filename
this.inputfilename = inputfilename;
//output graph filename
this.outputfilename = OutputFileName;
//Set Picture long width
setwidthandheight (width, height);
//is the equal-ratio scaling Mark
this.proportion = GP;
return Compresspic ();
}
Main Test
//compresspic (large 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", A., A, false);
}
}