Page 1 of 1

Set table size to image size.

Posted: Thu Jun 23, 2011 10:35 pm
by xsaero00
I am currently modifying DocBook InsertTable operation. One of the common tables to insert is a table that has mediaobject with imagedata as the body instead of the tgroup. I want to set the table width to the width of the image. I am having trouble getting the image width in Java. Currently I have:

TableInfo.java constructor

Code: Select all


          Image image = Toolkit.getDefaultToolkit().getImage(img_filename);
MediaTracker mTracker = new MediaTracker(the_table_dialog_object);
mTracker.addImage(image, 1);
try
{
mTracker.waitForID(1);
}
catch(InterruptedException e){}

image.getWidth(null); // returns -1
image.getHeight(null); // returns -1
Is there a utility class in Author API that I can use for finding out the image size?

Re: Set table size to image size.

Posted: Fri Jun 24, 2011 9:58 am
by Radu
Hi Mike,

I can give you the code which is used on our side to detect widths and heights for images which are supported by Java:

Code: Select all


 /**
* Get an image reader for the specified extension.
* @param is The input stream
* @return The image reader
*
* @throws IOException
*/
public static ImageReader getImageReader(InputStream is) throws IOException {
ImageReader imagereader = null;
try {
ImageInputStream imageStream = ImageIO.createImageInputStream(is);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageStream);
if (iterator.hasNext()) {
imagereader = iterator.next();
imagereader.setInput(imageStream);
}
} catch(IOException ex) {
ex.printStackTrace();
throw ex;
}
return imagereader;
}

public static void main(String[] args) throws IOException {
URL imageURL = ........;
boolean oldUseCache = ImageIO.getUseCache();
try {
ImageIO.setUseCache(false);
InputStream imageStream = imageURL.openStream();
ImageReader reader = null;
try {
reader = getImageReader(imageStream);
if (reader != null) {
int width = reader.getWidth(0);
int height = reader.getHeight(0);
}
} finally {
if(reader != null) {
reader.dispose();
}
imageStream.close();
}
}finally {
ImageIO.setUseCache(oldUseCache);
}
}
Regards,
Radu

Re: Set table size to image size.

Posted: Fri Jun 24, 2011 7:06 pm
by xsaero00
Thank you. It works now. I am using SimpleImageInfo class I found on the web http://jaimonmathew.wordpress.com/2011/ ... imageinfo/. It supposed to be faster than ImageIO. To get the file object of the image I am using AuthorUtilAccess.locateFile()