6

please help me with my error can't seem to make it work because of that can only iterate over an array or an instance of java.lang.Iterable. i want to create a barcode and read it and add it to the word document

Update Post the nodeCollection is from the com.aspose.words.

import com.aspose.barcode.*;
import com.aspose.barcoderecognition.BarCodeReadType;
import com.aspose.barcoderecognition.BarCodeReader;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageType;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.Shape;

              try
        {
            // Generate barcode image
            BarCodeBuilder builder = new BarCodeBuilder();
            builder.setSymbologyType(Symbology.Code39Standard);
            builder.setCodeText("test-123");
            String strBarCodeImageSave = "img.jpg";
            builder.save(strBarCodeImageSave);

            // Add the image to a Word doc
            Document doc = new Document();
            DocumentBuilder docBuilder = new DocumentBuilder(doc);
            docBuilder.insertImage(strBarCodeImageSave);
            String strWordFile = "docout.doc";
            doc.save(strWordFile);

            // Recognition part
            // Extract image from the Word document
            NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
            int imageIndex = 0;

            for(Shape shape: shapes)
            {   
                if (shape.hasImage())
                {
                    // If this shape is an image, extract image to file
                    String extension = ImageTypeToExtension(shape.getImageData().getImageType());
                    String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
                    String strBarCodeImageExtracted = "" + imageFileName;
                    shape.getImageData().save(strBarCodeImageExtracted);

                    // Recognize barcode from this image
                    BarCodeReader reader = new BarCodeReader((BufferedImage) Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
                    while (reader.read())
                    {
                        System.out.println("codetext: " + reader.getCodeText());
                    }
                    imageIndex++;
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }

    private static String ImageTypeToExtension(int imageType) throws Exception
    {
        switch (imageType)
        {
            case ImageType.BMP:
                return "bmp";
            case ImageType.EMF:
                return "emf";
            case ImageType.JPEG:
                return "jpeg";
            case ImageType.PICT:
                return "pict";
            case ImageType.PNG:
                return "png";
            case ImageType.WMF:
                return "wmf";
            default:
                throw new Exception("Unknown image type.");
        }
    }}
Rohan21
  • 353
  • 5
  • 9
  • 24
  • 4
    What is NodeCollection? – assylias Mar 15 '14 at 14:52
  • 3
    @AKS `can only iterate over an array or an instance of java.lang.Iterable` is a compiler error, not a runtime error. There is no stacktrace. – Tobias Mar 15 '14 at 14:54
  • 1
    Simple: don't use a for-each loop here but rather either a standard for loop or a while loop. – Hovercraft Full Of Eels Mar 15 '14 at 14:55
  • The error message is correct. The shorthand notation only works for arrays or `Iterable`s. If your class doesn't implement Iterable, explicitly ask it for its iterator and use that; if it doesn't have a `getIterator` or equivalent method, write an explicit loop to fetch its contents. – keshlam Mar 15 '14 at 14:57
  • `"Update Post the nodeCollection is from the com.aspose.words."` -- then the first thing you should do, before even coming here, is to look up the documentation for this class. This should be a reflex action. – Hovercraft Full Of Eels Mar 15 '14 at 15:04
  • @Rohan21: `com.aspose.words.Nodecollection` is an instance of `Iterable`. You should construct an iterator object explicitly on the `shapes` object and then use in `for` loop. Check my updated answer. – Ravinder Reddy Mar 15 '14 at 15:14

2 Answers2

3

Error: can only iterate over an array or an instance of java.lang.Iterable

It clearly says that you should iterate only on objects which are iterable.

In your code you are using

NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
...    
for(Shape shape: shapes)

The for loop fails unless the shapes base class is an instance of a java.util.Collection or java.lang.Iterable.

Check if NodeCollection is a collection type class that implemented java.lang.Iterable.


Edit:

the nodeCollection is from the com.aspose.words.

NodeCollection implements generic Iterable directly, without specifying the type of objects it would be handling. Hence you should explicitly generate the Iterator from the NodeCollection instance and on that you can iterate.

NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true, false);
Iterator<Shape> shapesIterator = shapes.iterator();
...    
// now use the above iterator in for loop, as below
for( Shape shape: shapesIterator )

Refer to a similar answer on so

Community
  • 1
  • 1
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
3

I assume Nodecollection is a com.aspose.words.NodeCollection.

If you want to use the foreach syntax you better do:

Node[] shapesArray = shapes.toArray();
for (Node node : shapesArray ){ ...
jjavier
  • 71
  • 2