这个图片生成方法是网上搜的,自己加了点东西.英文还好,中文就成了乱码,我电脑是英文版本的.想问下有没什么解决方法不用花太多时间的.另外,这个生成方法生成后图片有点模糊,想问下有没...
这个图片生成方法是网上搜的,自己加了点东西.
英文还好,中文就成了乱码,我电脑是英文版本的. 想问下有没什么解决方法不用花太多时间的.
另外,这个生成方法生成后图片有点模糊,想问下有没有其他方法?如果附上代码感激不尽.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.awt.font.*;
import java.awt.geom.*;
public class ImgTest
{
static String line[] = new String [999];
public static void ReadFile () throws Exception
{
BufferedReader txtRead = new BufferedReader (new FileReader ("test.txt"));
int count = 0;
line [count] = txtRead.readLine ();
while (line [count] != null)
{
count++;
line [count] = txtRead.readLine ();
}
}
public static void main (String[] args) throws Exception
{
ReadFile ();
//System.out.println (line [0]);
WriteImage (line [0]);
}
public static void WriteImage (String s) throws Exception
{
File file = new File ("C:\\Documents and Settings\\Owner\\Desktop\\ImgTest.jpg");
Font font = new Font ("Serif", Font.BOLD, 10);
int width = 100;
int height = 100;
BufferedImage bi = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics ();
g2.setBackground (Color.WHITE);
g2.clearRect (0, 0, width, height);
g2.setPaint (Color.RED);
FontRenderContext context = g2.getFontRenderContext ();
Rectangle2D bounds = font.getStringBounds (s, context);
double x = (width - bounds.getWidth ()) / 2;
double y = (height - bounds.getHeight ()) / 2;
double ascent = -bounds.getY ();
double baseY = y + ascent;
g2.drawString (s, (int) x, (int) baseY);
ImageIO.write (bi, "jpg", file);
}
}
展开