如何将原始图像调整为 Java 中常见的图像大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2305670/
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
How to resize the original image into a common size of image in Java?
提问by Venkat
In Java, how can I resize an image into a default size for any kind or size of image?
在 Java 中,如何将图像大小调整为任何类型或大小的图像的默认大小?
采纳答案by Nick Craver
There are full articles on how to do this around the net, something like these should get you going:
网上有很多关于如何做到这一点的完整文章,像这样的东西应该会让你开始:
- Ultimate Java Image Manipulation(Includes manyother image functions)
- 终极 Java 图像处理(包括许多其他图像功能)
回答by Riyad Kalla
You can't easily resize an image from one file size to another, BUT, most JPG/PNG/GIF/etc. tend to have similar file-sizes based on their resolutions. For example, a 200kb compressed JPG might typically be say 1280x960 in size. If that is the case, you would just target all your resize operations to size the target images to that size and get roughly the size constraint you want.
您不能轻松地将图像从一种文件大小调整为另一种文件大小,但是,大多数 JPG/PNG/GIF/等。基于其分辨率,往往具有相似的文件大小。例如,一个 200kb 的压缩 JPG 的大小通常可能是 1280x960。如果是这种情况,您只需将所有调整大小操作作为目标,将目标图像调整为该大小,并大致获得您想要的大小限制。
One really easy way to do this is to use very simple java image resizing library(Apache 2 license) that just does everything right for you. Example code to resize would look like this:
一种非常简单的方法是使用非常简单的 java图像大小调整库(Apache 2 许可证),它可以为您做所有正确的事情。调整大小的示例代码如下所示:
BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, 1280, 960);
Your image proportions are honored, the library makes a best-guess at the method it should use based on the amount of change in the image due to scaling (FASTEST, BALANCED or QUALITY) and the best supported Java2D image types are always used to do the scaling to avoid the issue of "black" results or really terrible looking output (e.g. overly dithered GIF images).
您的图像比例受到尊重,该库根据缩放导致的图像变化量(最快、平衡或质量)对它应该使用的方法进行最佳猜测,并且始终使用最受支持的 Java2D 图像类型缩放以避免“黑色”结果或看起来非常糟糕的输出(例如过度抖动的 GIF 图像)问题。
Also, if you want to force it to output the best looking result possible in Java, the API call would look like this:
此外,如果您想强制它在 Java 中输出尽可能最好的结果,API 调用将如下所示:
BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, Method.QUALITY, 1280, 960);
The library use the Java2D recommended incremental scaling for you to give you the best looking result.
该库使用 Java2D 推荐的增量缩放为您提供最佳效果。
You can read through all the comments in the library (the code itself is doc'ed heavily) to see all the different JDK bugs that are worked around or optimizations that are made to improve the performance or memory usage. I spent a LOT of time tuning this implementation and have had a lot of good feedback from folks deploying it in web apps and other Java projects.
您可以通读库中的所有注释(代码本身被大量记录)以查看所有不同的 JDK 错误或为提高性能或内存使用而进行的优化。我花了很多时间调整这个实现,并从人们那里得到了很多很好的反馈,这些反馈将它部署到 Web 应用程序和其他 Java 项目中。