-1

我编写了将图像加载到JEditorPane使用中的代码HTMLEditorKit。我知道如何使用 HTML 调整图像大小。但问题是加载的图像质量下降。我正在努力寻找在不损失质量的情况下调整大小的方法。

4

2 回答 2

0

正如安德鲁汤普森建议的那样,

扩展和HTMLEditorKit覆盖public View create(Element element) .HTMLFactory

扩展ImageView并覆盖该public void paint(Graphics g, Shape a)方法。获取图像并调整其大小。

getScaledInstance(WIDTH, HEIGHT, Image.SCALE_AREA_AVERAGING)

用你最喜欢的缩放比例HINT,最后画出来。

于 2015-07-22T14:53:17.280 回答
0

我不清楚 OP 所追求的确切用例,但我使用了以下更简单的 HTML 帮助查看器替代方法 - 覆盖设置渲染提示的paint()方法JEditorPane

        JEditorPane editor = new JEditorPane() {
            @Override public void paint(Graphics g) {
                if (g instanceof Graphics2D) {
                    Graphics2D g2 = (Graphics2D) g.create();
                    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                        RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                    super.paint(g2);
                    g2.dispose();
                } else {
                    // Print and other non-Graphics2D contexts.
                    super.paint(g);
                }
            }
        };

例如,这通过非积分比例因子 hidpi 屏幕提供了很好且简单的改进。如果图像缩小到原始图像的一半以上,质量仍将是次优的。正如其他人指出的那样,对于那个用例,定制HTMLEditorKit/HTMLVactory生产定制是必要的。ImageView

于 2020-11-21T19:18:41.657 回答