我试图对某些文字给出一个相当粗的笔划,这样做可以让我在某些角色周围留下空白,例如“我”。和'。'
我已经尝试了RenderingHints.KEY_STROKE_CONTROL和KEY_FRACTIONALMETRICS的所有排列(只有两个似乎与此有任何关系(KEY_ANTIALIASING和KEY_RENDERING除外)。我已尝试用TextLayout接近这两个我不知道如何继续进行。理想情况下,我希望使用VALUE_STROKE_PURE来实现这一点,因为这样可以获得最平滑的结果,但最终也会产生最大的差距。任何帮助将不胜感激!
public class TestLabelImage
{
public static void main(String[] args)
{
BufferedImage labelImg = getTextAsBmp("testing.",
Color.BLACK,
Color.WHITE,
"TimesRoman",
Font.PLAIN,
36,
18.0f);
}
static public void setRenderingHings(Graphics2D g2d)
{
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
//g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
//g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
//g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
//g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
}
static public BufferedImage getTextAsBmp(String text, Color textColor, Color textOutlineColor, String font, int style, int size, float stroke)
{
//create a temporary image so we can make use of Graphics class
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
setRenderingHings(g2d);
Font f = new Font(font, style, size);
FontMetrics metrics = g2d.getFontMetrics(f);
//Option 1:
TextLayout textTl = new TextLayout(text, f, g2d.getFontRenderContext());
Shape outline = textTl.getOutline(null);
//Option 2:
//GlyphVector glyphVector = f.createGlyphVector(g2d.getFontRenderContext(), text);
//Shape outline = glyphVector.getOutline();
g2d.dispose();
//create a new image to the calculated height and width
img = new BufferedImage((int)(outline.getBounds().getWidth() + stroke), (int)(outline.getBounds().getHeight() + stroke), BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
setRenderingHings(g2d);
g2d.setColor(Color.RED);
g2d.fillRect(0,0,img.getWidth(),img.getHeight());
g2d.translate((img.getWidth()/2 - outline.getBounds().getWidth()/2), outline.getBounds().getHeight() + (stroke)/2 - metrics.getDescent());
g2d.setColor(textOutlineColor);
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
//g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
g2d.draw(outline);
g2d.setColor(textColor);
g2d.fill(outline);
g2d.dispose();
return img;
}
}