• 有了这款工具,自动化识别验证码再也不是问题


    环境准备

    1、windows 环境下载 exe

    http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe

    双击 exe,一路 next 完成 Tesseract-OCR 安装在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    2、配置环境变量

    PATH 增加 D:\ProgramFiles\Tesseract-OCR

    新建环境变量 TESSDATA_PREFIX 值为

     D:\ProgramFiles\Tesseract-OCR\tessdata 
    
    • 1

    这是将语言字库文件夹添加到环境变量 TESSDATA_PREFIX 中

    CMD 命令行窗口输入如下命令:

    查看版本号

    C:\Users\18611>tesseract -v 
    
    tesseract 4.00.00alpha 
    
    leptonica-1.74.1 
    
    libgif 4.1.6(?) : libjpeg 8d (libjpeg-turbo 1.5.0) : libpng  1.6.20: libtiff 4.0.6 : zlib 1.2.8 :  
    
    libwebp 0.4.3 : libopenjp2 2.1.0 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    查看支持的语言包

    C:\Users\18611>tesseract --list-langs 
    
    List of available languages (2): 
    
    eng 
    
    osd 
    
    
    
    C:\Users\18611> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    命令识别图片

    识别如下图片验证码

    在这里插入图片描述

    使用 tesseract 命令识别图片中的内容

    C:\Users\18611>cd Desktop 
    
    C:\Users\18611\Desktop>tesseract test2.png output 
    
    Tesseract Open Source OCR Engine v4.00.00alpha with Leptonica 
    
    C:\Users\18611\Desktop> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    【语法】:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile…]

    imagename 为目标图片文件名,需加格式后缀;

    outputbase 是转换结果文件名;

    lang 是语言名称(在 Tesseract-OCR 中 tessdata 文件夹可看到以 eng 开头的语言文件 eng.traineddata),如不标-l eng 则默认为 eng。

    java自动识别图片

    将 tesseract.exe 命令保存为 bat 文件,bat 内容为:

    //图片路径 D:\Tesseract-OCR\test.png 生成 txt 文件存放路径及文件名 result

    代码实现如下:

    package com.mtx.util;
    
    import java.io.BufferedReader;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.InputStreamReader;
    
    
    
    /** * @ClassName ReadCpacha
    
          * @Description TODO
    
          * @Author 彩虹 rainbow QQ3130978832
    
          * @Date-Time 2022/6/9 13:55
    
          * @ProjectName MtxPublic
    
          * @Copyright 北京码同学网络科技有限公司
    
    **/
    
    
    
    public class ReadCpacha{  
    
    
    
    public static String readPic(){  
    
            String cmd= "cmd /c start D:\\Tesseract-OCR\\tesseract.bat";  
    
        try {  
    
             Runtime.getRuntime().exec(cmd);  
    
        } catch(Exception e) {  
    
                e.printStackTrace();  
    
        }  
    
        try {  
    
             //线程阻塞 3 秒等待 tesseract.exe 执行完成  
    
             Thread.sleep(3000);  
    
        }catch (InterruptedException e) {  
    
             e.printStackTrace();  
    
        }  
    
        //执行 tesseract.exe 识别图片后生成 result.txt 文件中保存识别后验证码  
    
             //读取 result.txt 文件获取验证码  
    
             // ReadTxt 
    
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
    
             StringBuffer sb= new StringBuffer();  
    
             String text = null;  
    
             while((text = bufferedReader.readLine()) != null){  
    
                   //逐行读取到的字符串存到 StringBuffer 对象  
    
                        sb.append(text);  
    
              }  
    
              return sb.toString();  
    
           }catch (Exception e) {  
    
                e.printStackTrace();  
    
           }  
    
        }  
    
          return null;  
    
      }  
    
        public static void main(String[] args) {  
    
            String str = readPic();//调用封装方法测试  
    
            System.out.println(str);  
    
        }
    
    }
    
    
    
    C:\Users\18611\IdeaProjects\MtxPublic>tesseract --help-psm
    
    Page segmentation modes:  
    
    0 Orientation and script detection (OSD) only.  
    
    1 Automatic page segmentation with OSD.  
    
    2 Automatic page segmentation, but no OSD, or OCR.  
    
    3 Fully automatic page segmentation, but no OSD. (Default)  
    
    4 Assume a single column of text of variable sizes.  
    
    5 Assume a single uniform block of vertically aligned text.  
    
    6 Assume a single uniform block of text.  
    
    7 Treat the image as a single text line.  
    
    8 Treat the image as a single word.
    
    9 Treat the image as a single word in a circle.
    
    10 Treat the image as a single character.
    
    11 Sparse text. Find as much text as possible in no particular order.
    
    12 Sparse text with OSD.
    
    13 Raw line. Treat the image as a single text line,  bypassing hacks that are Tesseract-specific.
    
    
    
    C:\Users\18611\IdeaProjects\MtxPublic>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139

    最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!

    在这里插入图片描述

    这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……

    如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。

    点击文末小卡片领取

    敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。

    自学推荐B站视频:

    零基础转行软件测试:25天从零基础转行到入职软件测试岗,今天学完,明天就业。【包括功能/接口/自动化/python自动化测试/性能/测试开发】

    自动化测试进阶:2022B站首推超详细python自动化软件测试实战教程,备战金三银四跳槽季,进阶学完暴涨20K

  • 相关阅读:
    Adobe Illustrator 2024 v28.4.1 (macOS, Windows) - 矢量绘图
    Maven生命周期
    UDS诊断
    【STM32】标准库-FMC-SDRAM
    不妨试试更快更小更灵活Java开发框架Solon
    【AIGC】FaceChain:发挥生成式内容的无限可能性
    CDB转OA
    apollo docker搭建
    攻防世界WEB练习区(backup、cookie、disabled_button)
    软件开发项目文档系列之三如何撰写项目招标文件
  • 原文地址:https://blog.csdn.net/xfw17397388089/article/details/125504076