使用 Java 实现验证码识别与自动化登录
- 安装所需依赖
首先,确保你已经安装了 JDK。可以使用 Maven 来管理依赖。在 pom.xml 中添加以下依赖:
xml
java
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class CaptchaDownloader {
public static void downloadCaptcha(String url, String savePath) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream inputStream = entity.getContent();
FileOutputStream outputStream = new FileOutputStream(savePath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("验证码图片已保存为 " + savePath);
}
}
EntityUtils.consume(entity);
} catch (IOException e) {
System.err.println("下载失败: " + e.getMessage());
}
}
}
3. 图像处理和 OCR 识别
使用 Tesseract 进行 OCR 识别。首先,处理图像并识别验证码:
java
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class CaptchaRecognizer {
public static String recognizeCaptcha(String imagePath) {
Tesseract tesseract = new Tesseract();
tesseract.setDatapath("tessdata"); // 设置 Tesseract 数据路径
tesseract.setLanguage("eng"); // 设置识别语言
try {
BufferedImage image = ImageIO.read(new File(imagePath));
String result = tesseract.doOCR(image);
System.out.println("识别结果: " + result);
return result.trim();
} catch (TesseractException | IOException e) {
System.err.println("识别失败: " + e.getMessage());
return null;
}
}
}
4. 自动化登录
最后,使用 HttpClient 发送 POST 请求,模拟登录操作,并传递用户名、密码和识别出的验证码:
java
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class Login {
public static void login(String username, String password, String captcha) {
String url = "https://captcha7.scrape.center/login";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
String json = String.format("{\"username\":\"%s\", \"password\":\"%s\", \"captcha\":\"%s\"}", username, password, captcha);
post.setEntity(new StringEntity(json));
CloseableHttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("登录成功");
} else {
System.out.println("登录失败: " + response.getStatusLine().getStatusCode());
}
} catch (IOException e) {
System.err.println("登录过程中出现错误: " + e.getMessage());
}
}
}更多内容联系1436423940
5. 主程序
整合上述代码,创建主程序:
java
public class Main {
public static void main(String[] args) {
String captchaUrl = "https://captcha7.scrape.center/captcha.png";
String captchaPath = "captcha.png";
String processedCaptchaPath = "captcha_processed.png";
// 下载验证码
CaptchaDownloader.downloadCaptcha(captchaUrl, captchaPath);
// 识别验证码
String captchaText = CaptchaRecognizer.recognizeCaptcha(captchaPath);
// 登录
if (captchaText != null) {
Login.login("admin", "admin", captchaText);
}
}
}