图片问题

一、用Thumbnails工具类实现
二、文件流实现
图片尺寸不变,质量改变
public static File resize(Float quality, File file, Image targetImage) throws ImageFormatException, IOException {
int newWidth = targetImage.getWidth(null);
int newHeight = targetImage.getHeight(null);
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;

// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("png").next();
imgWriteParams = new JPEGImageWriteParam(null);
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float)0.5);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel
.createCompatibleSampleModel(16, 16)));
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight,Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
Image from = targetImage.getScaledInstance(newWidth, newHeight, targetImage.SCALE_AREA_AVERAGING);
g2d.drawImage(from, 0, 0, null);
g2d.dispose();

System.out.println("原图片大小=================="+file.length());
System.out.println("原图片路径=================="+file.getCanonicalPath());

FileOutputStream out = new FileOutputStream(file);
imgWrier.reset();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何 OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(image, null, null), imgWriteParams);
out.flush();
out.close();
System.out.println("新图片大小=================="+file.length());
return file;
}


大小改变,质量不变
public static File resize20180224(Float quality, File file, Image targetImage) throws ImageFormatException, IOException {
int targetWidth = targetImage.getWidth(null);
int targetHeight = targetImage.getHeight(null);
int width = Math.max(targetWidth, 1);
int height = Math.max(targetHeight, 1);
double rate=2.0;
/* if(file.length()>=1024000){
rate= file.length()/(double)1024000;
}*/
int newWidth = (int) (((double) width) / rate);
int newHeight = (int) (((double) height) / rate);
BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight,Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
Image from = targetImage.getScaledInstance(newWidth, newHeight, targetImage.SCALE_AREA_AVERAGING);
g2d.drawImage(from, 0, 0, null);
g2d.dispose();

/** 设置图片压缩比例 */
if (quality == null || quality <= 0) {
if(file.length()<2049000){
//0.5倍压缩
quality = (Float)0.5f;//小于2MB
}else{
//0.25倍压缩
quality = (Float)0.25f;//大于2MB
}
}
System.out.println("原图片大小=================="+file.length());
ImageIO.write(image, "png", file);
System.out.println("新图片大小=================="+file.length());
return file;
}

你可能感兴趣的:(java开发,图片压缩)