java 压缩图片,所略图

public void compressImage(FormFile formFile, String path, String _fileName) { 
try { 
Image src = javax.imageio.ImageIO.read(formFile.getInputStream()); // construct the Image Object 
int width=src.getWidth(null); // get the width 
int height=src.getHeight(null); // get the height 

// Compress Image 
BufferedImage tag = new BufferedImage(width/2,height/2,BufferedImage.TYPE_INT_RGB); 
tag.getGraphics().drawImage(src,0,0,width/2,height/2,null); // draw the small image 
FileOutputStream out=new FileOutputStream(path + _fileName); 
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam encoder_param = encoder.getDefaultJPEGEncodeParam(tag); 
encoder_param.setQuality(Constants.IMAGE_COMPRESS_RATE,false); // Image compress rate 
encoder.setJPEGEncodeParam(encoder_param); 
encoder.encode(tag); 
out.close(); 
return true; 
} catch(Exception e) { 
Logger.getLogger(this.getClass().getName()).error(e.getMessage(), e); 
return false; 
} 
} 

public void primaryImage(FormFile formFile, String path, String _fileName){ 
try { 
Image src = javax.imageio.ImageIO.read(formFile.getInputStream()); // construct the Image Object 
int width=src.getWidth(null); // get the width 
int height=src.getHeight(null); // get the height 

// Primary Image 
BufferedImage _primaryTag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 
_primaryTag.getGraphics().drawImage(src,0,0,width,height,null); 
FileOutputStream _primaryOut=new FileOutputStream(path + _fileName); 
JPEGImageEncoder _primaryEncoder = JPEGCodec.createJPEGEncoder(_primaryOut); 
_primaryEncoder.encode(_primaryTag); 
_primaryOut.close(); 
return true; 
} catch(Exception e) { 
Logger.getLogger(this.getClass().getName()).error(e.getMessage(), e); 
return false; 
} 
} 

你可能感兴趣的:(java)