如何从sparseArray减去单个字符串?

时间:2018-10-27 23:04:17

标签: android android-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中的所有元素

enter image description here

似乎我在问一个不太明显的问题。但是我可以从上面打印的那些“单词”中仅提取一个单词还是几个单词吗?例如,我要提取字符大于12且其中有数字的单词。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您正在循环运行单词,这就是为什么它会打印所有值的原因。当根据@navylover的答案只运行一次时,您将获得一个字符串。只需删除for循环

相关问题