- import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.FontMetrics;
-
import java.awt.Graphics2D;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.util.Iterator;
-
-
import javax.imageio.ImageIO;
-
import javax.imageio.ImageReader;
-
import javax.imageio.stream.ImageInputStream;
-
-
public class TestImage {
-
-
public static void main(String[] args) throws Exception {
-
-
// 测试图片类型(不支持 tif 格式)
-
String[] imgFiles = new String[] { "C:/1/1.bmp", "C:/1/1.gif",
-
"C:/1/1.jpg", "C:/1/1.png", "C:/1/1.tif" };
-
for (String file : imgFiles) {
-
System.out.println(file + " - " + getImageType(new File(file)));
-
}
-
-
// 生成居中的文字图片
-
String rootDir = "C:/tmp";
-
generateImages(rootDir);
-
}
-
-
public static String getImageType(File file) throws Exception {
-
ImageInputStream iin = ImageIO
-
.createImageInputStream(new FileInputStream(file));
-
Iterator<ImageReader> it = ImageIO.getImageReaders(iin);
-
if (it.hasNext()) {
-
return it.next().getFormatName();
-
}
-
return null;
-
-
}
-
-
public static void generateImages(String rootDir) throws Exception {
-
File dir = new File(rootDir, "trainingImg");
-
dir.mkdirs();
-
Font font = new Font("Courier New", Font.PLAIN, 72);
-
// Font font = new Font("宋体", Font.PLAIN, 72);
-
for (char c = 'a'; c < 'z'; c++) {
-
String str = Character.toString(c);
-
File file = new File(dir, str + ".png");
-
generateImage(str, file, font, 72, 72);
-
}
-
for (char c = 'A'; c < 'Z'; c++) {
-
String str = Character.toString(c);
-
File file = new File(dir, "_" + str + ".png");
-
generateImage(str, file, font, 72, 72);
-
}
-
for (char c = '0'; c < '9'; c++) {
-
String str = Character.toString(c);
-
File file = new File(dir, "_" + str + ".png");
-
generateImage(str, file, font, 72, 72);
-
}
-
}
-
-
public static void generateImage(String str, File file, Font font,
-
int height, int width) throws Exception {
-
BufferedImage bi = new BufferedImage(width, height,
-
BufferedImage.TYPE_INT_BGR);
-
Graphics2D g = bi.createGraphics();
-
g.setColor(Color.WHITE);
-
g.fillRect(0, 0, width, height);
-
g.setFont(font);
-
g.setColor(Color.BLACK);
-
-
FontMetrics fm = g.getFontMetrics(font);
-
java.awt.geom.Rectangle2D rect = fm.getStringBounds(str, g);
-
int textWidth = (int) (rect.getWidth());
-
int textHeight = (int) (rect.getHeight());
-
-
int x = (width - textWidth) / 2;
-
int y = (height - textHeight) / 2 + fm.getAscent();
-
-
g.drawString(str, x, y);
-
g.dispose();
-
ImageIO.write(bi, "png", file);
-
}
-
}
- // ref:
-
-
import java.awt.Graphics2D;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
-
public class A {
-
public static void main(String[] args) throws IOException {
-
-
// 将彩色图片转为黑白二值图片
-
BufferedImage input = ImageIO.read(new File("C:/b1.jpg"));
-
BufferedImage im = new BufferedImage(input.getWidth(), input
-
.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
-
Graphics2D g2d = im.createGraphics();
-
g2d.drawImage(input, 0, 0, null);
-
ImageIO.write(im, "jpg", new File("C:/b2.jpg"));
-
-
// 将彩色图片转为灰度图片
-
im = new BufferedImage(input.getWidth(), input.getHeight(),
-
BufferedImage.TYPE_BYTE_GRAY);
-
g2d = im.createGraphics();
-
g2d.drawImage(input, 0, 0, null);
-
ImageIO.write(im, "jpg", new File("C:/b3.jpg"));
-
}
-
}
阅读(2264) | 评论(0) | 转发(0) |