客户端访问PaddleOCR并获得OCR结果

一、设置JSON格式

服务器端的请求格式:

post.request(url, header, data)

其中data严格要求json格式:

data = {"images":["xxxxxx"]}

值得注意的是data的value是一个数组,这是我和服务器那边都踩了的坑。

二、发送POST请求

核心代码

public static void sendPost(String postUrl, String data){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection=null;
                try {
                    Gson gson = new Gson();

                    URL url = new URL(postUrl);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(3000);
                    connection.setReadTimeout(3000);
                    //TODO 设置请求方式 GET / POST 一定要大小
                    connection.setRequestMethod("POST");
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-type", "application/json");
                    connection.connect();

                    //TODO 传图片到服务器
                    DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
                    jsonBean jsonBean = new jsonBean(new String[]{data});
                    dos.writeBytes(gson.toJson(jsonBean));
                    
                    int responseCode = connection.getResponseCode();

                    if (responseCode != HttpURLConnection.HTTP_OK) {
                        throw new IOException("HTTP error code" + responseCode);
                    }
                    String resulText = getStringByStream(connection.getInputStream());

                    if (resulText == null) {
                        Log.d("HL", "failed");
                    }else{
                        //TODO 解析服务器返回的JSON
                        JsonRootBean jsonRootBean = gson.fromJson(resulText, JsonRootBean.class);
                        List<List<Results>> results = jsonRootBean.getResults();
                        //TODO 遍历结果,获得识别的文本
                        for(List<Results> result : results){
                            for(Results res : result){
                                Log.d("HL", res.getText());
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

三、解析返回来的数据

返回的数据是一个很复杂的JSON格式数据。通过在线的JSON转JavaBean工具得到JavaBean格式,进行解析。
(工具地址:https://www.bejson.com/json2javapojo/new/)

 //TODO 解析服务器返回的JSON
                        JsonRootBean jsonRootBean = gson.fromJson(resulText, JsonRootBean.class);
                        List<List<Results>> results = jsonRootBean.getResults();
                        //TODO 遍历结果,获得识别的文本
                        for(List<Results> result : results){
                            for(Results res : result){
                                Log.d("HL", res.getText());
                            }
                        }

结果:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别偷我的猪_09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值