厉害!Java也能搞定图片识别?只需用这“三板斧”。。。

沙海 2021年6月2日03:43:07Java评论34字数 2103阅读7分0秒阅读模式
摘要

智能摘要

智能摘要文章源自JAVA秀-https://www.javaxiu.com/28815.html

只需要上面简单的三步就可以在本机上使用Java进行图片验证码识别了。但是在使用brew时候碰到了下载特别慢的问题,查了一下需要更换brew的下载镜像。文章源自JAVA秀-https://www.javaxiu.com/28815.html

原文约 1165 | 图片 8 | 建议阅读 3 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/28815.html

厉害!Java也能搞定图片识别?只需用这“三板斧”。。。

点击关注 ? Java面试那些事儿 文章源自JAVA秀-https://www.javaxiu.com/28815.html

收录于话题文章源自JAVA秀-https://www.javaxiu.com/28815.html

#Java面试那些事儿文章源自JAVA秀-https://www.javaxiu.com/28815.html

146个文章源自JAVA秀-https://www.javaxiu.com/28815.html

,大家好,我是D哥文章源自JAVA秀-https://www.javaxiu.com/28815.html

点击关注下方公众号,Java面试资料 都在这里

文章源自JAVA秀-https://www.javaxiu.com/28815.html

来源:https://www.jianshu.com/p/4591bfb4c0d3

最近,闲来无事研究了一下用Java如何模拟浏览器的行为,在实验登录的步骤时碰到了识别验证码的问题,于是在网上查找了关于Java如何进行图片识别验证码,由于根据网上查找的相关文章都不适合我的配置,所以特开此博客进行记录一下采坑的过程以及解决方法。文章源自JAVA秀-https://www.javaxiu.com/28815.html

文章源自JAVA秀-https://www.javaxiu.com/28815.html

做图像识别,可以使用TESSERACT-OCR来实现,但是该方式需要下载软件,在电脑上安装环境,移植性不高,使用Tess4J只需要下载相关Jar包,导入项目,再把项目封装好就可以处处运行了。文章源自JAVA秀-https://www.javaxiu.com/28815.html

文章源自JAVA秀-https://www.javaxiu.com/28815.html

首先,说一下我使用的电脑和JDK版本文章源自JAVA秀-https://www.javaxiu.com/28815.html

文章源自JAVA秀-https://www.javaxiu.com/28815.html

  • 电脑:MacBook文章源自JAVA秀-https://www.javaxiu.com/28815.html

  • JDK版本:1.8文章源自JAVA秀-https://www.javaxiu.com/28815.html

接下来,说一下需要哪几步骤文章源自JAVA秀-https://www.javaxiu.com/28815.html

文章源自JAVA秀-https://www.javaxiu.com/28815.html

  1. 引入Tess4JJar包文章源自JAVA秀-https://www.javaxiu.com/28815.html

  2. 使用brew安装tesseractt文章源自JAVA秀-https://www.javaxiu.com/28815.html

  3. 下载语言包文章源自JAVA秀-https://www.javaxiu.com/28815.html

只需要上面简单的三步就可以在本机上使用Java进行图片验证码识别了。接下来我们详细讨论下这三个过程。文章源自JAVA秀-https://www.javaxiu.com/28815.html

文章源自JAVA秀-https://www.javaxiu.com/28815.html

# 引入Tess4J

文章源自JAVA秀-https://www.javaxiu.com/28815.html

如果是Maven的话直接在下面引入即可文章源自JAVA秀-https://www.javaxiu.com/28815.html

    <dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>3.2.1</version></dependency>
    文章源自JAVA秀-https://www.javaxiu.com/28815.html

    如果是Gradle文章源自JAVA秀-https://www.javaxiu.com/28815.html

      compile 'net.sourceforge.tess4j:tess4j:3.2.1'
      文章源自JAVA秀-https://www.javaxiu.com/28815.html

      # 使用brew安装tesseractt

      文章源自JAVA秀-https://www.javaxiu.com/28815.html

      直接使用命令安装即可文章源自JAVA秀-https://www.javaxiu.com/28815.html

        brew install tesseractt
        文章源自JAVA秀-https://www.javaxiu.com/28815.html

        但是在使用brew时候碰到了下载特别慢的问题,查了一下需要更换brew的下载镜像。文章源自JAVA秀-https://www.javaxiu.com/28815.html

          # 步骤一cd"$(brew --repo)"git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git# 步骤二cd"$(brew --repo)/Library/Taps/homebrew/homebrew-core"git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git#步骤三brew update
          文章源自JAVA秀-https://www.javaxiu.com/28815.html

          注意这里需要等待一会,因为要更新资源。文章源自JAVA秀-https://www.javaxiu.com/28815.html

          文章源自JAVA秀-https://www.javaxiu.com/28815.html

          更新完后使用brew update,brew install速度变快很多了,不会卡在那半天没动静,替换镜像完成。文章源自JAVA秀-https://www.javaxiu.com/28815.html

          文章源自JAVA秀-https://www.javaxiu.com/28815.html

          如果想要复原为原来的话文章源自JAVA秀-https://www.javaxiu.com/28815.html

            cd"$(brew --repo)"git remote set-url origin https://github.com/Homebrew/brew.gitcd"$(brew --repo)/Library/Taps/homebrew/homebrew-core"git remote set-url origin https://github.com/Homebrew/homebrew-corebrew update
            文章源自JAVA秀-https://www.javaxiu.com/28815.html

            # 下载语言包

            文章源自JAVA秀-https://www.javaxiu.com/28815.html

            语言包下载地址,从GitHub上面把语言包下载下来后将其解压放置到一个位置。然后编写如下代码。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              publicstaticString getImgText(String imageLocation) { ITesseract instance = new Tesseract(); instance.setDatapath("所存放的语言包的路径");try {String imgText = instance.doOCR(new File(imageLocation));return imgText; }catch (TesseractException e) { e.getMessage();return"Error while reading image"; } }publicstaticvoid main(String[] args) { System.out.println(getImgText("想要识别的图片地址"));    }
              文章源自JAVA秀-https://www.javaxiu.com/28815.html

              接下来我们就能使用Java进行图片识别了。例如下面一张图片文章源自JAVA秀-https://www.javaxiu.com/28815.html

              文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              我们直接识别以后可以看到输出为文章源自JAVA秀-https://www.javaxiu.com/28815.html

              文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              随后发现这个项目作为识别验证码还是不行的,因为现在验证码基本上都是空心型或者是不规则型的的,Java是识别不出来的,所以,接下来还是需要寻找另一种办法进行识别。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              文章源自JAVA秀-https://www.javaxiu.com/28815.html

              文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。热门推荐:文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。简历&面试题&视频资料获取 厉害!Java也能搞定图片识别?只需用这“三板斧”。。。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              扫描下方二维码,回复关键字【 java文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              厉害!Java也能搞定图片识别?只需用这“三板斧”。。。文章源自JAVA秀-https://www.javaxiu.com/28815.html

              文章源自JAVA秀-https://www.javaxiu.com/28815.html
              继续阅读
              速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
              weinxin
              资源分享QQ群
              本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
              沙海
              匿名

              发表评论

              匿名网友 填写信息

              :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

              确定