如何使图像中的文本边缘平滑?

时间:2011-09-26 03:41:42

标签: java image

如何使图像中的文本边缘平滑?

这是图像:

Text in image is not smooth

3 个答案:

答案 0 :(得分:3)

如果您正在考虑抗锯齿,可以将Graphics转换为Graphics2D,然后使用g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);。您可以在绘制线条后执行此操作。

答案 1 :(得分:1)

Kevin Hikaru Evans的上述解决方案应该有效,也许你错过了一些东西。

Graphics2D g2=(Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawString("SCQSCQSCQ",x,y);

答案 2 :(得分:1)

尝试这个(快速,粗略)改编的例子。

Smooth text

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.File;

class PictureText {

    public static BufferedImage getImage(Area textOutline) {
        Rectangle bounds = textOutline.getBounds();
        System.out.println(bounds);
        int width = (2*(int)bounds.getX())+(int)bounds.getWidth();
        int height = (2*(int)bounds.getY())+(int)bounds.getHeight();
        BufferedImage bi = new BufferedImage(
            width,
            height,
            BufferedImage.TYPE_INT_ARGB);

        Color outline = new Color(0,0,0,255);

        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        g.setStroke(new BasicStroke(1.5f));
        g.setColor(outline);
        g.draw(textOutline);

        showImage(bi);

        return bi;
    }

    public static void showImage(Image image) {
        JLabel textLabel = new JLabel(
            new ImageIcon(image));
        textLabel.setBackground(Color.WHITE);
        textLabel.setOpaque(true);

        JPanel gui = new JPanel(new GridLayout(0,1,5,5));
        gui.add(textLabel);

        JOptionPane.showMessageDialog(null,gui);
    }

    public static void main(String[] args) throws Exception {

        AffineTransform shrinkTransform2 =
            AffineTransform.getScaleInstance(.5,.5);
        AffineTransform shrinkTransform4 =
            AffineTransform.getScaleInstance(.25,.25);

        final BufferedImage originalImage = new BufferedImage(
            260,
            50,
            BufferedImage.TYPE_INT_ARGB);
        GradientPaint gp = new GradientPaint(
            0f,0f,Color.GRAY.brighter(),
            0f,22f,Color.GRAY.brighter().brighter(),true);
        Graphics2D g0 = originalImage.createGraphics();
        g0.setPaint(gp);
        g0.fillRect(0,0,300,100);

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        final BufferedImage textImage = new BufferedImage(
            width,
            height,
            BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = textImage.createGraphics();

        FontRenderContext frc = g.getFontRenderContext();

        Font font1 = new Font(
            //"Wide Latin"
            //"Pythagoras"
            "Denmark"
            ,0,48);
        GlyphVector gv1 = font1.createGlyphVector(
            frc, "The quick brown fox..");
        Shape shape1 = gv1.getOutline(0,0);
        int y = (int)shape1.getBounds().getHeight()+2;
        Shape shapea = gv1.getOutline(6,y);
        Area area1 = new Area(shapea);
        Area area2nd = area1.createTransformedArea(shrinkTransform2);
        Area area4th = area1.createTransformedArea(shrinkTransform4);

        ImageIO.write(getImage(area1),"png",new File("text-image.png"));
    }
}