java在圆圈图像中添加文本

时间:2015-08-07 14:31:17

标签: java awt

我有那张照片:

enter image description here

我需要在图像中添加文字,所以我想编写收到2个字符串并将它们添加到图像的方法,例如:

addText(String str, String str2){...};

第一个字符串总是3个字符长度 第二个字符串可以是1到20个字符长度

addText(“str”,“secondstr”);

结果我想收到base64 png图像字符串,如下所示:

enter image description here

主要问题是如何以“圆圈格式”书写文字?我需要改变我写的每个字符的Y坐标吗?

UPDATE1: 探索链接http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawtextalongacurve.htm,但文字总是从左侧绘制,我如何将它们移动到我的圆圈中心?

3 个答案:

答案 0 :(得分:1)

重要的是这一部分:

for (int i = 0; i < length; i++) {
      Point2D p = gv.getGlyphPosition(i);
      double theta = (double) i / (double) (length - 1) * Math.PI / 4;
      AffineTransform at = AffineTransform.getTranslateInstance(p.getX(),
          p.getY());
      at.rotate(theta);

这里它定义了一个角度theta并使其从0循环到长度/长度* Math.PI / 4,所以它是从0°到45°的旋转。你需要做的是使用值theta(也使用负数)来使它在你想要的地方开始和结束。

根据您发布的图像,第一根弦似乎必须从-20°到20°(或从340°到20°),第二根从225°到135°。请注意,这些度必须以弧度进行转换才能使其正常工作。

答案 1 :(得分:0)

请在此处找到如何在某条曲线上书写文字的示例: draw text along curve

答案 2 :(得分:0)

以下示例程序将以循环方式打印字符串。 degIncrement变量是以度为单位的字符之间的距离。

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

public class RoundTextSample extends JFrame{
    public RoundTextSample() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);

        add(new SamplePanel());
    }

    class SamplePanel extends JPanel{
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            addText(g2, "str", "secondstr");
        }
    }

    public void addText(Graphics2D g2, String string1, String string2) {
        int radius = 40;
        Point center = new Point(120, 120);

        double startDegAngle = 0, offsetAngle;
        double degIncrement = 15, strImageWidth, charWidth;
        boolean isInverted;

        Font font = new Font("Serif", Font.PLAIN, 18);
        FontRenderContext frc = g2.getFontRenderContext();

        charWidth = font.createGlyphVector(frc, "A").getGlyphOutline(0).getBounds2D().getWidth();

        // compute start deg for string1
        offsetAngle =  ((string1.length() * degIncrement) - (degIncrement)) / 2.0;
        startDegAngle = 0 - offsetAngle;

        isInverted = false; // for top text
        addText(g2, string1, font, radius, center, isInverted, startDegAngle, degIncrement);


        // compute start deg for string2
        offsetAngle =  ((string2.length() * degIncrement) - degIncrement) / 2.0;
        startDegAngle = 180 + offsetAngle;
        isInverted = true; // for bottom text
        addText(g2, string2, font, radius, center, isInverted, startDegAngle, degIncrement);

    }

    public void addText(Graphics2D g2, String text, Font font, int radius, Point center, boolean isInverted, double startDeg, double degIncrement) {
        AffineTransform at;
        double deg;

        deg = startDeg;

        FontRenderContext frc = g2.getFontRenderContext();
        if (!isInverted) {
            radius -= font.createGlyphVector(frc, "A").getGlyphOutline(0).getBounds2D().getHeight();
        } else {
            degIncrement = 0 - degIncrement;
        }

        for (int i=0; i<text.length(); i++) {
            String s = ""+text.charAt(i);
            GlyphVector gv = font.createGlyphVector(frc, s);
            Shape glyph = gv.getGlyphOutline(0);
            double charImageCenterX = glyph.getBounds2D().getCenterX();

            double theta = Math.toRadians(deg);

            int xp = (int)Math.round(Math.sin(theta) * radius);
            int yp = (int)Math.round(Math.cos(theta) * radius);
            yp = 0 - yp;

            if (isInverted)
                at = new AffineTransform( -1.0, 0.0, 0.0, -1.0, center.x + xp, center.y + yp );
            else
                at = new AffineTransform( 1.0, 0.0, 0.0, 1.0, center.x + xp, center.x + yp );

            at.rotate(theta);
            at.translate(0-charImageCenterX, 0);

            Shape transformedGlyph = at.createTransformedShape(glyph);
            g2.fill(transformedGlyph);

            deg += degIncrement;
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RoundTextSample().setVisible(true);
            }
        });
    }

}

示例输出:

enter image description here

输出注释:

enter image description here