这是工作代码
public class ImageCompressor { public void compress() throws IOException { File infile = new File("Y:\img\star.jpg"); File outfile = new File("Y:\img\star_compressed.jpg"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( infile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(outfile)); SeekableStream s = SeekableStream.wrapInputStream(bis, true); RenderedOp image = JAI.create("stream", s); ((OpImage) image.getRendering()).setTileCache(null); RenderingHints qualityHints = new RenderingHints( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); RenderedOp resizedImage = JAI.create("SubsampleAverage", image, 0.9, 0.9, qualityHints); JAI.create("enpre", resizedImage, bos, "JPEG", null); } public static void main(String[] args) throws IOException { new ImageCompressor().compress(); }}
这段代码对我来说很棒。如果需要调整图像大小,则可以在此处更改x和y比例J
AI.create("SubsampleAverage", image, xscale,yscale, qualityHints);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)