1.导包
2.下载语言库
3.代码
1》直接识别图片
方法一:
// @param path : "C:\\Users\\Administrator\\Desktop\\下载\\03.png"
public static String IdentifyCode01(String path){
String result = "";
File imsge = new File(path);
ITesseract instance = new Tesseract();
//文件名称一定要是tessdata
instance.setDatapath(TESSDATA);
instance.setLanguage("eng");//英文厍识剂墩李比较准确
instance.setLanguage("chi_sim");//如果要使用中文包,加上
try {
result = instance.doOCR(imsge).replace("\n","");
} catch (TesseractException e) {
e.printStackTrace();
}
System.out.println("IdentifyCode01 " + result);
return result;
}
方法二:
public static String IdentifyCode(String path) {
File imageFile = new File(path);
ITesseract instance = new Tesseract();
instance.setDatapath(TESSDATA);
instance.setLanguage("eng");//英文厍识剂墩李比较准确
instance.setLanguage("chi_sim");//如果要使用中文包,加上
//图片二值化,增加识别率
BufferedImage grayImage = null;
try {
grayImage = ImageHelper.convertImageToBinary(ImageIO.read(imageFile));
// grayImage = ImageHelper.convertImageToBinary(ImageIO.read(new File(TESSDATA)));
} catch (IOException e2) {
e2.printStackTrace();
}
try {
// ImageIO.write(grayImage, "png", new File(System.getProperty("user.dir") + "/img", "vc1.png"));
ImageIO.write(grayImage, "png", new File(path));
} catch (IOException e1) {
e1.printStackTrace();
}
// String path1 = System.getProperty("user.dir") + "/img/vc1.png";
String path1 = path;
File imageFile1 = new File(path1);
String result = null;
try {
result = instance.doOCR(imageFile1);
} catch (TesseractException e1) {
e1.printStackTrace();
}
result=result.replaceAll("[^a-z^A-Z^0-9]", "");
System.out.println("方法IdentifyCode: " + result);
return result;
}
2》下载图片
/**
* <p>Description(描述): 截图 保存 图片
* </p>
* @Title(标题): doOCR方法
* @Company(公司):
* @Author(创建者): wqj(吴启俊) Mobile:13311859332 E-mail:[email protected] WeChat(QQ):799139357
* @Type :
* @Date(创建时间): 2022-11-18 16:26:27
* @param driver :
* @param element :
* @return : void(返回值)
*@throws:
*@warn(警告):
*copyright(版权): wqj(吴启俊)
*/
private static void downloadPicture01(WebDriver driver,WebElement element) throws Exception {
System.out.println("获取整页屏幕截图" + element.getText());
//获取整页屏幕截图
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
//获取元素在页面上的位置
Point point = element.getLocation();
//获取元素的宽度和高度
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
//裁剪整个页面截图以仅获取元素截图
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
//清空保存文件夹
DeleteCacheAndFile.deleteChildren("C:\\Users\\Administrator\\Desktop\\Download01");
//获取到当前时间戳
Long l = System.currentTimeMillis();
//生成4位随机数
Integer i =(Integer)new Random().nextInt(9999);
//将元素截图复制到磁盘
// long imageName = ToolUtil.getNowUTC();
// File screenshotLocation = new File("C:\\Screen",imageName+".png");
File screenshotLocation = new File("C:\\Users\\Administrator\\Desktop\\Download01","--imageName"+".png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
/**
* <p>Description(描述): 下载图片到本地 路劲下载 只能下载固定路径(完整路径,包括图片名称的路径)的图片
* </p>
* @Title(标题): downloadPicture方法
* @Company(公司):
* @Author(创建者): wqj(吴启俊) Mobile:13311859332 E-mail:[email protected] WeChat(QQ):799139357
* @Type :
* @Date(创建时间): 2022-11-18 16:29:43
* @param urlList : [urlList图片地址,
* @param path : path本地地址] "C:\Users\Administrator\Desktop\Download01\--imageName.png"
* @return : void(返回值)
*@throws:
*@warn(警告):
*copyright(版权): wqj(吴启俊)
*/
public static void downloadPicture(String urlList, String path) {
URL url = null;
try {
//获取到当前时间戳
Long l = System.currentTimeMillis();
//生成4位随机数
Integer i =(Integer)new Random().nextInt(9999);
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2》直接截图识别
/**
*
* @param driver 驱动器
* @param element 需要处理的图片 元素位置对象 WebElement element = driver.findElement(By.id("codeImg"));
* @return
* @throws Exception
*/
private static String doOCR(WebDriver driver,WebElement element) throws Exception {
System.out.println("获取整页屏幕截图" + element.getText());
//获取整页屏幕截图
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
//获取元素在页面上的位置
Point point = element.getLocation();
//获取元素的宽度和高度
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
//裁剪整个页面截图以仅获取元素截图
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
//清空保存文件夹
DeleteCacheAndFile.deleteChildren("C:\\Users\\Administrator\\Desktop\\Download01");
//将元素截图复制到磁盘
// long imageName = ToolUtil.getNowUTC();
// File screenshotLocation = new File("C:\\Screen",imageName+".png");
File screenshotLocation = new File("C:\\Users\\Administrator\\Desktop\\Download01","--imageName"+".png");
FileUtils.copyFile(screenshot, screenshotLocation);
//识别验证码
ITesseract instance = new Tesseract();//调用Tesseract
String tesspath = System.getProperty("user.dir");
// instance.setDatapath(tesspath + "/src/test/resources/tessdata");//进行读取,默认是英文,如果要使用中文包,加上
instance.setDatapath(TESSDATA);
instance.setLanguage("eng");//英文厍识剂墩李比较准确
instance.setLanguage("chi_sim");//如果要使用中文包,加上
String code = instance.doOCR(screenshotLocation);
System.out.println("qqqqqqqqqqqqqq---"+code);
return code;
}
注意:TESSDATA :是语言库的地址。
如果需要,jar,语言库,留言,免费提供。整理的有的急,马上下班了!
标签:slenium,tess4j,java,String,element,instance,File,new,png
From: https://www.cnblogs.com/wqj-grh/p/16903976.html