【问题标题】:How to substract single string from a sparseArray?如何从 sparseArray 中减去单个字符串?
【发布时间】:2018-10-27 23:04:17
【问题描述】:

我使用 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 且包含数字的单词。

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

【问题讨论】:

  • 当您说has character above 12 时,您的意思是has 12 or more characters?当你说has number in it时,你的意思是你想匹配一个特定的数字还是任何数字?
  • 是的,这只是一个案例。我需要的是从 sparseArray 中减去一个字符串来满足我的特定条件。

标签: android android-sparsearray


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回复,但是“word read”的结果仍然打印在字符串列表中。我仍然不知道如何使它只是一个或几个字符串而不是字符串列表
  • 好吧,我认为这是一个很好的选择,我添加了 if 和 else {},奇怪的是 if 和 else 条件都被 sparseArray 激活。我的错。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    相关资源
    最近更新 更多