Android Question How to use for each

ykucuk

Well-Known Member
Licensed User
Longtime User
I'm using com.google.android.gmslay-services-vision for extract texts from camera preview. I can get value (text) of block but i can't value(text) of each line in the block.

B4X:
    for (Text line : tBlock.getComponents())  {

I found bellow java code but i don't know how to use

Any help ?

Java:
for (int index = 0; index < textBlocks.size(); index++) {
                        //extract scanned text blocks here
                        TextBlock tBlock = textBlocks.valueAt(index);
                        blocks = blocks + tBlock.getValue() + "\n" + "\n";
                        for (Text line : tBlock.getComponents()) {
                            //extract scanned text lines here
                            lines = lines + line.getValue() + "\n";
                            for (Text element : line.getComponents()) {
                                //extract scanned text words here
                                words = words + element.getValue() + ", ";
                            }
                        }
                    }

My code:
Dim frametext As JavaObject = frameBuildertext.RunMethod("build", Null)   

Dim SparseArrayText As JavaObject = textdetector.RunMethod("detect", Array(frametext))

LastPreview = DateTime.Now

Dim MatchesText As Int = SparseArrayText.RunMethod("size", Null)

 For i = 0 To MatchesText - 1

 Dim TextBlock As JavaObject = SparseArrayText.RunMethod("valueAt", Array(i))

Dim Lines As JavaObject = TextBlock.RunMethod("getComponents", Null)

Dim MatchesLine As Int = Lines.RunMethod("size", Null)

For z = 0 To MatchesLine - 1

Log ("line " & z)

Dim line As JavaObject = Lines.RunMethod("valueAt", Array(z)) ' doesn't work

 Next
 

OliverA

Expert
Licensed User
Longtime User
Any help ?

Try
B4X:
Dim sb As StringBuilder
sb.Initialize
Dim lines As List = TextBlock.RunMethod("getComponents", Null)
For Each line As JavaObject In lines
  Dim elements As List = line.RunMethod("getComponents", Null)
  For Each element As JavaObject In elements
    sb.Append($"${element.RunMethod("getValue", Null)}, "$)
  Next
Next
Dim words As String = sb.ToString
'Note: if any words are found, the last word will still have a ", " appended - just like the example Java code
Note: untested code (even for typing errors)
 
Upvote 0

ykucuk

Well-Known Member
Licensed User
Longtime User
It works fine. Thank you so much.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…