java 图片压缩
Posted 默西塞德
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 图片压缩相关的知识,希望对你有一定的参考价值。
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 图片压缩的主要内容,如果未能解决你的问题,请参考以下文章