[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ImageIO

Posted on 2004-10-21
12
Medium Priority
?
684 Views
Last Modified: 2012-05-05
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)
      }
}
0
Comment
Question by:twans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12376280
what exactly is the problem?
0
 

Author Comment

by:twans
ID: 12376995
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12377008
i don't think imageio supports writing gifs.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:twans
ID: 12377083
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
 
LVL 92

Expert Comment

by:objects
ID: 12377111
what makes you think it doesn't support reading transparent gifs?
0
 

Author Comment

by:twans
ID: 12377146
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
 
LVL 92

Expert Comment

by:objects
ID: 12377191
> "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
 

Author Comment

by:twans
ID: 12377226
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12377286
try the following to see what is supported:

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

Author Comment

by:twans
ID: 12377687
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
 

Author Comment

by:twans
ID: 12377710
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
 
LVL 92

Expert Comment

by:objects
ID: 12377726
no worries :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question