简体   繁体   English

错误:只能遍历数组或java.lang.Iterable的实例

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

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. 请帮助我,我的错误似乎无法使其工作,因为它只能迭代数组或java.lang.Iterable的实例。 i want to create a barcode and read it and add it to the word document 我想创建一个条形码并阅读它并将其添加到word文档中

Update Post the nodeCollection is from the com.aspose.words. 更新发布 nodeCollection来自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.");
        }
    }}

Error: can only iterate over an array or an instance of java.lang.Iterable 错误:只能遍历数组或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 . 除非shapes基类是java.util.Collectionjava.lang.Iterable的实例,否则for循环失败。

Check if NodeCollection is a collection type class that implemented java.lang.Iterable . 检查NodeCollection是否是实现java.lang.Iterable集合类型类。


Edit : 编辑

the nodeCollection is from the com.aspose.words. nodeCollection来自com.aspose.words。

NodeCollection implements generic Iterable directly, without specifying the type of objects it would be handling. NodeCollection直接实现泛型Iterable ,而不指定它将处理的对象的类型。 Hence you should explicitly generate the Iterator from the NodeCollection instance and on that you can iterate. 因此,您应该从NodeCollection实例显式生成Iterator ,然后您可以迭代。

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 请参阅 类似的答案

I assume Nodecollection is a com.aspose.words.NodeCollection. 我假设Nodecollection是一个com.aspose.words.NodeCollection。

If you want to use the foreach syntax you better do: 如果您想使用foreach语法,最好:

Node[] shapesArray = shapes.toArray();
for (Node node : shapesArray ){ ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能迭代一个数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable 只能迭代java.lang.Iterable的数组或实例吗? - Can only iterate over an array or an instance of java.lang.Iterable? Eclipse 编译错误显示不正确(只能迭代数组或 java.lang.Iterable 的实例) - Eclipse compile error shown incorrectly (Can only iterate over an array or an instance of java.lang.Iterable) 规则编译错误:只能迭代java.lang.Iterable的数组或实例 - Rule Compilation error : Can only iterate over an array or an instance of java.lang.Iterable 在reducer中的for循环上获得编译错误“只能迭代数组或java.lang.Iterable实例” - Getting compilation error “Can only iterate over an array or an instance of java.lang.Iterable” at for loop in reducer 只能在java.lang.Iterable中的数组或实例上进行迭代 - Can only iterate over an array or an instance of java.lang.Iterable in java 使用FileUtils时只能迭代数组或java.lang.Iterable的实例 - Can only iterate over an array or an instance of java.lang.Iterable while using FileUtils 在包含私有集合的类上使用迭代器错误“只能迭代数组或java.lang.Iterable的实例” - Error “Can only iterate over an array or an instance of java.lang.Iterable” using iterator on a class that contains a private collection 另一个“只能迭代数组或java.lang.Iterable的实例”问题 - Yet another “Can only iterate over an array or an instance of java.lang.Iterable” issue ForStatement-只能迭代数组或java.lang的实例。在csv文件创建中可迭代 - ForStatement - Can only iterate over an array or an instance of java.lang.Iterable in csv file creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM