如何使用 tesseract、宽度和背景变量读取验证码
How to read captcha with tesseract, width and background variables
这个验证码可以读吗?我该如何阅读?
---编辑
代码:
public static String imgToTxt(String imgFileName) {
try {
Process pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log"))
.redirectError(new File("/tmp/tesseract-error.log"))
.command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start();
pr.waitFor();
Reader input = new FileReader(new File(imgFileName + ".txt"));
StringWriter output = new StringWriter();
IOUtils.copy(input, output);
return output.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
为了将来的帮助。
我不得不使用 imagemagick 工具处理图像。去除背景并改进字体文本。
public static String imgToTxt(String imgFileName) {
try {
ProcessBuilder pb = new ProcessBuilder("convert", imgFileName + ".png", "-fuzz", "15%", "-fill", "black",
"-opaque", "rgb(16,128,176)", "-fuzz", "40%", "-fill", "white", "+opaque", "black",
imgFileName + ".png");
pb.redirectErrorStream(true);
Process pr = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log"))
.redirectError(new File("/tmp/tesseract-error.log"))
.command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start();
pr.waitFor();
Reader input = new FileReader(new File(imgFileName + ".txt"));
StringWriter output = new StringWriter();
IOUtils.copy(input, output);
return output.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这个验证码可以读吗?我该如何阅读?
---编辑 代码:
public static String imgToTxt(String imgFileName) {
try {
Process pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log"))
.redirectError(new File("/tmp/tesseract-error.log"))
.command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start();
pr.waitFor();
Reader input = new FileReader(new File(imgFileName + ".txt"));
StringWriter output = new StringWriter();
IOUtils.copy(input, output);
return output.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
为了将来的帮助。 我不得不使用 imagemagick 工具处理图像。去除背景并改进字体文本。
public static String imgToTxt(String imgFileName) {
try {
ProcessBuilder pb = new ProcessBuilder("convert", imgFileName + ".png", "-fuzz", "15%", "-fill", "black",
"-opaque", "rgb(16,128,176)", "-fuzz", "40%", "-fill", "white", "+opaque", "black",
imgFileName + ".png");
pb.redirectErrorStream(true);
Process pr = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
pr = (new ProcessBuilder()).redirectOutput(new File("/tmp/tesseract.log"))
.redirectError(new File("/tmp/tesseract-error.log"))
.command("/opt/local/bin/tesseract", imgFileName + ".png", imgFileName).start();
pr.waitFor();
Reader input = new FileReader(new File(imgFileName + ".txt"));
StringWriter output = new StringWriter();
IOUtils.copy(input, output);
return output.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}