减少在java中的图像分辨率 [英] decrease image resolution in java
本文介绍了减少在java中的图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用Java程序,以减少图像的尺寸(未宽度和高度)。
是他们任何好的API可用于此?
I need to reduce the size of image(not the width and height) using Java program. Is their any good API available for this?
我需要从1MB大小降低到约50KB - 100 KB的。
的,当然分辨率会降低,但这并不重要。
I need to reduce the size from 1MB to about 50kb - 100 kb's. Of-course the resolution will decrease but that doesn't matter.
推荐答案
这是工作code
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("encode", resizedImage, bos, "JPEG", null);
}
public static void main(String[] args) throws IOException {
new ImageCompressor().compress();
}
}
这code是伟大的工作对我来说。如果需要调整图像大小,那么你就可以
这里改变x和y的比例 JAI.create(SubsampleAverage的形象,xscale的,yscale,qualityHints);
This code is Working Great for me. if you need to resize the image then you can
change the x and y scale here JAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);
这篇关于减少在java中的图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文