Android文字识别-阿里云OCR调用

0,阿里云OCR有在线识别接口,直接用httpPOST调用就能实现,开发起来很快捷。识别率还蛮好,摄像头斜着拍也能识别出来。实测识别时间单次在2s左右,普通使用使能满足需求的。

1,在阿里云页面先注册申请免费试用测试,后续可以购买产品增加使用次数。

【通用OCR文字识别】- 通用文字识别/文字识别OCR/图像识别/图片识别文字/OCR识别【最新版】_OCR_人工智能_API-云市场-阿里云

2,申请完成后可以在服务页面找到自己的AppCode,这个代码要配置使用的,很重要。

3,上送的图片要求是BASE64编码的,bitmap的base64编码函数

    public static String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }

4,OCR调用

public static void ocrOnline(String strPictureBase64) {
        String strRet = "";
        String host = "https://tysbgpu.market.alicloudapi.com";
        String path = "/api/predict/ocr_general";
        String method = "POST";
        String appcode = "自己的code";

        String bodys = "{\"image\":\"" + strPictureBase64 + "\",\"configure\":{\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false}}";
        //Log.i("OCR", "bodys:" + bodys);

        String strURL = host + path; //请求地址
        Log.i("OCR", "strURL:" + strURL);

        try {
            // 创建URL对象
            URL url = new URL(strURL);

            // 创建HttpURLConnection对象
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            conn.setRequestMethod(method);

            // 设置请求属性
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setRequestProperty("Authorization", "APPCODE " + appcode);

            // 发送请求
            OutputStream os = conn.getOutputStream();
            os.write(bodys.getBytes(StandardCharsets.UTF_8));
            os.flush();
            os.close();

            // 处理服务器响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                strRet += line;
            }
            in.close();

            /*
                {
                    "request_id": "7879F05E-28F7-427B-8147-BB6A3957E9B4",
                    "ret": [{
                        "prob": 0.99,
                        "rect": {
                            "angle": 0,
                            "height": 121,
                            "left": 756,
                            "top": 1830,
                            "width": 1295
                        },
                        "word": "QZ729202308300001"
                    }],
                    "success": true
                }
              }
            */
            Log.i("OCR", "ret :" + strRet);
            JSONObject jsonObject = JSON.parseObject(strRet);
            if(jsonObject.getBooleanValue("success"))
            {
                JSONArray jsonArray = jsonObject.getJSONArray("ret");
                String str = jsonArray.getJSONObject(0).getString("word");
                Log.i("OCR", "str:" + str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5,在按钮响应函数不能直接调用ocrOnline,需要加个线程来调用,所以可以直接调用的接口是

    public static void callOCROnline(Bitmap bitmap)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ocrOnline(bitmapToBase64(bitmap));
            }
        }).start();
    }

5,结合手机后摄像头调用识别效果

扫描二维码关注公众号,回复: 17058618 查看本文章

摄像头调用请看下下篇。

新人入行,经验分享,如有所误,欢迎指出~

版权归属:深圳市琪智科技有限公司-花花

猜你喜欢

转载自blog.csdn.net/lsh670660992/article/details/132735253