Re: highlighting word in JPEG image



Marian Hellema <lujanp@xxxxxxxxxx> wrote:
> - The coordinates are from Prime Recognition OCR-software. They say:
> "the coordinates are in BMU units. One BMU is 1/1200 of an inch". Trial
> and error with an example showed me that dividing their coordinates by
> 4 gave me the right coordinates for the word in Java. Would this always
> be correct or is there a more universal way of converting the
> OCR-coordinates to Java-coordinates? I read somewhere that Java
> coordinates are 72 units (pixels) per inch, so I can't understand how my
> division by 4 can give me the right result, but it does :-)

Just answering this part of your question for now.

Java does not always work in 72 dpi, so I don't know where you got that
information from. It is senseless to try to identify a pixel resolution
for "Java". Java can work with images in all sorts of resolutions. It
appears that the images you're getting are in 300 dpi resolution. That
can definitely vary depending on many factors, most notably the settings
of your scanner when you scanned the image.

Determining the resolution of an image is interesting, though. ImageIO
(part of the standard library as of 1.4, IIRC) provides the basic tools,
but it doesn't seem to properly interpret JPEG resolutions in the
standard cross-format encoding. As a result, you need to use JPEG-
specific code to get the values. It looks something like this:

public class Test
{
public static void main(String[] args)
throws Exception
{
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
if (result != JFileChooser.APPROVE_OPTION) return;

File f = chooser.getSelectedFile();
FileImageInputStream stream = new FileImageInputStream(f);

Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
assert readers.hasNext();
ImageReader r = readers.next();

r.setInput(stream);

final int RES_ASPECT = 0;
final int RES_INCHES = 1;
final int RES_CENTIMETERS = 2;

double xRes = Double.NaN, yRes = Double.NaN;

IIOMetadata meta = r.getImageMetadata(0);
Element topNode = (Element)
meta.getAsTree("javax_imageio_jpeg_image_1.0");

NodeList nodeList = topNode.getElementsByTagName("app0JFIF");
if (nodeList.getLength() == 1)
{
Element app0 = (Element) nodeList.item(0);

if (app0.hasAttribute("resUnits"))
{
int units = Integer.parseInt(
app0.getAttribute("resUnits"));
if (units != RES_ASPECT)
{
int xd = Integer.parseInt(
app0.getAttribute("Xdensity"));
int yd = Integer.parseInt(
app0.getAttribute("Ydensity"));

if (units == RES_INCHES)
{
xRes = xd;
yRes = yd;
}
else if (units == RES_CENTIMETERS)
{
xRes = xd * 2.54;
yRes = xd * 2.54;
}
}
}
}

System.out.println(xRes + " x " + yRes);
}
}

Not all JPEG files include resolution information. When this is the
case, they are often interpreted by various programs as either 72 dpi or
96 dpi.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.



Relevant Pages

  • Reference to a class that does not exist
    ... I have been studying chapter 12 of the Java Language Specification ... "The only requirement on when resolution is performed is that any ... require linkage to the class or interface ...
    (comp.lang.java.programmer)
  • Re: Determining Printer Resolution
    ... > But java printing API provides no means to determine that. ... It is easy to miss, because there is not one printing APi, but at least ... Regarding printer resolution: ... PrintRequestAttributeSet is, you guessed it, PrinterResolution. ...
    (comp.lang.java.programmer)
  • Re: General Guidelines about Java Application
    ... > Java Application? ... you have enough time to make the customer happy. ... make sure the interface fits completely in the lowest resolution ...
    (comp.lang.java.programmer)
  • Re: Declaring local variables inside loop
    ... clock) resolution is trivial in this case. ... Java adds a few twiddles, but they have been old hat too for several ...
    (comp.lang.java.programmer)
  • Re: Find the resolution of images
    ... I sent you ther way to find the correct resolution. ... resolution of images is less than 300 DPI or not. ... Dim MyImage As System.Drawing.Image = Nothing ... 'Target Pixels of the image to have 300 DPI ...
    (microsoft.public.dotnet.framework.drawing)