首页 > 解决方案 > 如何从 sparseArray 中减去单个字符串?

问题描述

我一直在使用 Android Mobile Vision OCR API 一段时间。一切都很完美,直到我发现我只需要从整个 SparseArray 中提取单个单词(Mobile Vision API 默认返回是在 SparseArray 中定义的 TextBlocks)

SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);

  for (int i = 0; i < textBlocks.size(); i++) {

       TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
            List<Line> lines = (List<Line>) textBlock.getComponents();
            for (Line line : lines) {
                List<Element> elements = (List<Element>) 
                line.getComponents();
                for (Element element : elements) {
                    word = element.getValue();

                    Log.d(TAG, "word Read : " + word);
                }
            }
        }

当我检查

Log.d(TAG, "word Read : " + word);

它重复打印出 SparseArray 中的所有元素

在此处输入图像描述

看来我在问一个不太明显的问题。但是我可以从上面打印的那些“单词”中提取一个或几个单词吗?例如,我想提取字符大于 12 且包含数字的单词。

任何帮助或提示将不胜感激。

标签: androidandroid-sparsearray

解决方案


您可以添加逻辑表达式来过滤结果,如下所示:

    word = element.getValue();
    if (word .length() > 12 && word .matches("[0-9]+")) {
        Log.d(TAG, "word Read : " + word);
    }

推荐阅读