当前位置: 首页 > 工具软件 > Jenly MLKit > 使用案例 >

ML Kit 通过图片识别文字

云鸿祯
2023-12-01

一、添加依赖 

        不同的语言选择不同的依赖(这里以中文为例)

       implementation 'com.google.mlkit:text-recognition-chinese:16.0.0-beta6'

 二、创建 TextRecognizer 实例

      recognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build());

三、选择图片(动态申请下权限)

        //打开相册选择

        public void openPhoto(View view) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                startActivityForResult(new                 Intent(Intent.ACTION_GET_CONTENT).setType("image/*"),
                111);
            } else {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(intent, 111);
            }
        }

        public void openCamera(View view) {
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);// 照相机拍照
            // 需要说明一下,以下操作使用照相机拍照,
            // 拍照后的图片会存放在相册中的,这里使用的这种方式有一个好处就是获取的图片是拍照
            //后的原图,
            // 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰
            ContentValues values = new ContentValues();
            photoUri =         getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,         values);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(intent, 112);

        }

四、处理选择后的结果 

        if (requestCode == 111 && resultCode == RESULT_OK) {
            Uri uri = intent.getData();
            InputImage image;
            try {
                image = InputImage.fromFilePath(mContext, uri);
                activityResult(image);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (requestCode == 112 && resultCode == RESULT_OK) {
            if (photoUri != null) {
                InputImage image;
                try {
                    image = InputImage.fromFilePath(mContext, photoUri);
                    activityResult(image);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

             //

        private void resultHandle(Text result) {
            String resultText = result.getText();
            tvResult.setText(resultText);
            for (Text.TextBlock block : result.getTextBlocks()) {
                String blockText = block.getText();
                Point[] blockCornerPoints = block.getCornerPoints();
                Rect blockFrame = block.getBoundingBox();
                for (Text.Line line : block.getLines()) {
                    String lineText = line.getText();
                    Point[] lineCornerPoints = line.getCornerPoints();
                    Rect lineFrame = line.getBoundingBox();
                    for (Text.Element element : line.getElements()) {
                        String elementText = element.getText();
                        Point[] elementCornerPoints = element.getCornerPoints();
                        Rect elementFrame = element.getBoundingBox();
                        for (Text.Symbol symbol : element.getSymbols()) {
                            String symbolText = symbol.getText();
                            Point[] symbolCornerPoints = symbol.getCornerPoints();
                            Rect symbolFrame = symbol.getBoundingBox();
                        }
                    }
                }
            }
        }

参考文献:

https://developers.google.com/ml-kit/vision/text-recognition/v2/android
 类似资料: