BufferedImage image_to_save = null; try { image_to_save = ImageIO.read(new File("d:\\123.jpg")); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } FileOutputStream fos = null; ImageWriter imageWriter = null; ImageOutputStream ios = null; try { fos = new FileOutputStream("d:\\124.jpg"); imageWriter = ImageIO.getImageWritersBySuffix("jpg").next(); ios = ImageIO.createImageOutputStream(fos); imageWriter.setOutput(ios); JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam(); jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(0.3f); imageWriter.write(null, new IIOImage(image_to_save, null, null), jpegParams); } catch (Exception ex) { } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { } } if (ios != null) { try { ios.close(); } catch (IOException e) { } } if (imageWriter != null) { imageWriter.dispose(); } }
本文详细介绍了一种使用Java进行图像读取、压缩并调整图像质量的方法。通过使用BufferedImage和ImageIO类,实现了从文件读取图像、设置JPEG压缩参数、调整图像质量并保存到新文件的过程。此方法适用于需要批量处理图像并调整其质量的应用场景。
231

被折叠的 条评论
为什么被折叠?



