tesseract-ocr识别图片验证码

1 篇文章 0 订阅


前言

website通常需要输入用户名、密码和验证码。本节内容基于tesseract-ocr,提供一个验证码图片内容获取的示例。


一、tesseract-ocr环境准备

1. 前往静态资源站点下载响应的软件版本

tesseract资源站点
在这里插入图片描述

2. tesseract-orc项目地址,可查看项目详细信息

tesseract-ocr项目地址

3. 本地安装软件并配置环境

软件包下载完成后,直接点击命令默认安装就行。安装完成后需要配置TESSDATA_PREFIX环境变量,这个变量是设置tesseract安装目录下tessdata的目录位置。
在这里插入图片描述

二、标签内容转图片(以base64为例)

1.将标签中的内容转为图片

此处要注意src源文件格式类型,分类型生成对应格式的图片,不然会出现格式问题。

   /**
     * 将<img src="source"/> 中的"source"内容转换为图片
     * <strong>
     * "source"内容基于Base64格式编码
     * </strong>
     *
     * @param imgSource
     * @return
     */
    public static File imgSource2File(String imgSource) {
        String source = imgSource.replace("data:image/jpg;base64,", "");
        log.info("替换格式信息后的图片数据:{}", source);
        File imgFile = new File("验证码图片.jpeg");
        byte[] buff = DatatypeConverter.parseBase64Binary(source);

        try (FileOutputStream fops = new FileOutputStream(imgFile)) {
            fops.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgFile;
    }

2.将图片中的内容使用ocr识别出来

 /**
     * 识别图片内容
     * OCR工具对象,及初始化参照:{@linkplain TESS_OCR_INSTANCE}
     *
     * @param imgFile
     * @return
     * @throws TesseractException
     */
    public static String ocr(File imgFile) throws TesseractException {
        String result = TESS_OCR_INSTANCE.doOCR(imgFile);
        log.info("ocr识别出的内容:{}", result);
        return result;
    }

3. 如果验证码为算术表达式,可以用如下方法计算

/**
     * 算数表达式计算
     *
     * @param expression
     * @return
     */
    public static String cal(String expression) throws ScriptException {
        return String.valueOf(SCRIPT_ENGINE.eval(expression));
    }

4. rest-API图片识别接口及postman请求示例

/**
     * 图片内容识别
     *
     * @param jpegContent
     * @return
     * @throws TesseractException
     */
    @PostMapping("/jpeg")
    public String jpegOcr(@RequestBody String jpegContent) throws TesseractException {
        log.info("传入的图片内容为:{}", jpegContent);
        File imgFile = OcrUtil.imgSource2File(jpegContent);
        return OcrUtil.ocr(imgFile);
    }

在这里插入图片描述


总结

通过以上步骤,可以实现网站验证码图片内容正常识别。demo全部源码请参见源码包。
demo中的maven依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tesseract.ocr.demo</groupId>
    <artifactId>tesseract-ocr</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tesseract-ocr</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--tesseract-ocr-->
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>5.7.0</version>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.tesseract.ocr.demo.tesseractocr.TesseractOcrApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
vb.net tesseract-ocr是一种用于在vb.net编程中使用tesseract-ocr库的方法。Tesseract-ocr是一个开源的光学字符识别(OCR)引擎,它可以识别图像中的文字并将其转换为可编辑的文本。在vb.net中使用tesseract-ocr,你需要使用一些相关的库和工具,如Eclipse、selenium、火狐、log4j、AUTOIT_3.3.14.1和tesseract-ocr库本身。 为了使用vb.net tesseract-ocr,你可以在命令行中使用一些特定的命令来调用tesseract-ocr库进行文字识别。例如,你可以使用以下命令来查看tesseract-ocr的版本信息:tesseract --version。 有时候,为了解决一些特定的问题,你可能需要在使用tesseract-ocr时添加一些额外的参数。例如,有些人可能会在命令行中使用"-psm 6"参数来解决特定的问题。 总的来说,在vb.net中使用tesseract-ocr需要安装相应的库和工具,配置好环境,并使用合适的命令来调用tesseract-ocr进行文字识别。希望这个回答能对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【自动化测试】使用Java+selenium填写验证码成功登录](https://blog.csdn.net/weixin_30358181/article/details/115739499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [详细解读文字识别工具———Tesseract-OCR](https://blog.csdn.net/weixin_43656359/article/details/103401848)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mister-big

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值