请帮助我,我的错误似乎无法使它起作用,因为它只能在数组或java.lang.Iterable实例上进行迭代。我想创建条形码并阅读并将其添加到Word文档中

更新发布后,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.");
        }
    }}

最佳答案

我假设Nodecollection是com.aspose.words.NodeCollection。

如果要使用foreach语法,则最好执行以下操作:

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

10-07 16:16