//将文字转换为图片
public static boolean saveStringToPic(String content, String filePath) {
boolean rs;
     int width = 1024; //300;
     int height = 600; 
int lineLength = 47;//23;
int lineLength2 = 47;//24;
String content1 = content;
String content2 = "";
String content3 = "";
int cnLineLength = lineLength;
for (int i = 0;i < content.length();i++) {
char ca = content.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength = cnLineLength - 1;
}
if (i >= cnLineLength) break;
}
if (content.length() > cnLineLength) {
content1 = content.substring(0, cnLineLength);
content2 = content.substring(cnLineLength);
String line2 = content2;
int cnLineLength2 = lineLength2;
for (int i = 0;i < line2.length();i++) {
char ca = line2.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength2 = cnLineLength2 - 1;
}
if (i >= cnLineLength2) break;
}
if (line2.length() > cnLineLength2) {
content2 = line2.substring(0, cnLineLength2);
content3 = line2.substring(cnLineLength2);
}
}
BufferedImage bi = null; 
     Graphics2D g2 = null;
     OutputStream outPutStream = null;
     try{ 
        bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
        g2 = (Graphics2D) bi.getGraphics(); 
        //g2.setBackground(Color.white); 
        Color bg = new Color(255, 255, 255);
            g2.setColor(bg);
            g2.fillRect(0, 0, width, height);
        g2.setColor(Color.black); 
        Font font = new Font("宋体", Font.BOLD, 40);
        g2.setFont(font);
        g2.drawString(content1, 10, 200); 
        g2.drawString(content2, 10, 300); 
        g2.drawString(content3, 10, 400); 
        outPutStream = new FileOutputStream(filePath); 
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outPutStream); 
        encoder.encode(bi);
        rs = true;
     } catch (Exception e) {
   e.printStackTrace();
   rs = false;
     } finally {
            if (g2 != null){ 
             g2.dispose();
            } 
            if (outPutStream != null) {
             try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
            }
     } 
        return rs;
}

解决方案 »

  1.   

    怎么不知所云,就是将一段文字转换为图片保存起来.
      

  2.   

    完全可以,否则 注册的时候那验证码怎么来的???不就是随机的字符 整成图片显示的嘛不过楼主0回帖率
      

  3.   

    lz是传授知识呢?还是传授知识呢?
      

  4.   

    你这是在问问题还在送思路和代码
      

  5.   

              将一段文字转换为图片 -?    我没见过 - -  楼主你会吗?  告诉我?
      

  6.   

    回答 不知所云~ 也能得分 我日哦