package org.leason.model;import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;import javax.imageio.ImageIO;
public class NumberCaption {
private String IMAGE_TYPE="bmp";
private int PICTURE_HEIGHT=32;
private int PICTURE_WIDTH=32;
private BufferedImage bufferedImage=null;
public NumberCaption() {
this.bufferedImage = new BufferedImage(PICTURE_WIDTH, PICTURE_HEIGHT,
BufferedImage.TYPE_BYTE_BINARY);
}
public String[] readNumberCaptionFile(File file){
String st[]=new String [10];
try {
FileReader fileReader =new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileReader);
String temp;

try {
while((temp=bufferedReader.readLine())!=null){
st=temp.split(",");

}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return st;
}    private void saveNumberImage(File file){
     try {
ImageIO.write(this.bufferedImage, this.IMAGE_TYPE, file);
} catch (IOException e) {
    e.printStackTrace();
}
    }
/**
 * 把绘制黑色的字(0-9)
 * @param st
 * @param g
 */
private void drawNumberCaption(String [] st){
Graphics g=this.bufferedImage.getGraphics();
this.fillBackGround(g);
FontMetrics fm = g.getFontMetrics();
int fontHeight = (fm.getAscent() + fm.getDescent() + fm.getLeading());
// 设置开始绘制文字的位置,因为一个字体包括三个部分ascent, descent, leading绘制的时候按照ascent为基准
int startPointY = PICTURE_HEIGHT - (PICTURE_HEIGHT - fontHeight) / 2
- (fm.getDescent() + fm.getLeading());
// 计算字体的高度和宽度
for(int i=0;i<st.length;i++){
int fontWidth = fm.stringWidth(st[i]);
int startPointX = (PICTURE_WIDTH - fontWidth) / 2;
System.out.println("jar:"+st[i]);
g.drawString(st[i], startPointX, startPointY);
}
}
/**
 * 填充白色背景
 * 
 * @param g
 */
private void fillBackGround(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0,this.PICTURE_WIDTH, this.PICTURE_HEIGHT);
}
public static void main(String[] args) {
NumberCaption numberCaption=new NumberCaption();
   String st[]= numberCaption.readNumberCaptionFile(new File("d:\\ocr\\number.txt"));
   numberCaption.drawNumberCaption(st);
    for(int i=0;i<st.length;i++){
     numberCaption.saveNumberImage(new File("d:\\ocr\\numberImage\\"+st[i]+".bmp"));
    }
}}
以上是我的代码; 
先从一个文件里读取0到9十个数字,然后返回一个数组;
然后把在绘字。设置图片;
最后保存;
为什么0到9十个数字的图片在d:\\ocr\\numberImage的目录下是10张图片没有数字?是十张白色的图片?
请问什么原因?

解决方案 »

  1.   

    绘制文字前,要g.setColor(Color.black);设置下颜色
      

  2.   

    我把上面设置字体的函数加了g.setColor(Color.black);
    还是不行啊 ?
    请问是什么原因?
    设置字体的代码如下:
    private   void   drawNumberCaption(String   []   st){ 
    Graphics   g=this.bufferedImage.getGraphics(); 
    this.fillBackGround(g); 
    g.setColor(Color.black);
    FontMetrics   fm   =   g.getFontMetrics(); 
    int   fontHeight   =   (fm.getAscent()   +   fm.getDescent()   +   fm.getLeading()); 
    //   设置开始绘制文字的位置,因为一个字体包括三个部分ascent,   descent,   leading绘制的时候按照ascent为基准 
    int   startPointY   =   PICTURE_HEIGHT   -   (PICTURE_HEIGHT   -   fontHeight)   /   2 
    -   (fm.getDescent()   +   fm.getLeading()); 
    //   计算字体的高度和宽度 
    for(int   i=0;i <st.length;i++){ 
    int   fontWidth   =   fm.stringWidth(st[i]); 
    int   startPointX   =   (PICTURE_WIDTH   -   fontWidth)   /   2; 
    System.out.println("jar:"+st[i]); 
    g.drawString(st[i],   startPointX,   startPointY); 


      

  3.   

    你把数字0-9都写在一个BufferedImage里面啦
    ,还有,你没有设置字体,特别是高度
    package test;import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;import javax.imageio.ImageIO;public class NumberCaption {
      private String IMAGE_TYPE = "bmp";  private int PICTURE_HEIGHT = 32;  private int PICTURE_WIDTH = 32;  private BufferedImage bufferedImage[] = null;  public NumberCaption() {  }  public String[] readNumberCaptionFile(File file) {
        String st[] = new String[10];
        try {
          FileReader fileReader = new FileReader(file);
          BufferedReader bufferedReader = new BufferedReader(fileReader);
          String temp;      try {
            while ((temp = bufferedReader.readLine()) != null) {
              st = temp.split(",");        }
          } catch (IOException e) {
            e.printStackTrace();
          }
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
        return st;
      }  private void saveNumberImage(int id, File file) {
        try {
          ImageIO.write(this.bufferedImage[id], this.IMAGE_TYPE, file);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }  /**
       * 把绘制黑色的字(0-9)
       * 
       * @param st
       * @param g
       */
      private void drawNumberCaption(String[] st) {
        bufferedImage = new BufferedImage[st.length];    for (int i = 0; i < st.length; i++) {
          bufferedImage[i] = new BufferedImage(PICTURE_WIDTH, PICTURE_HEIGHT, BufferedImage.TYPE_INT_RGB);
          Graphics g = this.bufferedImage[i].getGraphics();
          this.fillBackGround(g);
          FontMetrics fm = g.getFontMetrics();
          int fontHeight = (fm.getAscent() + fm.getDescent() + fm.getLeading());
          // 设置开始绘制文字的位置,因为一个字体包括三个部分ascent, descent, leading绘制的时候按照ascent为基准
          int startPointY = PICTURE_HEIGHT - (PICTURE_HEIGHT - fontHeight) / 2 - (fm.getDescent() + fm.getLeading());
          // 计算字体的高度和宽度
          g.setColor(new Color(255, 0, 0));
          g.setFont(new Font("Times New Roman", Font.ITALIC, 16));      int fontWidth = fm.stringWidth(st[i]);
          int startPointX = (PICTURE_WIDTH - fontWidth) / 2;
          System.out.println("jar:" + st[i]);
          g.drawString(st[i], startPointX, startPointY);
        }
      }  /**
       * 填充白色背景
       * 
       * @param g
       */
      private void fillBackGround(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, this.PICTURE_WIDTH, this.PICTURE_HEIGHT);
      }  public static void main(String[] args) {
        NumberCaption numberCaption = new NumberCaption();
        String st[] = numberCaption.readNumberCaptionFile(new File("d:\\ocr\\number.txt"));
        numberCaption.drawNumberCaption(st);
        for (int i = 0; i < st.length; i++) {
          numberCaption.saveNumberImage(i, new File("d:\\ocr\\numberImage\\" + st[i] + ".bmp"));
        }
      }}