Results 1 to 3 of 3
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default ImageIO - Saving JPGs

    I'm working on an Image Editor program which supports transparency (alpha). But, when I try to save it as a JPEG/JPG (no transparency) it changes the colors around entirely. I tried copying the data over to an RBG image from the ARGB image, but it doesn't seem to solve the problem. I'm pretty new to the ImageIO stuff. Also, it saves fine when I load in a JPG then resave as a JPG, so I'm guessing it's an issue with converting it.

    Saves fine as PNG or GIF (left), but not when saved as JPG (right)


    Then when I changed it to the below code, JPGs save like this:


    Here's my save method.

    Java Code:
    public void saveImage(File f)
    {
    	try
    	{
    		String ext = FileUtils.getExtension(f);
    		if(ext.equals("jpg") || ext.equals("jpeg"))
    		{
    			BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    			copy.setData(image.getData());
    			ImageIO.write(copy, FileUtils.getExtension(f), f);
    		}
    		else
    		{
    			ImageIO.write(image, FileUtils.getExtension(f), f);
    		}
    		status = FileStatus.SAVED;
    	}
    	catch(IOException e)
    	{
    		e.printStackTrace();
    	}
    }
    Last edited by AndrewM16921; 12-25-2012 at 06:49 PM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: ImageIO - Saving JPGs

    Hmm, I found an interesting solution. I just drew one image on a blank image of the other type to "convert" it. :P

    Java Code:
    public class ImageUtils
    {
    	public static BufferedImage ARGBtoRGB(BufferedImage argbImage)
    	{
    		BufferedImage rgbImage = new BufferedImage(argbImage.getWidth(), argbImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    		rgbImage.getGraphics().drawImage(argbImage, 0, 0, null);
    		return rgbImage;
    	}
    	
    	public static BufferedImage RGBtoARGB(BufferedImage rgbImage)
    	{
    		BufferedImage argbImage = new BufferedImage(rgbImage.getWidth(), rgbImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    		argbImage.getGraphics().drawImage(rgbImage, 0, 0, null);
    		return argbImage;
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ImageIO - Saving JPGs

    That makes sense. Your first bit of code copies the data assuming that the data configuration is automatically BufferedImage.TYPE_INT_RGB, and it seems that this just is not so.

Similar Threads

  1. ImageIO question
    By lordsurya08 in forum New To Java
    Replies: 1
    Last Post: 12-23-2011, 07:17 AM
  2. JPEGCodec and ImageIO
    By Lacrim in forum Advanced Java
    Replies: 0
    Last Post: 12-14-2011, 02:57 PM
  3. Error with ImageIO
    By stegano in forum New To Java
    Replies: 4
    Last Post: 03-31-2011, 03:59 PM
  4. Trouble with ImageIO
    By sari in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 09:42 PM
  5. Help with ImageIO
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •