. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to run a new project. I do a couple of things like this:
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.");
}
}}
But in my program, I am getting the warning:
Error: can only iterate over an array or an instance of java.lang.Iterable
Can someone explain why the “ can only iterate over an array or an instance of java.lang.iterable” issue happened? Where have I gone wrong? Thank you!
The cause: This warning expressly states that you should iterate on iterable objects.
The solution: I assume Nodecollection is probably a com.aspose.words.NodeCollection. If you want to use the foreach syntax, you should do the following:
It is clear that it says you should only iterate on iterable objects.
You are using a code
If the
shapes
base classes is an instancejava.util.Collection
, orjava.lang.Iterable
, then thefor
loop will fail.You can check if
NodeCollection
has a collection class that implementedjava.lang.Iterable
.Edit:
NodeCollection
implements genericIterable
directly without specifying which objects it would be handling. You should therefore generate theIterator
explicitly from theNodeCollection
instance, and then iterate.Refer for a similar answer so