try { // 压缩前的JPEG文件 File srcFile = new File("c://src.jpg"); // 压缩前后的JPEG文件 File dstFile = new File("c://dst.jpg"); // 压缩百分比 float quality = 0.75f; BufferedImage srcImage = ImageIO.read(srcFile); Iterator it = ImageIO.getImageWritersBySuffix("jpg"); if (it.hasNext()) { FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(dstFile); ImageWriter iw = (ImageWriter) it.next(); ImageWriteParam iwp = iw.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); iw.setOutput(fileImageOutputStream); iw.write(null, new IIOImage(srcImage, null, null), iwp); iw.dispose(); fileImageOutputStream.flush(); fileImageOutputStream.close(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }
java实现jpeg压缩_想对jpeg格式图像进行压缩(不是缩略图),压缩后还是jpeg格式,在java中如何实现...
最新推荐文章于 2022-05-25 17:58:00 发布