Java 图片压缩:像素质量压缩 、图片长宽缩放。常用方式:一种为Java有BufferedImage来处理;另一种为谷歌的Thumbnailator处理。如果为文件流处理图片的时候,推荐使用第一种;如果
路径读取文件,推荐选择第二种。
1.BufferedImage来处理
? ? 1.1 质量压缩
//读取图片文件流
BufferedImage image = ImageIO.read(inputStream);// 得到指定Format图片的writer
Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
ImageWriter imageWriter = iter.next();// 得到指定writer的输出参数设置(ImageWriteParam )
ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 设置可否压缩iwp.setCompressionQuality(2000000); // 设置压缩质量参数iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();// 指定压缩时使用的色彩模式
iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel,
colorModel.createCompatibleSampleModel(16, 16)));// 开始打包图片,写入byte[]
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream(); // 取得内存输出流
IIOImage iIamge =new IIOImage(image, null, null);// 此处因为ImageWriter中用来接收write信息的output要求必须是ImageOutput
// 通过ImageIo中的静态方法,得到byteArrayOutputStream的ImageOutput
imageWriter.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));
imageWriter.write(null, iIamge, iwp);InputStream sbs =new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//输入文件流
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ImageIO.write(ImageIO.read(sbs), "JPEG", outStream);
return outStream.toByteArray();?
? ??1.2 图片长宽缩放
//读取图片文件流
BufferedImage image = ImageIO.read(inputStream);
int width = image.getWidth(); int height = image.getHeight();?
//判断图片的摆放位置(横向图片 纵向图片)
if(width >4000? || height >4000){
????BigDecimal value;
? ? //定义的基准为4K
? ? BigDecimal bignum1 =new BigDecimal("4000");
? ? //获取相对应的比例
? ? if(image.getWidth() > image.getHeight()){
? ? ? ? BigDecimal bignum2 =new BigDecimal(image.getWidth());
? ? ? ? value = bignum1.divide(bignum2, 2);
????}else{
????????BigDecimal bignum2 =new BigDecimal(image.getHeight());
? ? ? ? value = bignum1.divide(bignum2, 2);
????}? ? //缩小相应的比例
?????if(value.doubleValue() <1){
????????width =new BigDecimal(image.getWidth()).multiply(value).intValue();
????????height =new BigDecimal(image.getHeight()).multiply(value).intValue();
? ? ? ? //重绘图片
????????BufferedImage imageNew =new BufferedImage(width, ????????????????height,BufferedImage.TYPE_INT_RGB);? ? ? ? imageNew.getGraphics().drawImage(image, 0, 0, width, height, null); // 绘制缩小后的图
? ? ? ? ByteArrayOutputStream outStream =new ByteArrayOutputStream(); ????????ImageIO.write(imageNew, "JPEG", outStream);
????????//把outStream里的数据写入内存
????????return outStream.toByteArray();
?}
2.Thumbnailator处理
? ? 2.1 质量压缩?
? ??Thumbnails.of(path).scale(1f).outputQuality(0.25f).toFile(toPic);
? ??outputQuality就是用来控制图片质量的
? ???2.2?图片长宽缩放?
ByteArrayOutputStream out = new ByteArrayOutputStream();
Thumbnails.of(path).size(ppi, ppi).outputFormat("png").toOutputStream(out);
return? out.toByteArray();
备注:读取文件流时,使用thumbnailator 时部分图片抛异常(因此推荐Java BufferedImage)
net.coobird.thumbnailator.tasks.UnsupportedFormatException: No suitable ImageReader foundfor source data.
? ? at net.coobird.thumbnailator.tasks.io.InputStreamImageSource.read(Unknown Source)
? ? at net.coobird.thumbnailator.tasks.SourceSinkThumbnailTask.read(Unknown Source)
? ? at net.coobird.thumbnailator.Thumbnailator.createThumbnail(Unknown Source)
? ? at net.coobird.thumbnailator.Thumbnails$Builder.toOutputStream(Unknown Source)