Solved

ImageIO

Posted on 2004-10-21
674 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
Question by:twans
    12 Comments
     
    LVL 92

    Expert Comment

    by:objects
    what exactly is the problem?
    0
     

    Author Comment

    by:twans
    >>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
    i don't think imageio supports writing gifs.
    0
     

    Author Comment

    by:twans
    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
    what makes you think it doesn't support reading transparent gifs?
    0
     

    Author Comment

    by:twans
    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
    > "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
    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:
    try the following to see what is supported:

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

    Author Comment

    by:twans
    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
    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
    no worries :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    modThree challenge 4 42
    tripleUp challenge 7 45
    tenRun challenge 28 50
    Return ZipOutputStream using Servlet 2 18
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now