Java 使用 Graphics2D 模糊背景

Java blurrying background with Graphics2D

我正在使用 Java 创建带有文本的图像。我设置背景颜色并填充它。然后我使用 LineBreakMeasurer 放置文本。问题是(如您在附图中所见)文本周围的图像背景模糊(如果放大)。这对我来说是个问题。有可能解决这个问题吗?

final BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setColor(new Color(255,0,0));
    g2d.fillRect (0, 0, image.getWidth(), image.getHeight());

...

AttributedString attributedString = getAttributedString(text, fontSize, new Color(1.0f,1.0f,1.0f,0.9f));
    AttributedCharacterIterator characterIterator = attributedString
            .getIterator();
    FontRenderContext fontRenderContext = g2d.getFontRenderContext();
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
            fontRenderContext);
    while (measurer.getPosition() < characterIterator.getEndIndex()) {
        TextLayout textLayout = measurer.nextLayout(width);
        int currentX = x + (int)(width - textLayout.getVisibleAdvance()) / 2;
        y += textLayout.getAscent();

        textLayout.draw(g2d, currentX, y);
    }

如果您指的是字母周围的人工制品,那么问题不在于您的绘画代码。这是将图像保存为 JPEG 的结果。使用无损格式保存它,例如 PNG。您也可以尝试降低 JPEG 的压缩率,但另存为 PNG 可能更容易。