从文件夹中一张一张地抓取图像文本。这一张仅适用于一张图像。如何处理所有图像
public static String crackImage(String filepath) throws TesseractException {
File imgfile = new File(filepath);
ITesseract instance = new Tesseract();
instance.setDatapath("C:\\selenium_work\\ScrapingText.PDF\\tessdata");
String result = instance.doOCR(imgfile);
return result;
}
public static void main(String[] args) throws TesseractException {
String textdata = ImageScraping.crackImage("C:\\selenium_work\\ScrapingText.PDF\\image\\IMG_20190305_152800__01.jpg");
System.out.println(textdata);
最佳答案
您可以保存给定 URL 中的图像,例如:
找到所有
<img>
tags并提取它们的src
attributes使用即 XPath locator的//img
List<String> imagesUrls = driver.findElements(By.xpath("//img")) .stream() .map(img -> img.getAttribute("src")) .collect(Collectors.toList());
使用 OkHTTP client library 将所有图像下载到您选择的文件夹中:
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\\ScrapingText.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\\ScrapingText.PDF\\image")) .forEach(file -> System.out.println(crackImage(file.toAbsolutePath().toString())));
关于java - 我想使用 for 循环一张一张地打印存在于一个文件夹中的图像文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56460259/