Java生成图片中文成了乱码。
这个图片生成方法是网上搜的,自己加了点东西.英文还好,中文就成了乱码,我电脑是英文版本的.想问下有没什么解决方法不用花太多时间的.另外,这个生成方法生成后图片有点模糊,想...
这个图片生成方法是网上搜的,自己加了点东西.
英文还好,中文就成了乱码,我电脑是英文版本的. 想问下有没什么解决方法不用花太多时间的.
另外,这个生成方法生成后图片有点模糊,想问下有没有其他方法?如果附上代码感激不尽.
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);
}
} 展开
英文还好,中文就成了乱码,我电脑是英文版本的. 想问下有没什么解决方法不用花太多时间的.
另外,这个生成方法生成后图片有点模糊,想问下有没有其他方法?如果附上代码感激不尽.
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);
}
} 展开
3个回答
展开全部
BufferedReader txtRead = new BufferedReader (new FileReader ("test.txt"));
读取tests.txt的时候就是乱码。
解决方法:
把上面那句改成这样就ok了
BufferedReader txtRead=new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"),"gbk"));
读取tests.txt的时候就是乱码。
解决方法:
把上面那句改成这样就ok了
BufferedReader txtRead=new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"),"gbk"));
参考资料: 还有其他问题的话,给我发百度消息
展开全部
你使用的输入输出流错了,使用BufferedReader读取的时候经常会出现中文乱码,你可以试试用管道来处理,可以再加多一个OutputStream
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
多半是因为你的操作系统是英文版引起的,你把代码拿到中文系统上试试。 英文系统中用的编码是cp什么来着,在java中默认会取系统编码来处理。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询