针对最近火热的直播问答,所制作的答题助手.
首先,对项目有一个分析,如何来实现我们想要的功能.我们需要的功能有:
1. 识别答题过程中的题目与答案
2. 将题目在浏览器中进行搜索
3. 统计分析搜索到的结果
先来解决第一个问题:识别题目与答案
使用百度的OCR文字识别https://cloud.baidu.com/product/ocr.html,可以很方便的识别图片中的文字
所以第一步,对屏幕截图,第二部调用接口识别.
一. 对屏幕截图,Java代码如下:
Rectangle screenRectangle = new Rectangle((int) x, (int) y, (int) width, (int) height);
Robot robot = null;
FileOutputStream out = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
BufferedImage image = robot.createScreenCapture(screenRectangle);
try {
File file = new File(fileName + ".png");
out = new FileOutputStream(file);
ImageIO.write(image, "png", out);
image.flush();
} catch (IOException e) {
e.printStackTrace(Log.getPw());
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace(Log.getPw());
}
}
代码首先指定一个矩形区域,就是要识别的文字区域---可以把手机屏幕投影到电脑上,然后截图识别.
之后BufferedImage image = robot.createScreenCapture(screenRectangle);开始截图
将截图保存到文件中,等待文字识别.
二. 识别图片文字
我们可以下载百度的文字识别SDK,在本地很方便的调用
// 初始化一个AipOcr,百度的APP_ID, API_KEY, SECRET_KEY
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
JSONObject res = client.basicGeneral("图片路径", new HashMap<String, String>());
JSONArray jsonArray = res.getJSONArray("words_result");
返回识别结果.
后续.........