【将txt文本转图片】

【测试类】

 1     public static void main(String[] args)
 2     {
 3         try
 4         {
 5             File textFile = new File("F:\java56班\eclipse-SDK-4.2-win32\1.txt");
 6             File imageFile  = new File("F:\java56班\eclipse-SDK-4.2-win32\1.png");
 7             TextToImage texttoimage = new TextToImage(textFile, imageFile);
 8             texttoimage.convert();
 9         }
10         catch (Exception e)
11         {
12             e.printStackTrace();
13         }
14     }

【将文本转换为图片类】

 1 import java.awt.Color;
 2 import java.awt.Font;
 3 import java.awt.Graphics;
 4 import java.awt.image.BufferedImage;
 5 import java.io.BufferedReader;
 6 import java.io.File;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.FileReader;
10 import java.io.IOException;
11 import com.sun.image.codec.jpeg.JPEGImageEncoder;
12 import com.sun.image.codec.jpeg.JPEGCodec;
13  
14 public class TextToImage {
15     
16     /** 文本文件  */
17     private File textFile;
18     /** 图片文件 */
19     private File imageFile;
20     /** 图片 */
21     private BufferedImage image;
22     /** 图片宽度  */
23     private final int IMAGE_WIDTH = 200;
24     /** 图片高度 */
25     private final int IMAGE_HEIGHT = 60;
26     /** 图片类型  */
27     private final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
28      
29     /**
30      * 构造函数
31      * @param textFile 文本文件
32      * @param imageFile 图片文件
33      */
34     public TextToImage(File textFile,File imageFile){
35         this.textFile = textFile;
36         this.imageFile = imageFile;
37         this.image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE);
38     }
39      
40     /**
41      * 将文本文件里文字,写入到图片中保存
42      * @return boolean  true,写入成功;false,写入失败
43      */
44     public boolean convert() {
45          
46         //读取文本文件
47         BufferedReader reader = null;
48         try {
49             reader = new BufferedReader(new FileReader(textFile));
50         } catch (FileNotFoundException e) {
51             e.printStackTrace();
52             return false;
53         }
54          
55         //获取图像上下文
56         Graphics g = createGraphics(image);
57         String line;
58         //图片中文本行高
59         final int Y_LINEHEIGHT = 15;
60         int lineNum = 1;
61         try {
62             while((line = reader.readLine()) != null){
63                 g.drawString(line, 0, lineNum * Y_LINEHEIGHT);
64                 lineNum++;
65             }
66             g.dispose();
67              
68             //保存为jpg图片
69             FileOutputStream fos = new FileOutputStream(imageFile);
70             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
71             encoder.encode(image);
72             fos.close();
73         } catch (IOException e) {
74             e.printStackTrace();
75             return false;
76         }
77         return true;
78     }
79      
80     /**
81      * 获取到图像上下文
82      * @param image 图片
83      * @return Graphics
84      */
85     private Graphics createGraphics(BufferedImage image){
86         Graphics g = image.createGraphics();
87         g.setColor(null); //设置背景色
88         g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);//绘制背景
89         g.setColor(Color.BLACK); //设置前景色
90         g.setFont(new Font("微软雅黑", Font.PLAIN, 15)); //设置字体
91         return g;
92     }
原文地址:https://www.cnblogs.com/tankqiu/p/4248833.html