import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
public class TestCHSGraphic
{
public static void main(String[] args) throws Exception
{
System.setProperty("java.awt.headless", "true");
BufferedImage bi = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
String s = "中文测试";
// 注意!具体转码可能有所不同!
g.drawString(new String(s.getBytes(), "GB2312"), 50, 50);
bi.flush();
FileOutputStream fos = new FileOutputStream("test.jpg");
JPEGImageEncoder jpegie = JPEGCodec.createJPEGEncoder(fos);
jpegie.encode(bi);
fos.close();
}
}
TestCHSGraphic.java(jdk1.4)
import java.awt.Graphics;
import java.awt.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
public class TestCHSGraphic
{
public static void main(String[] args) throws Exception
{
System.setProperty("java.awt.headless", "true");
BufferedImage bi = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
String s = "中文测试";
// 注意!具体转码可能有所不同!
g.drawString(new String(s.getBytes(), "GB2312"), 50, 50);
ImageIO.write(bi, "jpeg", new File("test.jpg"));
}
}
本文地址:http://com.8s8s.com/it/it34803.htm