0

Let me start off with I have no code and I have no idea what I am suppose to do. Apparently the information is stored in the meta data of the .png file. I figure this should not be so hard to do, but it is turning out to be a challenge.

The only thing that looks somewhat useful is the following post here at stack overflow.

How to set DPI information in an image?

However I have tried to use the code that was provided and the websites that were provided to do such things and have achieved nothing.

When I try to use the Iterator, like below, I get an error with both the iw.hasNext() and the iw.next().

Errors are:

  • The method hasNext() is undefined for the type HTMLDocument.Iterator
  • Type mismatch: cannot convert from void to ImageWriter

    for (Iterator iw = (Iterator) ImageIO.getImageWritersByFormatName(formatName); iw.hasNext;) { ImageWriter writer = iw.next();

My question is, where do I start and what do I need to be looking for? Are there any examples that you know of or have you done this yourself?


One a side note my questions about the "How to set DPI information in an image?" post are this...

  1. Why is it then when I use the Iterator like he does, I get errors?
  2. What is "INCH_2_CM" suppose to mean / be?

The way I changed his code is below...


    for (Iterator iw = (Iterator) ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
       ImageWriter writer = iw.next();
       ImageWriteParam writeParam = writer.getDefaultWriteParam();
       ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
       IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
       if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
          continue;
       }

       setDPI(metadata);

       final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
       try {
          writer.setOutput(stream);
          writer.write(metadata, new IIOImage(gridImage, null, metadata), writeParam);
       } finally {
          stream.close();
       }
       break;
    }
Community
  • 1
  • 1
Zeveso
  • 1,274
  • 3
  • 21
  • 41
  • 2
    INCH_2_CM is probably a `final` variable equal to 2.54, the multiplication constant to convert a length in inches to its value in centimeters. – corsiKa May 18 '12 at 23:03

1 Answers1

2

Your code is getting confused between the classes javax.swing.text.html.HTMLDocument.Iterator and java.util.Iterator. His code uses the latter, but your code seems to be using the former. Use fully qualified types, or figure out how to modify your imports.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • You are right, I feel like an idiot. How did you figure that out without me showing my imports? – Zeveso May 19 '12 at 00:17
  • It was pretty clear from `The method hasNext() is undefined for the type HTMLDocument.Iterator`. – Keith Randall May 19 '12 at 00:18
  • In all honesty, a poor decision on the designer of that library, knowing the possible confusion down the road. Chances are, they expected people to simply use enhanced for loops instead of Iterators. – corsiKa May 19 '12 at 20:57
  • @corsiKa: you're right, it was a dumb naming decision. The decision predated enhanced for loops, so there's no excuse. I always get bitten by `java.util.List` and `java.awt.List`. – Keith Randall May 20 '12 at 02:21