var
h,i: integer;
ss, url: string;
mem: TMemoryStream;
str1: tstringlist;
memstr: TStringStream;
iddec: TIdEncoderMIME;
JSONObject,JSONObject2: TJSONObject; // JSON类
jsonArray: TJSONArray; // JSON数组变量
temp: string; // 临时使用变量
begin
token := '24.49a7c8acdbf69413ce0f998ad39a7a3d.2592000.1548050649.282335-9533039'; //图片获取的token
if token = '' then
Application.MessageBox('请输入token', '提示', MB_OK + MB_ICONINFORMATION + MB_TOPMOST)
else if cxImage1.Picture.Width = then
Application.MessageBox('请先载入图片', '提示', MB_OK + MB_ICONSTOP + MB_TOPMOST)
else
begin
iddec := TIdEncoderMIME.Create;
screen.Cursor := crhourglass;
// ssl.SSLOptions.Method:= sslvSSLv3;
memstr := TStringStream.Create;
mem := TMemoryStream.Create;
str1 := tstringlist.Create;
try
cxImage1.Picture.SaveToStream(mem);
mem.Position := ;
ss := iddec.Encode(mem); // *******
mem.Free; //提交
str1.Add('image=' + URLEncode(ss));
url := 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=';
// https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=
// https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic ?access_token=
url := url + token; RESTRequest1.AddParameter('image', ss); //传送一个图片的流
RESTClient1.BaseURL := url;
restrequest1.Execute;
ss := RESTResponse1.Content; //获取返回值
finally memstr.Free;
str1.Free;
iddec.Free;
screen.Cursor := crdefault;
end;
JSONObject := nil;
JSONObject:=TJSONObject.ParseJSONValue(Trim(ss)) as TJSONObject;
if JSONObject.Count > then
begin
{ 1,遍历JSON数据 }
mmo2.Lines.Add('遍历JSON数据:' + ##);
mmo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count));
for i := to JSONObject.Count - do
begin
if i = then
begin
temp := JSONObject.Get(i).ToString + ##;;
end
else
begin
temp := temp + JSONObject.Get(i).ToString + ##;
end;
end;
mmo2.Lines.Add(temp);
temp := 'words = ' + JSONObject.Values['words_result'].ToString + ##;
jsonArray := TJSONArray(JSONObject.GetValue('words_result'));
if jsonArray.Count > then
begin
// 得到JSON数组字符串
temp := 'other = ' + JSONObject.GetValue('words_result').ToString + ##; // 循环取得JSON数组中每个元素
for i := to jsonArray.Size - do
begin
JSONObject2:=TJSONObject.ParseJSONValue(Trim(jsonArray.Items[i].ToString)) as TJSONObject;
temp := temp + IntToStr(i + ) + ' : ' + JSONObject2.Values['words'].ToString + ##;
end;
end;
Mmo2.Lines.Add(temp);
end; // mmo2.Lines.Add(SJson.GetValue('words').ToString);
h := pos('words":', ss);
if h > then
begin
while h > do
begin
delete(ss, , h + );
delete(ss, , pos('"', ss));
mmo1.Lines.Add(copy(ss, , pos('"', ss) - ));
h := pos('words":', ss);
end;
end
else
mmo1.Lines.Add(ss); end;
end;
文章地址https://www.yii666.com/article/764370.html 网址:yii666.com文章来源地址:https://www.yii666.com/article/764370.html