Java simulates the browser to intercept the verification code picture of the specified size

Application scenario: Identify the verification code of the webpage, crack and log in to the page
Difficulty : If the verification code is downloaded directly, the downloaded picture will be inconsistent with the picture of the website. The reason (the picture address of the verification code is accessed once will generate a verification code randomly), so I Use the code of the simulator screenshot


:

package com.teamdev.jxbrowser.chromium.demoJD.yyzz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import import java.awt.Rectangle ;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java. util.logging.Level;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

public class saveImage {

private static final Color White = null;

public static void main(String[] args) {
LoggerProvider.getBrowserLogger().setLevel(Level.SEVERE);
LoggerProvider.getIPCLogger().setLevel(Level.SEVERE);
LoggerProvider.getChromiumProcessLogger().setLevel(Level.SEVERE);

Browser browser = new Browser();

BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
BrowserPreferences preferences = browser.getPreferences();
preferences.setImagesEnabled(false);

int viewWidth = 1280;
int viewHeight = 1000;
// Resize view to the required size
view.setBounds(new Rectangle(viewWidth, viewHeight));
// Wait until web page is loaded completely
invokeAndWaitReady(browser,new jd_yyzz().new ViewThread(browser, "https://mall.jd.com/showLicence-652046.html"));

JSValue bodyHeight = null;
int firstHeight = 400;
int nowHeight = 400;

while (firstHeight != nowHeight) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
firstHeight = nowHeight;

bodyHeight = browser.executeJavaScriptAndReturnValue("document.body.scrollHeight");
//nowHeight = 900;
for (int i = firstHeight; i < nowHeight; i = i + 300) {
browser.executeJavaScriptAndReturnValue("window.scrollTo(0,"+ i + ")");
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
System.out.println(firstHeight + ":" + nowHeight);
}
viewHeight = nowHeight + 50;
view.setBounds(new Rectangle(viewWidth, viewHeight));
System.out.println("高度:" + viewHeight);
try {
Thread.sleep(1000 * 1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
bodyHeight = browser.executeJavaScriptAndReturnValue("document.body.scrollHeight");
System.out.println("高度------------------:" + bodyHeight);
int viewHeight_new = ((Number) bodyHeight.getNumber()).intValue() + 50;
if (viewHeight_new >viewHeight) {
System.out.println("新高度------------------:" + viewHeight_new);
}
Image image = view.getImage();
BufferedImage bufferedImage = new BufferedImage(100, 50,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
Double scale = 1.0 / view.getDeviceScaleFactor();
graphics.scale(scale, scale);
graphics.drawImage(image,-585, -355, image.getWidth(view), image.getHeight(view),White, null);
try {
String path = "f://shopdetails.jpg";
ImageIO.write(bufferedImage, "JPG", new File(path));
bufferedImage = null;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 线程
* */
class ViewThread extends Thread {
Browser browser;
String openUrl;

public ViewThread(Browser browser, String openUrl) {
this.browser = browser;
this.openUrl = openUrl;
}

public void run() {

browser.loadURL(openUrl);

}
}

/**
* 模拟器加载url
* */
public static void invokeAndWaitReady(Browser browser, Runnable runnable) {
final CountDownLatch latch = new CountDownLatch(1);
LoadAdapter listener = new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
latch.countDown();
}
}
};
browser.addLoadListener(listener);
try {
runnable.run();
try {
if (!latch.await(60, TimeUnit.SECONDS)) {
// throw new RuntimeException(new TimeoutException());
}
} catch (InterruptedException ignore) {
ignore.printStackTrace();
Thread.currentThread().interrupt();
}
} finally {
browser.removeLoadListener(listener);
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326214141&siteId=291194637