Chinaunix首页 | 论坛 | 博客
  • 博客访问: 530166
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2012-02-12 21:06:22


  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.util.Iterator;

  9. import javax.imageio.ImageIO;
  10. import javax.imageio.ImageReader;
  11. import javax.imageio.stream.ImageInputStream;

  12. public class TestImage {

  13.     public static void main(String[] args) throws Exception {

  14.         // 测试图片类型(不支持 tif 格式)
  15.         String[] imgFiles = new String[] { "C:/1/1.bmp", "C:/1/1.gif",
  16.                 "C:/1/1.jpg", "C:/1/1.png", "C:/1/1.tif" };
  17.         for (String file : imgFiles) {
  18.             System.out.println(file + " - " + getImageType(new File(file)));
  19.         }

  20.         // 生成居中的文字图片
  21.         String rootDir = "C:/tmp";
  22.         generateImages(rootDir);
  23.     }

  24.     public static String getImageType(File file) throws Exception {
  25.         ImageInputStream iin = ImageIO
  26.                 .createImageInputStream(new FileInputStream(file));
  27.         Iterator<ImageReader> it = ImageIO.getImageReaders(iin);
  28.         if (it.hasNext()) {
  29.             return it.next().getFormatName();
  30.         }
  31.         return null;

  32.     }

  33.     public static void generateImages(String rootDir) throws Exception {
  34.         File dir = new File(rootDir, "trainingImg");
  35.         dir.mkdirs();
  36.         Font font = new Font("Courier New", Font.PLAIN, 72);
  37.         // Font font = new Font("宋体", Font.PLAIN, 72);
  38.         for (char c = 'a'; c < 'z'; c++) {
  39.             String str = Character.toString(c);
  40.             File file = new File(dir, str + ".png");
  41.             generateImage(str, file, font, 72, 72);
  42.         }
  43.         for (char c = 'A'; c < 'Z'; c++) {
  44.             String str = Character.toString(c);
  45.             File file = new File(dir, "_" + str + ".png");
  46.             generateImage(str, file, font, 72, 72);
  47.         }
  48.         for (char c = '0'; c < '9'; c++) {
  49.             String str = Character.toString(c);
  50.             File file = new File(dir, "_" + str + ".png");
  51.             generateImage(str, file, font, 72, 72);
  52.         }
  53.     }

  54.     public static void generateImage(String str, File file, Font font,
  55.             int height, int width) throws Exception {
  56.         BufferedImage bi = new BufferedImage(width, height,
  57.                 BufferedImage.TYPE_INT_BGR);
  58.         Graphics2D g = bi.createGraphics();
  59.         g.setColor(Color.WHITE);
  60.         g.fillRect(0, 0, width, height);
  61.         g.setFont(font);
  62.         g.setColor(Color.BLACK);

  63.         FontMetrics fm = g.getFontMetrics(font);
  64.         java.awt.geom.Rectangle2D rect = fm.getStringBounds(str, g);
  65.         int textWidth = (int) (rect.getWidth());
  66.         int textHeight = (int) (rect.getHeight());

  67.         int x = (width - textWidth) / 2;
  68.         int y = (height - textHeight) / 2 + fm.getAscent();

  69.         g.drawString(str, x, y);
  70.         g.dispose();
  71.         ImageIO.write(bi, "png", file);
  72.     }
  73. }

  1. // ref:

  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;

  6. import javax.imageio.ImageIO;

  7. public class A {
  8.     public static void main(String[] args) throws IOException {

  9.         // 将彩色图片转为黑白二值图片
  10.         BufferedImage input = ImageIO.read(new File("C:/b1.jpg"));
  11.         BufferedImage im = new BufferedImage(input.getWidth(), input
  12.                 .getHeight(), BufferedImage.TYPE_BYTE_BINARY);
  13.         Graphics2D g2d = im.createGraphics();
  14.         g2d.drawImage(input, 0, 0, null);
  15.         ImageIO.write(im, "jpg", new File("C:/b2.jpg"));

  16.         // 将彩色图片转为灰度图片
  17.         im = new BufferedImage(input.getWidth(), input.getHeight(),
  18.                 BufferedImage.TYPE_BYTE_GRAY);
  19.         g2d = im.createGraphics();
  20.         g2d.drawImage(input, 0, 0, null);
  21.         ImageIO.write(im, "jpg", new File("C:/b3.jpg"));
  22.     }
  23. }



阅读(2265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~