从文件夹中一张一张的图片文本。这个只有一张图片。如何处理所有图片
public static String crackImage(String filepath) throws TesseractException {
File imgfile = new File(filepath);
ITesseract instance = new Tesseract();
instance.setDatapath("C:\\selenium_work\\ScrappingText.PDF\\tessdata");
String result = instance.doOCR(imgfile);
return result;
}
public static void main(String[] args) throws TesseractException {
String textdata = ImageScrapping.crackImage("C:\\selenium_work\\ScrappingText.PDF\\image\\IMG_20190305_152800__01.jpg");
System.out.println(textdata);
您可以从给定的 URL 保存图像,例如:
找到所有的<img>
标签,并提取它们的src
属性,即使用XPath的定位器的//img
List<String> imagesUrls = driver.findElements(By.xpath("//img"))
.stream()
.map(img -> img.getAttribute("src"))
.collect(Collectors.toList());
使用OkHTTP 客户端库将所有图像下载到您选择的文件夹中:
for (String imageUrl : imagesUrls) {
Request request = new Request.Builder().url(imageUrl).build();
Response response = client.newCall(request).execute();
File downloadedLogo = new File("C:\\selenium_work\\ScrappingText.PDF\\image\\" + imageUrl.substring(imageUrl.lastIndexOf('/') + 1));
BufferedSink sink = Okio.buffer(Okio.sink(downloadedLogo));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();
}
完成后,您应该能够crackImage
通过 Files.walk() 方法为每个下载的文件调用您的函数:
Files.walk(Paths.get("C:\\selenium_work\\ScrappingText.PDF\\image"))
.forEach(file -> System.out.println(crackImage(file.toAbsolutePath().toString())));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句