Java中FOP图像缩放问题
好的,这就是我的问题:
我们使用 FOP 来创建“漂亮”的报告输出。 如果用户想要文件,我们使用 pdf 选项,使用 AWT 进行预览,使用 -print 选项进行打印。 我们使用的是 FOP 0.25.x,我完全认识到它不是最新版本,但升级到 0.95 似乎是一项不简单的任务,我不一定愿意承担。
无论如何,我们的一位用户注意到,当将身份证(通过 FOP -print 选项生成)打印到身份证打印机时,卡上的图像(员工的照片)有一些损坏......有点就像绿色和红色的点和线一样。 我们还发现,如果我们向一台 HP 彩色激光打印机发送完全相同的打印请求,它会打印得很好。 更奇怪的是,如果我们使用 FOP 创建身份证的 PDF,然后通过证卡打印机上的 acrobat reader 进行打印,则打印效果很好。
我最终发现这与图像的缩放有关……我们将 600 像素高的图像缩放到 120 像素左右。 如果我预先缩小图像的大小,即使只是将它们减半,损坏也会明显减少。 同样,当我放大图像时,腐败现象也会增加。
所以我的问题是:有人知道这里发生了什么吗? 或者曾经遇到过这样的事情吗?
由于我不知道为什么会发生这种情况,所以我不知道如何解决根本原因,但我一直在研究一些各种解决方法:
1)使用 FOP 创建图像的 pdf,然后通过打印爪哇。 这似乎是一个显而易见的答案,但一些谷歌搜索表明,通过 Java 打印 PDF 并不简单。 我在 java.net 上看到过 PDF Renderer 项目,但对于单个非常具体的应用程序来说似乎相当庞大。
2) 在将图像提供给 FOP 之前尝试调整图像大小。 这看起来也很简单,但是我们的各种用户可以根据自己的需要为这些身份证设置样式表,并且在其中使用“pt”和“in”大小似乎很常见......我不知道有什么好方法将其映射到像素大小调整。
如果有人对根本原因、使这些变通办法发挥作用的方法和/或其他想法有任何见解,那么您将感激我。
Okay, so here's my problem:
We use FOP for creating "pretty" report output. We use the pdf option if the user wants a file, AWT for previewing, and the -print option for printing them. We are using FOP 0.25.x, which I fully recognize is not the newest version, but upgrading to 0.95 appears to be a non-trivial task that I don't necessarily want to undertake.
Anyway, it was noticed by one of our users that when printing ID cards (generated via FOP -print option) to the id card printer, the images on the cards (pictures of the employees) had some corruption in them...sort of like like green and reds dots and lines. We also discovered that if we sent the exact same print request to one of our HP color laserjets, it printed fine. To add to the strangeness, if we use FOP to create a PDF of the ID card and then print it via acrobat reader on the card printer, it prints fine.
I eventually discovered that it had something to do with the scaling of the images...we were scaling 600px high images down to something like 120px. If I presized the images down, even just halfing them, the corruption went down noticeably. Similarly, when I upsized the images, the corruption went up.
So my question: anybody have ANY idea what is going on here? Or has ever run into such a thing?
Since I don't know why this is happening, I don't know how to fix the root cause, but I've been working through some various workarounds:
1) Use FOP to create a pdf of the image and then print that via Java. This seems like an obvious answer, but some Googling around showed that printing a PDF via Java is not trivial. I've seen the PDF Renderer project on java.net, but seems pretty bulky for a single very specific application.
2) Try to resize the images before giving it to FOP. This also seemed pretty straightforward, however our various users can setup stylesheets for these id cards however they want and using "pt" and "in" sizing in them seems to be pretty common...I don't know of any good way to map that to a pixel resizing.
If anybody has any insight into the root cause, ways to make these work arounds work, and/or another idea, you'd be in my debt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最确定的解释:
图像损坏? 这是一个错误。
为什么不使用0.95? 迟早你必须升级,Apache 联盟不会
修复 0.25.x 版本中的错误。
您不能指望为将来可能出现的每个错误找到解决方法。
Most certain explanation:
image corruption? it's a bug.
Why not use 0.95? Sooner or later you have to upgrade, Apache consortium won't
fix bugs in 0.25.x versions.
You can't hope to find workarounds for every bugs which might occur in future.
我最终做了我在原始问题中提到的第二件事......即在将其交给 FOP 之前调整其大小。 我发现我可以检索要打印的打印机的 dpi,并对其进行一些数学运算以获得像素大小。 似乎在我的所有测试中都完美工作......不是真正的解决方案,而是足够的解决方法。
I ended up doing the second thing I mention in the original question...i.e. resizing it before giving it to FOP. I found that I could retrieve the dpi of the printer I was printing to and do some math on it to get pixel sizing. Seems to work perfectly in all my testing...not a real solution but an adequate workaround.