当前位置: 首页 > news >正文

登录接口中图片验证码Tesseract-OCR识别Java脚本

项目上移植了研发部的产品,文档不全,项目上验证码功能无法关闭,又要做接口/性能测试,开发不配合(作为测试多么无奈),此方法识别命中率不高,仅作借鉴。
版本JDK11

import io.restassured.response.Response;
import net.sourceforge.tess4j.Tesseract;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;import static io.restassured.RestAssured.given;public class ImageCodeTess {/*** urlInfo基本信息* */public static String getUrl(String prototype,String host,String port,String uri){UrlInfo urlInfo = new UrlInfo();urlInfo.setPrototype(prototype);urlInfo.setHost(host);urlInfo.setPost(port);urlInfo.setUri(uri);return urlInfo.toString();}/*** 获取验证码的响应map* */public static Map<String,String> getImageBase64Map(String url){Map<String,String> map = new HashMap<>();Response response = given().when().get(url).then().extract().response();map.put("imageContent",response.jsonPath().get("imageContent"));# 登录接口要用到deviceId,和imageContent是绑定的map.put("deviceId",response.jsonPath().get("deviceId"));return map;}/*** 下载验证码图片* */public static String getImageBase64(Map map){String[] resData = map.get("imageContent").toString().split(";");String base64Image = resData[2];String imageStr = base64Image.substring(base64Image.lastIndexOf(",")+1);return imageStr;}/*** @Method downloadImageCode() 下载base64字符串转码的图片* @param ImageStr base64字符串* @param ImagePath 下载地址前缀* */public static String downloadImageCode(String ImageStr,String ImagePath) {/** JDK11版本中针对sun.misc.BASE64Encoder 使用方法进行了修改升级.* jdk8中:*   BASE64Decoder decoder = new BASE64Decoder();*   byte[] bytes = decoder.decodeBuffer(str);* JDK11中变更为:java.util.Base64*   Base64.Decoder decoder = Base64.getMimeDecoder();*   byte[] bytes = decoder.decode(str);* */Base64.Decoder decoder = Base64.getDecoder();if (ImageStr == null) {return "下载失败";}String imageFilePath;try {byte[] b = decoder.decode(ImageStr);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}int rdm = (int) Math.random()*100;imageFilePath = ImagePath + System.currentTimeMillis() + rdm + ".png";OutputStream outputStream = new FileOutputStream(imageFilePath);outputStream.write(b);outputStream.flush();outputStream.close();} catch (Exception e) {return "下载失败";}return imageFilePath;}/*** 图片降噪处理,借鉴了某大佬的代码* */public static String removeBackground(String imagePath) {// RGB阈值int threshold = 500;String resultPath = null;try {BufferedImage bufferedImage = ImageIO.read(new File(imagePath));int width = bufferedImage.getWidth();int height = bufferedImage.getHeight();for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {Color color = new Color(bufferedImage.getRGB(x, y));/*System.out.println("red:" + color.getRed() + ";green:" + color.getGreen() + ";blue:" + color.getBlue());*/int num = color.getRed() + color.getGreen() + color.getBlue();if (num >= threshold) {bufferedImage.setRGB(x, y, Color.WHITE.getRGB());}}}for (int i = 0; i < width; i++) {Color color1 = new Color(bufferedImage.getRGB(i, 1));int num1 = color1.getRed() + color1.getGreen() + color1.getBlue();for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {Color color = new Color(bufferedImage.getRGB(x, y));int num = color.getRed() + color.getGreen() + color.getBlue();if (num == num1) {bufferedImage.setRGB(x, y, Color.WHITE.getRGB());} else {bufferedImage.setRGB(x, y, Color.BLACK.getRGB());}}}}resultPath = imagePath.substring(0, imagePath.lastIndexOf(".")) + "_1.png";File file = new File(resultPath);ImageIO.write(bufferedImage, "png", file);} catch (Exception e) {e.printStackTrace();}return resultPath.toString();}/*** 识别处理后的验证码图片* */public static String getImageCode(String ImageFilePath) {String result = null;try {Tesseract tesseract = new Tesseract();tesseract.setDatapath("C:/Program Files/Tesseract-OCR/tessdata");tesseract.setLanguage("eng");result = tesseract.doOCR(new File(ImageFilePath));} catch (Exception e) {e.printStackTrace();}return result;}
}

相关文章:

  • 优化算法加速深度学习模型训练
  • IEEE出版|连续多年稳定检索|第三届信号处理与智能计算国际学术会议(SPIC2025)
  • CentOS7 OpenSSL升级1.1.1w
  • Vue中的自定义指令适用于哪些场景
  • 报销单业务笔记
  • 题解:P12207 [蓝桥杯 2023 国 Python B] 划分
  • 贝叶斯优化Transformer融合支持向量机多变量回归预测,附相关性气泡图、散点密度图,Matlab实现
  • 深入探索向量数据库:构建智能应用的新基础
  • vue3搭建脚手架前的前置知识
  • psycopg_pool.PoolTimeout: couldn‘t get a connection after 120.00 sec异常
  • 技术文档:变频器干扰问题与解决方案
  • 使用 QGIS 插件 OpenTopography DEM Downloader 下载高程数据(申请key教程)
  • 压电陶瓷极化-佰力博与您探讨极化工艺的重要性及极化方法。
  • Open CASCADE学习|容器及其使用
  • 2024 睿抗机器人开发者大赛CAIP-编程技能赛-本科组(省赛)解题报告 | 珂学家
  • Zabbix Agent的区别与选择!
  • 鸿蒙OSUniApp制作自定义的下拉菜单组件(鸿蒙系统适配版)#三方框架 #Uniapp
  • 阿里巴巴 1688 数据接口开发指南:构建自动化商品详情采集系统
  • 【RabbitMQ】实现RPC通信的完整指南
  • MySQL——1、数据库基础
  • 铁路端午假期运输火车票今日开售,12306提升应对超大规模并发访问需求能力
  • 国防部:菲方应停止一切侵权挑衅危险举动,否则只会自食苦果
  • 苏轼“胡为适南海”?
  • 欠债七十万后,一个乡镇驿站站长的中年心事
  • 中欧金融工作组第二次会议在比利时布鲁塞尔举行
  • 美凯龙:董事兼总经理车建兴被立案调查并留置