ImageIO

heya, i have written a java program which takes in a jpg, gif or png, adds text to that image then saves it as another file.

everything works fine, excecpt for transparent *.gif files.

Is it possible to read transparent gifs in java? i have the 1.4. something sdk.

worthy of note, is that i can open a jpg and save it as a gif fine.

code posted below:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;

public class ImageMod{
      public static void main(String[] args) throws Exception {
            /*
             * takes in 3 arguments;
             * input file
             * output file
             * text to be added to output file
             */
            
            if (args.length != 3) {
                  System.err.println("Usage: 3 arguments: args[0] file input, args[1] file output, args[2] the text");
                  System.exit(1);
            }
            // load input image
            BufferedImage image = ImageIO.read(new File(args[0]));
            
            // do the text
            Graphics graphics = image.getGraphics();
            //Color color = new Color(Color.BLUE);
            graphics.setColor(Color.BLUE);
            Font theFont = new Font("Times", Font.PLAIN, 18);
            graphics.setFont(theFont);
            graphics.drawString(args[2],
                  10, image.getHeight() - 10);
            // save modified image
            String format = "JPG";
            if (args[0].toLowerCase().endsWith(".png")) {
                  format = "PNG";
            }
          if (args[0].toLowerCase().endsWith(".gif")) {
                  format = "GIF";
            }
            ImageIO.write(image, format, new File(args[1]));
            // System.exit(0)
      }
}
twansAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
what exactly is the problem?
0
twansAuthor Commented:
>>everything works fine, excecpt for transparent *.gif files.
>>Is it possible to read transparent gifs in java? i have the 1.4. something sdk.

sorry, maybe i should have explained a little better.

When i take a transparent GIF as my input , after modifying and saving. the output.gif has a zero byte size.

As i can take a jpg and store it as a GIF i presume its not the saving in *.GIF format but rather reading GIFs.

i was wondering whether i am reading the GIF correctly (and if this method is possible for transparent gifs) with

ImageIO.write(image, format, new File(args[1]));

and if you can write transparent gifs, or if ImageIO will automatically add a background.

tia

-twans
0
objectsCommented:
i don't think imageio supports writing gifs.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

twansAuthor Commented:
it also seems that ImageIO doesnt supoprt transparent files in its read method.

any ideas of ways around it?

unfortunatnly transparent gifs are essential for the application.
0
objectsCommented:
what makes you think it doesn't support reading transparent gifs?
0
twansAuthor Commented:
if i use the following input:

"input.gif","output.gif","objects"

it creates the output.gif as a zero byte file (obviously unreadable)

furthermore, if you do

"input.gif","output.jpg","objects" it also creates a zero byte sized file output.jpg

however, if the input is input.jpg, regardless of the output (.png, .gif, .jpg) it will always work.

hence i am leaning towards the possiblility the transperancy and/or reading of a gif is what is not supported.

any ideas would be much appreciated

cheers
0
objectsCommented:
> "input.gif","output.gif","objects"
> it creates the output.gif as a zero byte file (obviously unreadable)

no it can't write it, have you tried displaying the read image.

0
twansAuthor Commented:
havnt tried displaying the read image >> will do that now.

>>no it can't write it

like i said, if you use input.jpg as the input and output to a GIF it works, whether its properly encoded as a GIF im not sure as im no specialist with imagining, however it successfully displays in a webpage when calling <img src="whatever.gif">.
0
objectsCommented:
try the following to see what is supported:

http://javaalmanac.com/egs/javax.imageio/GetFormats.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
twansAuthor Commented:
great. thanks for that link. backed up exactly what you were saying :)

still interesting that although the ImageIO is meant to support gif's as readable.. they are causing the zero byte files to be saved (reguardless of the format, jpg etc)

it is also interesting to note that this problem with reading gif's is not dependent upon transperency.

this issue arises when displaying the gif within a Toolkit viewer (taking your advice to display the input file, before outputting).

so what can this be said about java's 'ability' to read gif's through the imageio.

pleaes understand i have tried (and still am) furiously on google to answer some of my questions, but i cannot unfortunantly find anything suitable to my questions.

thanks
0
twansAuthor Commented:
i am sorry, my previous post is incorrect.

the gif is supported with both transparent and normal backgrounds for viewing.

i have found a java gif encoder and will pursue trying to use that to solve my issue.

thankyou objects for your help

:D

-twans
0
objectsCommented:
no worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.