java - 通过保留物理尺寸降低图像 PPI(每英寸像素数)
问题描述
我正在为印刷行业开发软件,我需要将上传的高质量和高 PPI(例如 300)图像转换为具有相同物理尺寸(以英寸为单位)的低 PPI(例如 40)。
例如,将具有 300 PPI 和 10x10(英寸 x 英寸)的图像转换为具有 50 PPI 和 10x10(英寸 x 英寸)的图像,
此转换很重要,因为我们希望向用户显示具有其他透明层的低质量图像, 由于预览实际最终打印结果。
这张图片是另一个例子
,我如何在 Java 或 Kotlin 中做到这一点?
解决方案
首先,请耐心等待,因为我既不了解打印过程,也不了解图形术语。这个答案假设(用我自己的话说)目标是拍摄图像并对其进行修改,以减少细节 - 显示图像所需的像素更少,但尺寸保持不变。如果事实证明我误解了目标,请随时指出。我要么尝试编辑答案,要么想出一些新的东西。
我建议您拍摄图像,缩小并将其拉伸回相同大小的解决方案:
val path = "Y:\\our\\path\\\\to\\directory\\"
val sourceImage = ImageIO.read(File("${path}original.png"))
val smallerImage = BufferedImage(
sourceImage.width / 2,
sourceImage.height / 2,
sourceImage.type
)
var graphics2D = smallerImage.createGraphics()
graphics2D.drawImage(
sourceImage,
0,
0,
sourceImage.width / 2,
sourceImage.height / 2,
null
)
graphics2D.dispose()
在这里,smallerImage
按比例缩小 - 我们现在使用的像素比最初使用的像素少 4 倍(因为我们将宽度和高度都缩放了 2 倍)。
这实现了使用更少像素的目标,但没有保留大小 - 它被缩小了。我们现在需要将其拉伸回原始大小,但现在我们将使用较少数量的像素:
val stretched = smallerImage.getScaledInstance(
sourceImage.width,
sourceImage.height,
Image.SCALE_DEFAULT
)
val destination = BufferedImage(
sourceImage.width,
sourceImage.height,
sourceImage.type
)
graphics2D = destination.createGraphics()
graphics2D.drawImage(stretched, 0, 0, null)
graphics2D.dispose()
最后,我们将图像保存到文件中:
val destinationImageFile = File("${path}destination.png")
ImageIO.write(destination, "png", destinationImageFile)
我们完成了。图像original
和destination
:
保存的像素数仅由您使用的比例因子决定。您必须使用缩放来实现精确的像素数量节省。
推荐阅读
- javascript - 将两个数组合并为字符串
- amazon-web-services - 创建 Amazon EKS 集群时出现 SharedConfigLoadError
- oracle12c - 创建 Oracle 数据库链接和数据库链接密码中的字符
- spring-cloud-stream - 如何使用 spring-cloud-stream-rocketmq 配置多个名称服务器
- php - 管理中间件组路由中未定义错误路由
- c# - 如何在 Qna maker 中应用全词搜索?
- javascript - 具有与 android 包名称不同的 iOS 包 ID 是否不好?
- javascript - 如何使复选框在反应中表现得像单选按钮
- multithreading - 如何在 Common Lisp 中同时运行多个阻塞循环。[将 cl-async 与线程池中的队列相结合]
- css - dl 选择器中的样式启用以设置下一个选择器的样式