Re: highlighting word in JPEG image
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Tue, 28 Jun 2005 13:10:39 -0600
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
.
- References:
- highlighting word in JPEG image
- From: Marian Hellema
- highlighting word in JPEG image
- Prev by Date: Re: JTable Drag and Drop Rows
- Next by Date: Re: highlighting word in JPEG image
- Previous by thread: highlighting word in JPEG image
- Next by thread: Re: highlighting word in JPEG image
- Index(es):
Relevant Pages
|
|