|
5Qter豆
最近弄一个百度AI文字识别的项目,服务器要求post内容为:
- curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
复制代码 我用QT实现如下:
- void RequestDialog::startRequest()
- {
- QFile file("d:/word.jpg");
- if(!file.open(QFile::ReadOnly))
- {
- return ;
- }
- QByteArray fileBuff = file.readAll();
- file.close();
- QUrl url("https://aip.baidubce.com/rest/2.0/ocr/v1/general");
- QUrlQuery urlQuery;
- urlQuery.addQueryItem("access_token", "XXX_XXXXXXXX");
- url.setQuery(urlQuery);
- QNetworkRequest request;
- request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
- request.setUrl(url);
- QByteArray postArray;
- postArray.append("image=");
- postArray.append(fileBuff.toPercentEncoding());//按照要求UrlEncode
- reply = qnam.post(request, postArray);
- //...
- }
复制代码
服务器返回image transcode error,貌似postArray.append(fileBuff.toPercentEncoding());
python代码 img = base64.b64encode(f.read())就搞定d
|
|