0

I want to get image dpi to convert pixels to cm, so I use ImageInfo.java to get this:

ImageInfo myMapInfo = new ImageInfo();
            try {
                File testFile = new File(fileNameString);
                InputStream myMapStream = new FileInputStream(testFile);
                myMapInfo.setInput(myMapStream);
                myMapInfo.setDetermineImageNumber(true);
                myMapInfo.setCollectComments(true);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            x = myMapInfo.getPhysicalWidthInch();
            y = myMapInfo.getPhysicalHeightInch();

but unfortunately I get -1 and -1 in (x, y)! I use this class: ImageInfo.java

Mahdi_Alavi
  • 45
  • 1
  • 2
  • 8

1 Answers1

0

From looking at the source code of ImageInfo, you need to call the check() method of your myMapInfo variable and if this passes you can use the various getXxX() methods to obtain whatever information you need. Incidentally getting -1 as result means information is not available, but that's most likely cause you're not invoking the check() method.

Radi Radichev
  • 562
  • 6
  • 13
  • Do you mean to use the `check()` method like: `boolean sth = myMapInfo.check()` ? , I used it and `sth` has a true value but the error remains... – Mahdi_Alavi May 24 '17 at 07:06
  • After you set your input and determineImageNumber etc., you need to call `myMapInfo.check()` . This will try to populate the `myMapInfo` with the available information. You can also try running ImageInfo.java (it has main method and can be used as a console application) directly and supply your test file as input, just to check it works. – Radi Radichev May 24 '17 at 11:12
  • I recognized after testing so, `myMapInfo.getFormatName()` and `myMapInfo.getMimeType()` , ... are working well, but `myMapInfo.getPhysicalWidthInch()` or `myMapInfo.getPhysicalWidthDpi()` returns `-1` ! – Mahdi_Alavi May 24 '17 at 13:48
  • Maybe there is something wrong with the image or the library. I would put a break point in the `check()` method and follow through to see where it goes wrong. – Radi Radichev May 29 '17 at 12:11