Java image compression ideas and code

  • 2020-04-01 02:05:27
  • OfStack

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.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGImageEncoder; 
 
public class CompressPicDemo { 
private File file = null; //The file object
private String inputDir; //Input graph path
private String outputDir; //Output graph path
private String inputFileName; //Input map file name
private String outputFileName; //Output graph file name
private int outputWidth = 100; //The default output picture width
private int outputHeight = 100; //The default output image is high
private boolean proportion = true; //Equal scale mark (default is equal scale)
public CompressPicDemo() { //Initialize 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; 
} 
 
public long getPicSize(String path) { 
file = new File(path); 
return file.length(); 
} 
//The image processing
public String compressPic() { 
try { 
//Get the source
file = new File(inputDir + inputFileName); 
if (!file.exists()) { 
return ""; 
} 
Image img = ImageIO.read(file); 
//Determine if 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; 
//Determines whether it is an isometric scale
if (this.proportion == true) { 
//Calculate the width and height of the output picture for proportional scaling
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1; 
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1; 
//Scale control according to the scale 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 picture width
newHeight = img.getHeight(null); //Output image height
} 
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); 
 
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); 
FileOutputStream out = new FileOutputStream(outputDir + outputFileName); 
//JPEGImageEncoder can be used for 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 graph path
this.inputDir = inputDir; 
//Output graph path
this.outputDir = outputDir; 
//Input map 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) { 
//Input graph path
this.inputDir = inputDir; 
//Output graph path
this.outputDir = outputDir; 
//Input map file name
this.inputFileName = inputFileName; 
//Output graph file name
this.outputFileName = outputFileName; 
//Set the length and width of the image
setWidthAndHeight(width, height); 
//Is it an isometric scaling flag
this.proportion = gp; 
return compressPic(); 
} 
//The main test
//CompressPic (large image path, generate small image path, large image file name, generate small image text name, generate small image width, generate small image height, scale or not (default is true))
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 articles: