Search code examples
javaapigoogle-cloud-platformocrgoogle-cloud-vision

Google Cloud Vision Block Index


could you give me information on how to obtain the index of the block that is iterating, that is, if I need to know in what block number during the iteration of this.

Here its an example:

TextAnnotation annotation = res.getFullTextAnnotation();                
            for (com.google.cloud.vision.v1.Page page : annotation.getPagesList()) {
                String pageText = "";
                System.out.println("Bloque-->" + page.getBlocksCount());
                
                for (Block block : page.getBlocksList()) {
                    String blockText = "";
                    
                    //System.out.println("Index of block-->" + block.getIndex());
                    System.out.println("Paragrafos-->" + block.getParagraphsCount());
                

                

Solution

  • If you need to know the index, just don't use the enhanced for loop - use a regular for loop, and access the element by index. (Unless things have changed significantly since I last looked at the Java protobuf implementation, access by index is cheap.)

    
    for (int i = 0; i < page.getBlocksCount(); i++) {
        Block block = page.getBlocksList().get(i);
        // ...
    }