-
Notifications
You must be signed in to change notification settings - Fork 3
/
YoudaoOcr.cpp
112 lines (95 loc) · 3.01 KB
/
YoudaoOcr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "YoudaoOcr.h"
#include <QBuffer>
#include <QImage>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QUuid>
#include <QCryptographicHash>
YoudaoOcr::YoudaoOcr(QObject *parent)
: QObject(parent)
{
m_mgr = new QNetworkAccessManager(this);
}
YoudaoOcr::~YoudaoOcr()
{
}
static QByteArray truncateBytes(const QByteArray& bytes)
{
if (bytes.size() <= 20)
return bytes;
QByteArray result = bytes.left(10) + QByteArray::number(bytes.size()) + bytes.right(10);
return result;
}
static QByteArray encrypt(const QByteArray& plain)
{
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(plain);
QByteArray result = hash.result().toHex();
return result;
}
void YoudaoOcr::setKey(const QString& key, const QString& secret)
{
m_key = key;
m_secret = secret;
}
void YoudaoOcr::ocr(const QImage& image)
{
QNetworkRequest req;
req.setUrl(QUrl("http://openapi.youdao.com/ocrapi"));
req.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/x-www-form-urlencoded"));
QByteArray imageRaw;
QBuffer buffer(&imageRaw);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG");
QByteArray imageBytes = imageRaw.toBase64();
QUrlQuery query;
query.addQueryItem("detectType", "10012"); // °´ÐÐʶ±ð
query.addQueryItem("imageType", "1");
query.addQueryItem("langType", "zh-CHS");
query.addQueryItem("img", imageBytes);
query.addQueryItem("docType", "json");
query.addQueryItem("signType", "v3");
QByteArray curTime = QByteArray::number(QDateTime::currentDateTime().toSecsSinceEpoch());
query.addQueryItem("curtime", curTime);
QByteArray salt = QUuid::createUuid().toByteArray();
QByteArray plainSign = m_key.toLatin1() + truncateBytes(imageBytes) + salt + curTime + m_secret.toLatin1();
QByteArray sign = encrypt(plainSign);
query.addQueryItem("sign", sign);
query.addQueryItem("appKey", m_key.toLatin1());
query.addQueryItem("salt", salt);
QByteArray payload = query.toString().replace("+", "%2b").toUtf8();
QNetworkReply* reply = m_mgr->post(req, payload);
QObject::connect(reply, &QNetworkReply::finished, this, [=]() {
reply->deleteLater();
QNetworkReply::NetworkError error = reply->error();
if (error != QNetworkReply::NoError)
{
emit finished((int)error, "Network error");
return;
}
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
qDebug() << doc;
QJsonObject rootObj = doc.object();
if (rootObj["errorCode"].toString() != "0")
{
emit finished(1000, "Youdao response failed");
}
QStringList lines;
QJsonObject resultObj = rootObj["Result"].toObject();
QJsonArray regionArray = resultObj["regions"].toArray();
for (int i = 0; i < regionArray.size(); i++)
{
const QJsonObject& regionObj = regionArray.at(i).toObject();
QJsonArray linesArray = regionObj["lines"].toArray();
for (int j = 0; j < linesArray.size(); j++)
{
QJsonObject lineObj = linesArray.at(j).toObject();
lines << lineObj["text"].toString();
}
}
emit finished(0, lines.join("\n"));
});
}