我正在研究Spring-MVC应用程序,我们正在其中集成OCR功能。 OCR有一个习惯,就是为了错误的检测以及在背景中存在图像而丢弃通配符。处理完图像后,我们获得了相当不错的数据,但仍然存在一些错误。我们想要如下处理输出:Java:如何删除字符串中除a-z,数字和德语字符以外的所有字符
- 删除输出字符串中的所有单个字符。
- 删除除A-Z,a-z,德语字符(如äöü,ÄÖÜ,ß)以外的所有字符。
- 空格和数字应保持不变。
代码:
File imageFile = new File(fileLocation);
BufferedImage img = ImageIO.read(imageFile);
BufferedImage blackNWhite = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(img, 0, 0, null);
String blackAndWhiteImage = zipLocation + String.valueOf(new BigInteger(130, random).toString(32))+".png";
File outputfile = new File(blackAndWhiteImage);
ImageIO.write(blackNWhite, "png", outputfile);
ITesseract instance = new Tesseract();
// Point to one folder above tessdata directory, must contain training data
instance.setDatapath("/usr/share/tesseract-ocr/");
// ISO 693-3 standard
instance.setLanguage("deu");
String result = instance.doOCR(outputfile);
//System.out.println(result);
result = result.replaceAll("\\P{ASCII}","");
System.out.println("Result is "+result);
return result;
谢谢。
更新
野生字符由正则表达式左:
|
| '(°Ul")
_} °
=# '
()
...................................__+_......_._._.__._._._+._._.
正则表达式应该是''[[ÖÖäÄüÜßa-zA-Z]'' – osanger