Java + Tesseract,53个字节
由于我没有Mathematica,因此我决定稍微修改规则,并使用Tesseract进行OCR。我编写了一个程序,使用各种字体,大小和样式将“ 2014”渲染为图像,并找到了被识别为“ 2014”的最小图像。结果取决于可用的字体。
这是我计算机上的获胜者-53字节,使用“ URW Gothic L”字体:
码:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ocr {
public static boolean blankLine(final BufferedImage img, final int x1, final int y1, final int x2, final int y2) {
final int d = x2 - x1 + y2 - y1 + 1;
final int dx = (x2 - x1 + 1) / d;
final int dy = (y2 - y1 + 1) / d;
for (int i = 0, x = x1, y = y1; i < d; ++i, x += dx, y += dy) {
if (img.getRGB(x, y) != -1) {
return false;
}
}
return true;
}
public static BufferedImage trim(final BufferedImage img) {
int x1 = 0;
int y1 = 0;
int x2 = img.getWidth() - 1;
int y2 = img.getHeight() - 1;
while (x1 < x2 && blankLine(img, x1, y1, x1, y2)) x1++;
while (x1 < x2 && blankLine(img, x2, y1, x2, y2)) x2--;
while (y1 < y2 && blankLine(img, x1, y1, x2, y1)) y1++;
while (y1 < y2 && blankLine(img, x1, y2, x2, y2)) y2--;
return img.getSubimage(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
}
public static int render(final Font font, final int w, final String name) throws IOException {
BufferedImage img = new BufferedImage(w, w, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = img.createGraphics();
float size = font.getSize2D();
Font f = font;
while (true) {
final FontMetrics fm = g.getFontMetrics(f);
if (fm.stringWidth("2014") <= w) {
break;
}
size -= 0.5f;
f = f.deriveFont(size);
}
g = img.createGraphics();
g.setFont(f);
g.fillRect(0, 0, w, w);
g.setColor(Color.BLACK);
g.drawString("2014", 0, w - 1);
g.dispose();
img = trim(img);
final File file = new File(name);
ImageIO.write(img, "gif", file);
return (int) file.length();
}
public static boolean ocr() throws Exception {
Runtime.getRuntime().exec("/usr/bin/tesseract 2014.gif out -psm 8").waitFor();
String t = "";
final BufferedReader br = new BufferedReader(new FileReader("out.txt"));
while (true) {
final String s = br.readLine();
if (s == null) break;
t += s;
}
br.close();
return t.trim().equals("2014");
}
public static void main(final String... args) throws Exception {
int min = 10000;
for (String s : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
for (int t = 0; t < 4; ++t) {
final Font font = new Font(s, t, 50);
for (int w = 10; w < 25; ++w) {
final int size = render(font, w, "2014.gif");
if (size < min && ocr()) {
render(font, w, "2014win.gif");
min = size;
System.out.println(s + ", " + size);
}
}
}
}
}
}