java 降低java中的图像分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6390964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
decrease image resolution in java
提问by Mohammad Adil
I need to reduce the size of image(not the width and height) using Java program. Is their any good API available for this?
我需要使用 Java 程序减小图像的大小(不是宽度和高度)。他们有什么好的 API 可用于此吗?
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.
我需要将大小从 1MB 减少到大约 50kb - 100 kb。当然,分辨率会降低,但这没关系。
采纳答案by Mohammad Adil
This is the working 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();
}
}
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);
这段代码对我来说很好用。如果您需要调整图像大小,则可以在此处更改 x 和 y 比例JAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);
回答by ironchefpython
According to this blog post: http://i-proving.com/2006/07/06/java-advanced-imaging/you can use the Java Advanced Imaging Libraryto do what you want. The following sample code should provide you a good starting point. This will resize the image, both in height and width, as well as image quality. Once your image is of the desired file size, you can scale it back up to your desired pixel height and width when you display the image.
根据这篇博客文章:http: //i-proving.com/2006/07/06/java-advanced-imaging/您可以使用Java Advanced Imaging Library来做您想做的事。下面的示例代码应该为您提供一个很好的起点。这将调整图像的高度和宽度以及图像质量。一旦您的图像具有所需的文件大小,您可以在显示图像时将其缩放回所需的像素高度和宽度。
// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
inputStream, true);
RenderedOp image = JAI.create("stream", s);
((OpImage)image.getRendering()).setTileCache(null);
// now resize the image
float scale = newWidth / image.getWidth();
RenderedOp resizedImage = JAI.create("SubsampleAverage",
image, scale, scale, qualityHints);
// lastly, write the newly-resized image to an
// output stream, in a specific encoding
JAI.create("encode", resizedImage, outputStream, "PNG", null);
回答by Atrus
You can use JAI to increase the compression of a written JPEG without doing any scaling. See http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html
您可以使用 JAI 来增加写入 JPEG 的压缩率,而无需进行任何缩放。见http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html
回答by trashgod
If your image type is supported by an implementation of ImageWriteParam
, you can adjust the quality as shown in this example. Other ImageWriteParam
methods, such as getBitRate()
, may allow you to optimize the result.
如果 的实现支持您的图像类型ImageWriteParam
,您可以调整质量,如本例所示。其他ImageWriteParam
方法,例如getBitRate()
,可能允许您优化结果。