【问题标题】:HTML Text to Image and saving as blob to DatabaseHTML文本到图像并作为blob保存到数据库
【发布时间】:2017-01-12 14:32:19
【问题描述】:

我已将富文本转换为图像并作为 blob 保存到数据库。 Blob 可以从本地服务器 (WINDOWS) 很好地创建,但在部署到应用程序服务器 (LINUX) 时。图像格式(字体、清晰度)发生了变化。我不确定我必须做什么。请解释一下。这是我使用的代码。

        BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);      
            Graphics graphics = image.createGraphics();
            JEditorPane jep = new JEditorPane("text/html", html);
            jep.setSize(width, height);
            jep.print(graphics);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", new File("C:\\Dev\\img8_TYPE_INT_RGB.png"));
            ImageIO.write(image, "png", bos);

哪种图像格式适合文本内容?

提前致谢。

来自本地服务器(WINDOWS)的图像

来自 Linux 服务器(应用服务器)的映像(Linux)

【问题讨论】:

  • 您指定两种图像格式,JPEG 用于写入(特定于 Windows 的)文件,GIF 用于写入内存流。 “图像格式(字体,清晰度)发生了变化”是什么意思?如果您发布与 Windows 和 Linux 输出的并排比较,可能更容易理解/可视化。很可能字体/抗锯齿发生了变化,因为您使用了 Swing 组件 (JEditorPane),它对 Windows 和 Linux (Gnome/KDE/whatever) 具有不同的“外观”。您可以通过跨平台的外观来解决这个问题。
  • 我这里复制的图片都是.png类型,一种来自本地,一种来自服务器,使用了BufferedImage.TYPE_INT_RGB..让我编辑我的代码。
  • 您之前存储的字体和清晰度的文本是什么,是否也显示在JEditorPane中?
  • Yes In JEditorPane - "font-family: Arial, Verdana; font-size: 13.3333px。看看我复制的图片。
  • 两个平台上都有 Arial 吗?您使用什么 Swing 外观和感觉(针对每个平台)?您使用哪些 Java2D 渲染管道?如果您希望获得相同的结果,您可能需要在两个平台上使用相同的方法。

标签: java graphics bufferedimage javax.imageio


【解决方案1】:

我也用过这段代码,还是有同样的问题。

BufferedImage image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();               
graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
JEditorPane jep = new JEditorPane("text/html", html);
jep.setSize(width, height);
jep.print(graphics);

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2017-11-30
    • 2016-07-02
    • 2015-12-10
    • 2011-07-23
    • 2013-12-05
    • 2017-10-22
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多