How can I make the text edges in the images smooth? [closed]
How can I make the text edges in the images smooth?
Here's the image:
If you're thinking of anti aliasing, you can typecast Graphics to Graphics2D then use g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
. You can do this after you draw the lines.
The above solution from Kevin Hikaru Evans should works, maybe you missed something.
Graphics2D g2=(Graphics2D)g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawString("SCQSCQSCQ",x,y);
Try this (quickly, crudely) adapted example..
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"));
}
}
精彩评论