egono
asked on
Create transparent GIF with JAI
Hello,
I'm trying to create a transparent GIF with JAI:
private static void createImage(BufferedImage argBufferedImage, String argFileName) {
ImageWriter gifWriter = null;
IIOImage iioImage = null;
IIOMetadata iioMetadata = null;
IIOMetadataNode gifMetadataNode = null;
IIOMetadataNode tmpNode = null;
gifWriter = (ImageWriter) ImageIO.getImageWritersByF ormatName( "gif").nex t();
try {
gifWriter.setOutput(ImageI O.createIm ageOutputS tream(new File(argFileName)));
iioMetadata = gifWriter.getDefaultImageM etadata(ne w ImageTypeSpecifier(argBuff eredImage) , null);
gifMetadataNode = (IIOMetadataNode) iioMetadata.getAsTree("jav ax_imageio _gif_image _1.0");
tmpNode = (IIOMetadataNode) gifMetadataNode.getElement sByTagName ("GraphicC ontrolExte nsion").it em(0);
tmpNode.setAttribute("tran sparentCol orIndex", "0");
tmpNode.setAttribute("tran sparentCol orFlag", "true");
iioMetadata.setFromTree("j avax_image io_gif_ima ge_1.0", gifMetadataNode);
iioImage = new IIOImage(argBufferedImage, null, iioMetadata);
gifWriter.write(iioImage);
gifWriter.dispose();
} catch (Exception e) {
System.out.println("ex:" + e);
}
}
Unfortunately my code doesn't work. Any recommendations?
I'm trying to create a transparent GIF with JAI:
private static void createImage(BufferedImage argBufferedImage, String argFileName) {
ImageWriter gifWriter = null;
IIOImage iioImage = null;
IIOMetadata iioMetadata = null;
IIOMetadataNode gifMetadataNode = null;
IIOMetadataNode tmpNode = null;
gifWriter = (ImageWriter) ImageIO.getImageWritersByF
try {
gifWriter.setOutput(ImageI
iioMetadata = gifWriter.getDefaultImageM
gifMetadataNode = (IIOMetadataNode) iioMetadata.getAsTree("jav
tmpNode = (IIOMetadataNode) gifMetadataNode.getElement
tmpNode.setAttribute("tran
tmpNode.setAttribute("tran
iioMetadata.setFromTree("j
iioImage = new IIOImage(argBufferedImage,
gifWriter.write(iioImage);
gifWriter.dispose();
} catch (Exception e) {
System.out.println("ex:" + e);
}
}
Unfortunately my code doesn't work. Any recommendations?
ASKER
No - the background color is just not set transparent.
I get a
java.lang.NullPointerExcep tion
at com.sun.media.imageioimpl. common.Pal etteBuilde r.findPale tteEntry(P aletteBuil der.java:3 49)
at com.sun.media.imageioimpl. common.Pal etteBuilde r.getIndex ColorModel (PaletteBu ilder.java :335)
at com.sun.media.imageioimpl. common.Pal etteBuilde r.getIndex edImage(Pa letteBuild er.java:18 4)
at com.sun.media.imageioimpl. common.Pal etteBuilde r.createIn dexedImage (PaletteBu ilder.java :116)
at com.sun.media.imageioimpl. plugins.gi f.GIFImage Writer.wri te(GIFImag eWriter.ja va:602)
at com.sun.media.imageioimpl. plugins.gi f.GIFImage Writer.wri te(GIFImag eWriter.ja va:530)
at javax.imageio.ImageWriter. write(Imag eWriter.ja va:573)
if I want to create a complete transparent gif. But this seems to be a known bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6287936
Again - the NPE is *not* my problem.
regards
I get a
java.lang.NullPointerExcep
at com.sun.media.imageioimpl.
at com.sun.media.imageioimpl.
at com.sun.media.imageioimpl.
at com.sun.media.imageioimpl.
at com.sun.media.imageioimpl.
at com.sun.media.imageioimpl.
at javax.imageio.ImageWriter.
if I want to create a complete transparent gif. But this seems to be a known bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6287936
Again - the NPE is *not* my problem.
regards
Can you try to upgrade/downgrade the library?
ASKER
girionis - I could try this, but the environment is given because the jai libs are used for other things too.
the question is: is my code the right way to create a transparent gif?
the question is: is my code the right way to create a transparent gif?
Not sure tbh. But the exception you get means that something is not loaded up properly so I guess there is a problem either with some sort fo configuration or with the package itself.
ASKER
girionis - no the exception is a know bug for complete transparent images, and again this is not my problem.
maybe I should change my question into: How to create a transparent gif with JAI?
maybe I should change my question into: How to create a transparent gif with JAI?
This link: http://www.esri.com/software/arcexplorer/about/ae9_faq.html states that this is a limitation. Maybe you can create a transparent PNG and then convert it to GIF?
ASKER
some more info. here is how I create my BufferedImage:
bufferedImage = new BufferedImage(argWidth, argHeight, BufferedImage.TYPE_INT_ARG B);
graphics2D = bufferedImage.createGraphi cs();
if (colorIdx == 0) {
graphics2D.setBackground(C olor.WHITE );
} else {
graphics2D.setBackground(C olor.BLACK );
}
graphics2D.clearRect(0, 0, argWidth, argHeight);
graphics2D.setColor(Color. BLUE);
graphics2D.fillRect(0, 0, 10, 10);
bufferedImage = new BufferedImage(argWidth, argHeight, BufferedImage.TYPE_INT_ARG
graphics2D = bufferedImage.createGraphi
if (colorIdx == 0) {
graphics2D.setBackground(C
} else {
graphics2D.setBackground(C
}
graphics2D.clearRect(0, 0, argWidth, argHeight);
graphics2D.setColor(Color.
graphics2D.fillRect(0, 0, 10, 10);
ASKER
girionis - I'm not displaying the gif with jai, I just want to save it (I'm using Paint.Net to view it)
If you just want to save it just use one of the usual output stream methods that write the binary data on the disk. But I guess the problem is that the image is not what you want and therefore before you save it you need it to be transparent. Not sure how exactly you can do it, but from a quick search on jai-interest archives I found the following URL for GIF files: http://archives.java.sun.com/cgi-bin/wa?A2=ind9902&L=jai-interest&P=R5968&I=-3
You might also want to try this: http://archives.java.sun.com/cgi-bin/wa?S1=jai-interest and do a search for "GIF encoding".
You might also want to try this: http://archives.java.sun.com/cgi-bin/wa?S1=jai-interest and do a search for "GIF encoding".
ASKER
Now here is a working solution. The trick is to use a IndexColorModel for the image. The createColorModel() method should create the desired color model, in my case a model with just 2 colors (first one is black).
create the image with
[...]
bufferedImage = new BufferedImage(argWidth, argHeight, BufferedImage.TYPE_BYTE_IN DEXED, createColorModel(COLORS[co lorIdx]));
[...]
private static IndexColorModel createColorModel(Color argColor) {
IndexColorModel result = null;
result = new IndexColorModel(1, 2,
new byte[] {0, (byte)argColor.getRed()},
new byte[] {0, (byte)argColor.getGreen()} ,
new byte[] {0, (byte)argColor.getBlue()},
0);
return result;
}
private static void createImage(BufferedImage argBufferedImage, String argFileName) {
ImageWriter gifWriter = null;
IIOImage iioImage = null;
IIOMetadata iioMetadata = null;
gifWriter = (ImageWriter) ImageIO.getImageWritersByF ormatName( "gif").nex t();
try {
gifWriter.setOutput(ImageI O.createIm ageOutputS tream(new File(argFileName)));
iioMetadata = gifWriter.getDefaultImageM etadata(ne w ImageTypeSpecifier(argBuff eredImage) , null);
iioImage = new IIOImage(argBufferedImage, null, iioMetadata);
gifWriter.write(iioImage);
} catch (Exception e) {
System.out.println("ex:" + e);
}
}
that's it :-)
create the image with
[...]
bufferedImage = new BufferedImage(argWidth, argHeight, BufferedImage.TYPE_BYTE_IN
[...]
private static IndexColorModel createColorModel(Color argColor) {
IndexColorModel result = null;
result = new IndexColorModel(1, 2,
new byte[] {0, (byte)argColor.getRed()},
new byte[] {0, (byte)argColor.getGreen()}
new byte[] {0, (byte)argColor.getBlue()},
0);
return result;
}
private static void createImage(BufferedImage argBufferedImage, String argFileName) {
ImageWriter gifWriter = null;
IIOImage iioImage = null;
IIOMetadata iioMetadata = null;
gifWriter = (ImageWriter) ImageIO.getImageWritersByF
try {
gifWriter.setOutput(ImageI
iioMetadata = gifWriter.getDefaultImageM
iioImage = new IIOImage(argBufferedImage,
gifWriter.write(iioImage);
} catch (Exception e) {
System.out.println("ex:" + e);
}
}
that's it :-)
Fine by me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
;JOOP!