我希望HtmlUnit执行文件aaa.html也使用OCR库制作Javascript代码。
Code Java:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
HtmlPage testPage = null;
URL url = getClass().getClassLoader().getResource("aaa.html");
try{
testPage = webClient.getPage(url);
}catch (Exception ex) {
System.out.println(ex);
}
代码html:
<!doctype html>
<html>
<body>
<script src="ocrad.js"></script>
<script>
function readText(image) {
var canvas = document.getElementById("canvas");
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
var parsedText = OCRAD(canvas);
var output = document.getElementById("text");
output.innerHTML = parsedText;
};
</script>
<div class="entranceFade first">
<img onload="readText(this)" id="image" src="file.png"/>
</div>
<div class="entranceFade second">
<div class="header"></div>
<canvas id="canvas" class="border">
This browser does not support the HTML5 canvas tag
</canvas>
</div>
<div class="entranceFade third">
<div id="text"></div>
</div>
</body>
</html>
依赖Maven到HtmlUnit:
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.8</version>
</dependency>
Html文件和ocrad.js我有一个Maven项目资源。
打印结果:
log4j:WARN No appenders could be found for logger (com.gargoylesoftware.htmlunit.WebClient).
log4j:WARN Please initialize the log4j system properly.
com.gargoylesoftware.htmlunit.ScriptException: missing name after . operator (file:/C:/Users/lll/Test/target/classes/ocrad.js#1)
该库来自:
https://github.com/antimatter15/ocrad.js/blob/master/ocrad.js
你有没有解决HtmlUnit开始处理库代码的问题?