Hi Jody and all,
this evening I have performed tests with ImageIO myself. Up to now I had only
used the JAI internal formats.
The good news is that I'd call the tests successful. What were the lessens
learned?
1. Every file format works for me ... at least in the IDE.
I wrote a small test case loading a small JPEG image and then saving it with
all available ImageWriters. All produces images that were readable. (OK, except
RAW and one writer which could not cope with an RGB image: wbmp).
Then I switched to the JRE delivered with uDig. It worked perfectly well. So
the JRE is NOT the problem.
- Mmaybe the test below will fail in uDig's OSGi environment for whatever
reason?
- Or it may be because in my test I don't have a BufferedImage but a RenderedOP
instance?
2. ImageIO class is easy to use but useless for serious file format discovery
(imho).
You can query file formats, file extensions and mime types from ImageIO class
but since every writer advertises several file formats, extensions and mime
types using the returned lists for a widget is nonsense. The script below shows
an alternative using the SPI itself to iterate the writers and fetch their
information. Then you should limit the dropdown list to ONE file format name.
As far as I can tell they don't overlap.
You'll then have to find out which writer supports your image (not shown in
code below).
Then you either use ImageIO.writeFile if you give ImageIO the choice of the
ImageWriter or you use the writer that provided you the file format as in my
example. (So you are SURE the intended writer is picked.)
So how to proceed?
Since the JRE and JAI/ImageIO work fine the problem must be on the uDIG end.
F.e. I'd check the rendered image which you intend to save for sanity or I'd
try to run the test below (adapted slightly) from within uDig and check the
result.
-----------------------
package jUnit;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageOutputStream;
import javax.media.jai.JAI;
import junit.framework.TestCase;
import org.mabautils.Convert;
import org.mabautils.OS;
/** Simple test for image reading and writing to the formats supported by
ImageIO
* @author Matthias
*/
public class Test_ImageIO extends TestCase {
public void testLoadAndSaveImageFile() {
try {
RenderedImage img = JAI.create("fileload",
Test_ImageIO.class.getResource("Fence_climber.jpg").getPath());
System.out.println("Image size: " + img.getWidth() +
'x' + img.getHeight() + "pixels");
System.out.println("Data type (0=TYPE_BYTE):" +
img.getSampleModel().getDataType());
System.out.println("Color model:" +
img.getColorModel().toString());
String tempDir = OS.get(OS.TEMP_DIR);
System.out.println("--- SUFFIXES ---");
for (String s : ImageIO.getWriterFileSuffixes()){
System.out.println(s);
}
System.out.println("--- FORMATS ---");
for (String s : ImageIO.getWriterFormatNames()){
System.out.println(s);
}
System.out.println("--- MIME TYPES ---");
for (String s : ImageIO.getWriterMIMETypes()){
System.out.println(s);
}
//**********************************************
System.out.println("--- WRITERS ---");
IIORegistry reg = IIORegistry.getDefaultInstance();
Iterator<ImageWriterSpi> it =
reg.getServiceProviders(ImageWriterSpi.class, true);
while (it.hasNext()){
ImageWriterSpi spi = it.next();
System.out.println(spi.getDescription(Locale.getDefault()));
String[] formatNames = spi.getFormatNames();
String[] suffixes = spi.getFileSuffixes();
String[] mimes = spi.getMIMETypes();
System.out.println(Convert.concatenate(formatNames, ","));
System.out.println(Convert.concatenate(suffixes, ","));
System.out.println(Convert.concatenate(mimes,
","));
ImageWriter writer = spi.createWriterInstance();
File output = new File("E://test." +
suffixes[0]);
ImageOutputStream stream = null;
try {
output.delete();
stream = ImageIO.createImageOutputStream(output);
writer.setOutput(stream);
writer.write(img);
} catch (IOException e) {
throw new IIOException("Can't create output
stream!", e);
} catch (Exception e) {
System.err.println("Cannot write image to
format " + formatNames[0]);
} finally {
writer.dispose();
stream.flush();
stream.close();
}
System.out.println("----------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
}
-----------------------
--
Matthias Basler
[EMAIL PROTECTED]
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel