Set table size to image size.

Oxygen general issues.
xsaero00
Posts: 58
Joined: Sat Aug 01, 2009 12:57 am

Set table size to image size.

Post 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?
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Set table size to image size.

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
xsaero00
Posts: 58
Joined: Sat Aug 01, 2009 12:57 am

Re: Set table size to image size.

Post 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()
Post Reply