Results 1 to 8 of 8
  1. #1
    shadowayex is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Reading image files into Strings

    For a program I'm working on, it's required to read in data from an image file, store it in as a string in it's own file, and then be able to read the data and put it into a BufferedImage. I wrote some testing code to try to get a hang of what the process might need.

    If I read in the data as a byte array and use a byte stream to create the BufferedImage, it displays fine in a Swing application:
    Java Code:
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.imageio.stream.FileImageInputStream;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class TestCode
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			File f = new File("star.jpg");
    			FileImageInputStream im = new FileImageInputStream(f);
    
    			byte[] data = new byte[(int) f.length()];
    			int read = 0;
    			int bytesRead = 0;
    
    			while (read < data.length
    					&& (bytesRead = im.read(data, read, data.length - read)) > -1)
    			{
    				read += bytesRead;
    			}
    
    			BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
    
    			JFrame frame = new JFrame();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setPreferredSize(new Dimension(800, 600));
    			frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    			frame.pack();
    			frame.setVisible(true);
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    The image displays just as it should. But if I convert the byte array into a String, use getBytes on that String, and supply that array to the byte stream, the image comes out wrong.

    Java Code:
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.imageio.stream.FileImageInputStream;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class TestCode
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			File f = new File("star.jpg");
    			FileImageInputStream im = new FileImageInputStream(f);
    
    			byte[] data = new byte[(int) f.length()];
    			int read = 0;
    			int bytesRead = 0;
    
    			while (read < data.length
    					&& (bytesRead = im.read(data, read, data.length - read)) > -1)
    			{
    				read += bytesRead;
    			}
    
    			String dataStr = new String(data);
    			BufferedImage img = ImageIO.read(new ByteArrayInputStream(dataStr.getBytes()));
    
    			JFrame frame = new JFrame();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setPreferredSize(new Dimension(800, 600));
    			frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    			frame.pack();
    			frame.setVisible(true);
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Is there a particular process I have to do when converting between the byte array and String that will make this work? Also, as soon as I find a solid method I can use in a closed-source proprietary program, I plan on encoding the data in Base64. I don't know if that changes anything, but I figured I should mention it.

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

    Default Re: Reading image files into Strings

    You may be running into a Charset issue where bytes that aren't part of the default Charset are being transformed into the byte equivalent of '?'. What if you read data into a String with a different Charset, and read it out with the same?

    For example:

    Java Code:
             while (read < data.length
                   && (bytesRead = im.read(data, read, data.length - read)) > -1) {
                read += bytesRead;
             }
    
             System.out.println("charset: "
                   + Charset.defaultCharset().displayName());
    
             Charset charset = Charset.forName("ISO-8859-1");
             String dataStr = new String(data, charset);
    
             byte[] data2 = dataStr.getBytes(charset);
    
             BufferedImage img = ImageIO.read(new ByteArrayInputStream(data2));

  3. #3
    shadowayex is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Reading image files into Strings

    ISO-8859-1 does work on this image. Is there any way to figure out how images are encoded? Or is there a standard that says all images have to be encoded under a certain charset?

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

    Default Re: Reading image files into Strings

    The images aren't "encoded" since they aren't Strings. You do need to use a Charset that allows maximum flexibility and accepts all bytes. Probably better would be to manually write and read the byte Strings in and out.

  5. #5
    shadowayex is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Reading image files into Strings

    Ah ok. Well the only one that doesn't give me an error in my code is ISO-8859-1. Would that be a safe one? I'll know nothing about the incoming files. They are user supplied.

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

    Default Re: Reading image files into Strings

    I don't know, but the whole thing sounds iffy to me. Again if I were to do this and I absolutely had to use a String, I'd go the safe route and write each byte out explicitly, possibly to a StringBuilder. Then I'd have no need to worry about Charset.

  7. #7
    shadowayex is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Reading image files into Strings

    Ok. Well it's being saved into an XML file with other data. The current design is to generate the XML and save it in a String and write that all to a file using an output stream. That's the reason for converting it to a String. I do happen to use a StringBuilder for that functionality, so it wouldn't be any extra work to use a StringBuilder.

    Thanks for all the help by the way. I've (obviously) not learned much about encoding, working with bytes, and other things of that nature.

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

    Default Re: Reading image files into Strings

    And I'm not a pro regarding Charsets either, so please take all advice I give on this subject with a grain of salt. I'm learning as I go! Best of luck!

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  2. Bug Reading txt files
    By MHardeman25 in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:03 PM
  3. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM
  4. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 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
  •