Java image compression code
Copy the 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 Coke with sugar
*/
public class CompressPicDemo {
private File file = null; // file object
private String inputDir; //Input map path
private String outputDir; // Output graph path
private String inputFileName; //Input image file name
private String outputFileName; // Output image file name
private int outputWidth = 100; //Default output image width
private int outputHeight = 100; //Default output picture height
private boolean proportion = true; // Whether to scale the mark proportionally (default is proportional scaling)
public CompressPicDemo() { //Initialize 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 image size
* Pass in parameter String path: image path
*/
public long getPicSize(String path) {
file = new File(path);
return file.length();
}
//Image processing
public String compressPic() {
try {
//Get 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 for proportional scaling
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
// Perform zoom control based on the larger 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); // Output image width
newHeight = img.getHeight(null); // Output image height
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH thumbnail algorithm generates smoothness of thumbnail images
* The priority is higher than the speed. The quality of the generated pictures is better but the speed is slow.
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder can be applied to the conversion of other image 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 image path
this.inputDir = inputDir;
//Output graph path
this.outputDir = outputDir;
// Enter the image file name
this.inputFileName = inputFileName;
// Output image file name
this.outputFileName = outputFileName;
return compressPic();
}
public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
//Input image path
this.inputDir = inputDir;
//Output graph path
this.outputDir = outputDir;
// Enter the image file name
this.inputFileName = inputFileName;
// Output image file name
this.outputFileName = outputFileName;
//Set the image length and width
setWidthAndHeight(width, height);
// Whether it is a proportional scaling mark
this.proportion = gp;
return compressPic();
}
// main test
// compressPic (large picture path, generate small picture path, large picture file name, generate small picture text name, generate small picture width, generate small picture height, whether to scale proportionally (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", 120, 120, false);
}
}