Java中如何将原始图像调整为通用图像大小?

发布于 2024-08-22 04:16:55 字数 42 浏览 3 评论 0原文

在 Java 中,如何将图像大小调整为任何类型或大小的图像的默认大小?

In Java, how can I resize an image into a default size for any kind or size of image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-08-29 04:16:55

您无法轻松地将图像从一种文件大小调整为另一种文件大小,但是,大多数 JPG/PNG/GIF 等。根据其分辨率,它们往往具有相似的文件大小。例如,200kb 压缩的 JPG 通常大小为 1280x960。如果是这种情况,您只需将所有调整大小操作作为目标,将目标图像调整为该大小,并大致获得您想要的大小约束。

一种非常简单的方法是使用非常简单的 java 图像大小调整库(Apache 2 许可证),只需一切都适合您。调整大小的示例代码如下所示:

BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, 1280, 960);

您的图像比例受到尊重,库根据缩放导致的图像变化量(最快、平衡或质量)和最佳方式对应使用的方法进行最佳猜测支持的 Java2D 图像类型始终用于进行缩放,以避免出现“黑色”结果或看起来非常糟糕的输出(例如过度抖动的 GIF 图像)。

另外,如果您想强制它输出 Java 中可能的最佳外观结果,API 调用将如下所示:

BufferedImage img = ImageIO.read(...); // load image
BufferedImage scaledImg = Scalr.resize(img, Method.QUALITY, 1280, 960);

该库使用 Java2D 建议的增量缩放为您提供最佳外观结果。

您可以通读库中的所有注释(代码本身已进行大量文档化),以查看已解决的所有不同 JDK 错误或为提高性能或内存使用率而进行的优化。我花了很多时间来调整这个实现,并从在 Web 应用程序和其他 Java 项目中部署它的人们那里得到了很多好的反馈。

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.

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:

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).

Also, if you want to force it to output the best looking result possible in Java, the API call would look like this:

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.

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.

枕头说它不想醒 2024-08-29 04:16:55

网上有关于如何执行此操作的完整文章,类似这些应该可以帮助您:

There are full articles on how to do this around the net, something like these should get you going:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文